From d1a199258daa62d2a79d99ccde23e6013b14302d Mon Sep 17 00:00:00 2001 From: phot0n Date: Tue, 12 Jul 2022 17:47:47 +0530 Subject: [PATCH] fix: pull from accounts for oauth whose refresh_token is not null * chore: rename Oauth to OAuth --- .../doctype/email_account/email_account.js | 6 ++-- .../doctype/email_account/email_account.json | 4 +-- .../doctype/email_account/email_account.py | 31 ++++++++++++------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/frappe/email/doctype/email_account/email_account.js b/frappe/email/doctype/email_account/email_account.js index f86ec12344..34d6e6acd8 100644 --- a/frappe/email/doctype/email_account/email_account.js +++ b/frappe/email/doctype/email_account/email_account.js @@ -166,7 +166,7 @@ frappe.ui.form.on("Email Account", { }, after_save(frm) { - if (frm.doc.auth_method === "Oauth" && !frm.doc.refresh_token) { + if (frm.doc.auth_method === "OAuth" && !frm.doc.refresh_token) { oauth_access(frm); } }, @@ -191,9 +191,9 @@ frappe.ui.form.on("Email Account", { }, show_oauth_authorization_message(frm) { - if (frm.doc.auth_method === "Oauth") { + if (frm.doc.auth_method === "OAuth") { let msg = { - message: !frm.doc.refresh_token ? "Oauth Enabled but not Authorized. Please use Authorize API Access Button to do the same." : "Oauth Authorized. Re-Authorization can be done using Authorize API Access Button.", + message: !frm.doc.refresh_token ? "OAuth Enabled but not Authorized. Please use Authorize API Access Button to do the same." : "OAuth Authorized. Re-Authorization can be done using Authorize API Access Button.", indicator: !frm.doc.refresh_token ? "yellow" : "green" }; frm.dashboard.clear_headline(); diff --git a/frappe/email/doctype/email_account/email_account.json b/frappe/email/doctype/email_account/email_account.json index feb1a998f6..ecb5af7378 100644 --- a/frappe/email/doctype/email_account/email_account.json +++ b/frappe/email/doctype/email_account/email_account.json @@ -586,7 +586,7 @@ "label": "IMAP Details" }, { - "depends_on": "eval: doc.service === \"GMail\" && doc.auth_method === \"Oauth\" && !doc.__islocal && !doc.__unsaved", + "depends_on": "eval: doc.service === \"GMail\" && doc.auth_method === \"OAuth\" && !doc.__islocal && !doc.__unsaved", "fieldname": "authorize_api_access", "fieldtype": "Button", "label": "Authorize API Access" @@ -610,7 +610,7 @@ "fieldname": "auth_method", "fieldtype": "Select", "label": "Method", - "options": "Basic\nOauth" + "options": "Basic\nOAuth" } ], "icon": "fa fa-inbox", diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index 8f2107e165..589ddf42f0 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -81,7 +81,7 @@ class EmailAccount(Document): if frappe.local.flags.in_patch or frappe.local.flags.in_test: return - use_oauth = self.auth_method == "Oauth" + use_oauth = self.auth_method == "OAuth" if getattr(self, "service", "") != "GMail" and use_oauth: self.auth_method = "Basic" @@ -159,7 +159,7 @@ class EmailAccount(Document): awaiting_password=self.awaiting_password, email_id=self.email_id, enable_outgoing=self.enable_outgoing, - used_oauth=self.auth_method == "Oauth", + used_oauth=self.auth_method == "OAuth", ) def there_must_be_only_one_default(self): @@ -214,7 +214,7 @@ class EmailAccount(Document): "email_sync_rule": email_sync_rule, "incoming_port": get_port(self), "initial_sync_count": self.initial_sync_count or 100, - "use_oauth": self.auth_method == "Oauth", + "use_oauth": self.auth_method == "OAuth", "refresh_token": decrypt(self.refresh_token) if self.refresh_token else None, "access_token": decrypt(self.access_token) if self.access_token else None, } @@ -284,7 +284,7 @@ class EmailAccount(Document): @property def _password(self): raise_exception = not ( - self.auth_method == "Oauth" or self.no_smtp_authentication or frappe.flags.in_test + self.auth_method == "OAuth" or self.no_smtp_authentication or frappe.flags.in_test ) return self.get_password(raise_exception=raise_exception) @@ -432,7 +432,7 @@ class EmailAccount(Document): "use_ssl": cint(self.use_ssl_for_outgoing), "use_tls": cint(self.use_tls), "service": getattr(self, "service", ""), - "use_oauth": self.auth_method == "Oauth", + "use_oauth": self.auth_method == "OAuth", "refresh_token": decrypt(self.refresh_token) if self.refresh_token else None, "access_token": decrypt(self.access_token) if self.access_token else None, } @@ -802,12 +802,18 @@ def pull(now=False): else: return - queued_jobs = get_jobs(site=frappe.local.site, key="job_name")[frappe.local.site] - for email_account in frappe.get_list( - "Email Account", - filters={"enable_incoming": 1}, - or_filters={"awaiting_password": 0, "auth_method": "Oauth"}, - ): + doctype = frappe.qb.DocType("Email Account") + email_accounts = ( + frappe.qb.from_(doctype) + .select(doctype.name) + .where(doctype.enable_incoming == 1) + .where( + (doctype.awaiting_password == 0) + | ((doctype.auth_method == "OAuth") & (doctype.refresh_token.isnotnull())) + ) + .run(as_dict=1) + ) + for email_account in email_accounts: if now: pull_from_email_account(email_account.name) @@ -815,6 +821,7 @@ def pull(now=False): # job_name is used to prevent duplicates in queue job_name = f"pull_from_email_account|{email_account.name}" + queued_jobs = get_jobs(site=frappe.local.site, key="job_name")[frappe.local.site] if job_name not in queued_jobs: enqueue( pull_from_email_account, @@ -929,7 +936,7 @@ def remove_user_email_inbox(email_account): @frappe.whitelist() def set_email_password(email_account, password): account = frappe.get_doc("Email Account", email_account) - if account.awaiting_password and not account.auth_method == "Oauth": + if account.awaiting_password and not account.auth_method == "OAuth": account.awaiting_password = 0 account.password = password try: