Merge pull request #2877 from mbauskar/develop

Email Account js cleanups
This commit is contained in:
Nabin Hait 2017-03-15 19:38:27 +05:30 committed by GitHub
commit d33fbeaebb
3 changed files with 65 additions and 54 deletions

View file

@ -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</a>');
}
},
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

View file

@ -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
@ -226,7 +239,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:

View file

@ -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()