refactor: code cleanup

This commit is contained in:
kamaljohnson 2022-02-08 12:54:51 +05:30
parent b978b5a70b
commit cb25d894b4
3 changed files with 11 additions and 8 deletions

View file

@ -421,10 +421,10 @@ class EmailAccount(Document):
def get_failed_attempts_count(self):
return cint(frappe.cache().get('{0}:email-account-failed-attempts'.format(self.name)))
def receive(self, test_mails=None):
def receive(self):
"""Called by scheduler to receive emails from this EMail account using POP3/IMAP."""
exceptions = []
inbound_mails = self.get_inbound_mails(test_mails=test_mails)
inbound_mails = self.get_inbound_mails()
for mail in inbound_mails:
try:
communication = mail.process()
@ -457,7 +457,7 @@ class EmailAccount(Document):
if exceptions:
raise Exception(frappe.as_json(exceptions))
def get_inbound_mails(self, test_mails=None) -> List[InboundMail]:
def get_inbound_mails(self) -> List[InboundMail]:
"""retrive and return inbound mails.
"""
@ -466,8 +466,8 @@ class EmailAccount(Document):
def process_mail(messages, append_to=None):
for index, message in enumerate(messages.get("latest_messages", [])):
uid = messages['uid_list'][index] if messages.get('uid_list') else None
seen_status = 1 if messages.get('seen_status', {}).get(uid) == 'SEEN' else 0
if not (self.email_sync_option == 'UNSEEN' and seen_status):
seen_status = messages.get('seen_status', {}).get(uid)
if self.email_sync_option != 'UNSEEN' or seen_status != "SEEN":
# only append the emails with status != 'SEEN' if sync option is set to 'UNSEEN'
mails.append(InboundMail(message, self, uid, seen_status, append_to))

View file

@ -195,7 +195,8 @@ class TestEmailAccount(unittest.TestCase):
# parse reply
messages = {
'"INBOX"': { # append_to = ToDo
# append_to = ToDo
'"INBOX"': {
'latest_messages': [
raw
],
@ -226,7 +227,8 @@ class TestEmailAccount(unittest.TestCase):
# parse reply
messages = {
'"INBOX"': { # append_to = ToDo
# append_to = ToDo
'"INBOX"': {
'latest_messages': test_mails,
'seen_status': {
2: 'UNSEEN',

View file

@ -176,7 +176,8 @@ class TestEmail(unittest.TestCase):
with open(frappe.get_app_path('frappe', 'tests', 'data', 'email_with_image.txt'), 'r') as raw:
messages = {
'"INBOX"': { # append_to = ToDo
# append_to = ToDo
'"INBOX"': {
'latest_messages': [
raw.read()
],