From c20d5b9a5724539f11f3e7b8f18eb6a18508675e Mon Sep 17 00:00:00 2001 From: mbauskar Date: Wed, 15 Mar 2017 15:27:46 +0530 Subject: [PATCH 1/3] [minor] fixed email seen/unseen status condition --- frappe/email/doctype/email_account/email_account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index 7b6908f4db..fbc93f82af 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -226,7 +226,7 @@ class EmailAccount(Document): def get_seen(status): if not status: return None - seen = 0 if status == "SEEN" else 1 + seen = 1 if status == "SEEN" else 0 return seen if self.enable_incoming: From 8c1a7883314245af1db726b634207d2112de1495 Mon Sep 17 00:00:00 2001 From: mbauskar Date: Wed, 15 Mar 2017 16:28:50 +0530 Subject: [PATCH 2/3] [minor] fixes in email account update domain trigger --- .../doctype/email_account/email_account.js | 66 +++++++++---------- .../doctype/email_account/email_account.py | 49 +++++++++----- 2 files changed, 63 insertions(+), 52 deletions(-) diff --git a/frappe/email/doctype/email_account/email_account.js b/frappe/email/doctype/email_account/email_account.js index 109a852367..98498eea91 100644 --- a/frappe/email/doctype/email_account/email_account.js +++ b/frappe/email/doctype/email_account/email_account.js @@ -100,10 +100,10 @@ frappe.ui.form.on("Email Account", { frm.set_query("append_to", "frappe.email.doctype.email_account.email_account.get_append_to"); }, validate:function(frm){ - frm.events.update_domain(frm,true); + frm.events.update_domain(frm, true); }, refresh: function(frm) { - frm.events.update_domain(frm,true); + frm.events.update_domain(frm, true); frm.events.enable_incoming(frm); frm.events.notify_if_unreplied(frm); frm.events.show_gmail_message_for_less_secure_apps(frm); @@ -124,6 +124,7 @@ frappe.ui.form.on("Email Account", { } } }, + show_gmail_message_for_less_secure_apps: function(frm) { if(frm.doc.service==="Gmail") { frm.dashboard.set_headline_alert('Gmail will only work if you allow access for less secure \ @@ -131,49 +132,46 @@ frappe.ui.form.on("Email Account", { href="https://support.google.com/accounts/answer/6010255?hl=en">Read this for details'); } }, + email_id:function(frm){ //pull domain and if no matching domain go create one - frm.events.update_domain(frm,false); + frm.events.update_domain(frm, false); }, - update_domain:function(frm,norefresh){ - if (cur_frm.doc.email_id && !cur_frm.doc.service) { - frappe.call({ - method: 'get_domain', - doc: cur_frm.doc, - async:false, - args: { - "email_id": cur_frm.doc.email_id - }, - callback: function (frm) { - if (frm.message) { - if (cur_frm.doc.domain != frm["message"][0]["name"]) { - cur_frm.doc.domain = frm["message"][0]["name"] - cur_frm.doc.email_server = frm["message"][0]["email_server"]; - cur_frm.doc.use_imap = frm["message"][0]["use_imap"]; - cur_frm.doc.smtp_server = frm["message"][0]["smtp_server"]; - cur_frm.doc.use_ssl = frm["message"][0]["use_ssl"]; - cur_frm.doc.use_tls = frm["message"][0]["use_tls"]; - cur_frm.doc.smtp_port = frm["message"][0]["smtp_port"]; - if (!norefresh) { - cur_frm.refresh(); - } - } - }else{ - frappe.confirm( - __('Email Domain not configured for this account, Create one?'), + + update_domain: function(frm, no_refresh){ + if (!frm.doc.email_id && !frm.doc.service) + return + + frappe.call({ + method: 'get_domain', + doc: frm.doc, + args: { + "email_id": frm.doc.email_id + }, + callback: function (r) { + if (r.message) { + domain = r.message; + + for(field in domain) + frm.set_value(field, domain[field]); + + if (!no_refresh) + frm.refresh(); + } else { + frm.set_value("domain", "") + frappe.confirm(__('Email Domain not configured for this account, Create one?'), function () { frappe.model.with_doctype("Email Domain", function() { - frappe.route_options = {email_id: cur_frm.doc.email_id}; + frappe.route_options = { email_id: frm.doc.email_id }; frappe.route_flags.return_to_email_account = 1 var doc = frappe.model.get_new_doc("Email Domain"); frappe.set_route("Form", "Email Domain", doc.name); }) } - ) - } + ); } - }); - } + } + }); }, email_sync_option: function(frm) { // confirm if the ALL sync option is selected diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index fbc93f82af..77ca47c61f 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -45,6 +45,14 @@ class EmailAccount(Document): else: self.login_id = None + duplicate_email_account = frappe.get_all("Email Account", filters={ + "email_id": self.email_id, + "name": ("!=", self.name) + }) + if duplicate_email_account: + frappe.throw(_("Email id must be unique, Email Account is already exist \ + for {0}".format(frappe.bold(self.email_id)))) + if frappe.local.flags.in_patch or frappe.local.flags.in_test: return @@ -78,39 +86,44 @@ class EmailAccount(Document): def on_update(self): """Check there is only one default of each type.""" + from frappe.core.doctype.user.user import ask_pass_update + self.there_must_be_only_one_default() if self.awaiting_password: # push values to user_emails frappe.db.sql("""UPDATE `tabUser Email` SET awaiting_password = 1 - WHERE email_account = %(account)s""", {"account": self.name}) + WHERE email_account = %(account)s""", {"account": self.name}) else: frappe.db.sql("""UPDATE `tabUser Email` SET awaiting_password = 0 - WHERE email_account = %(account)s""", {"account": self.name}) - from frappe.core.doctype.user.user import ask_pass_update + WHERE email_account = %(account)s""", {"account": self.name}) + ask_pass_update() def there_must_be_only_one_default(self): """If current Email Account is default, un-default all other accounts.""" - for fn in ("default_incoming", "default_outgoing"): - if self.get(fn): - for email_account in frappe.get_all("Email Account", - filters={fn: 1}): - if email_account.name==self.name: - continue - email_account = frappe.get_doc("Email Account", - email_account.name) - email_account.set(fn, 0) - email_account.save() + for field in ("default_incoming", "default_outgoing"): + if not self.get(field): + continue + + for email_account in frappe.get_all("Email Account", filters={ field: 1 }): + if email_account.name==self.name: + continue + + email_account = frappe.get_doc("Email Account", email_account.name) + email_account.set(field, 0) + email_account.save() @frappe.whitelist() - def get_domain(self,email_id): + def get_domain(self, email_id): """look-up the domain and then full""" try: domain = email_id.split("@") - return frappe.db.sql("""select name,use_imap,email_server,use_ssl,smtp_server,use_tls,smtp_port - from `tabEmail Domain` - where name = %s - """,domain[1],as_dict=1) + fields = [ + "name as domain", "use_imap", "email_server", + "use_ssl", "smtp_server", "use_tls", + "smtp_port" + ] + return frappe.db.get_value("Email Domain", domain[1], fields, as_dict=True) except Exception: pass From 8454b195e3b5221c41c709adf06c2961c118b955 Mon Sep 17 00:00:00 2001 From: mbauskar Date: Wed, 15 Mar 2017 18:32:03 +0530 Subject: [PATCH 3/3] [minor] fixed test_password test case --- frappe/tests/test_password.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/tests/test_password.py b/frappe/tests/test_password.py index c659401633..acaca6a290 100644 --- a/frappe/tests/test_password.py +++ b/frappe/tests/test_password.py @@ -39,7 +39,7 @@ class TestPassword(unittest.TestCase): 'append_to': 'Communication', 'smtp_server': 'test.example.com', 'pop3_server': 'pop.test.example.com', - 'email_id': 'test@example.com', + 'email_id': 'test-password@example.com', 'password': 'password', }).insert()