2019-01-22 12:52:20 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-09-15 11:29:38 +00:00
|
|
|
import frappe
|
2015-04-03 08:59:44 +00:00
|
|
|
from frappe.model import default_fields
|
2014-09-15 11:29:38 +00:00
|
|
|
|
2018-02-15 05:58:55 +00:00
|
|
|
from six import iteritems
|
|
|
|
|
2014-09-15 11:29:38 +00:00
|
|
|
def execute():
|
|
|
|
frappe.reload_doc("email", "doctype", "email_account")
|
|
|
|
|
|
|
|
# outgoing
|
2014-11-19 13:58:34 +00:00
|
|
|
outgoing = dict(frappe.db.sql("select field, value from tabSingles where doctype='Outgoing Email Settings'"))
|
2015-05-12 09:32:33 +00:00
|
|
|
if outgoing and outgoing['mail_server'] and outgoing['mail_login']:
|
2014-11-12 11:30:42 +00:00
|
|
|
account = frappe.new_doc("Email Account")
|
|
|
|
mapping = {
|
2015-10-31 17:19:42 +00:00
|
|
|
"login_id_is_different": 1,
|
|
|
|
"email_id": "auto_email_id",
|
|
|
|
"login_id": "mail_login",
|
2014-11-12 11:30:42 +00:00
|
|
|
"password": "mail_password",
|
|
|
|
"footer": "footer",
|
|
|
|
"smtp_server": "mail_server",
|
|
|
|
"smtp_port": "mail_port",
|
|
|
|
"use_tls": "use_ssl"
|
|
|
|
}
|
2014-09-15 11:29:38 +00:00
|
|
|
|
2018-02-15 05:58:55 +00:00
|
|
|
for target_fieldname, source_fieldname in iteritems(mapping):
|
2014-11-12 11:30:42 +00:00
|
|
|
account.set(target_fieldname, outgoing.get(source_fieldname))
|
2014-09-15 11:29:38 +00:00
|
|
|
|
2014-11-12 11:30:42 +00:00
|
|
|
account.enable_outgoing = 1
|
|
|
|
account.enable_incoming = 0
|
2014-09-15 11:29:38 +00:00
|
|
|
|
2014-11-12 11:30:42 +00:00
|
|
|
account.insert()
|
2014-09-15 11:29:38 +00:00
|
|
|
|
|
|
|
# support
|
2014-11-19 13:58:34 +00:00
|
|
|
support = dict(frappe.db.sql("select field, value from tabSingles where doctype='Support Email Settings'"))
|
2015-05-12 09:32:33 +00:00
|
|
|
if support and support['mail_server'] and support['mail_login']:
|
2014-09-15 11:29:38 +00:00
|
|
|
account = frappe.new_doc("Email Account")
|
|
|
|
mapping = {
|
2014-11-12 11:30:42 +00:00
|
|
|
"enable_incoming": "sync_support_mails",
|
|
|
|
"email_id": "mail_login",
|
|
|
|
"password": "mail_password",
|
2015-12-03 02:39:27 +00:00
|
|
|
"email_server": "mail_server",
|
2014-09-15 11:29:38 +00:00
|
|
|
"use_ssl": "use_ssl",
|
2014-11-12 11:30:42 +00:00
|
|
|
"signature": "support_signature",
|
|
|
|
"enable_auto_reply": "send_autoreply",
|
|
|
|
"auto_reply_message": "support_autoreply"
|
2014-09-15 11:29:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 05:58:55 +00:00
|
|
|
for target_fieldname, source_fieldname in iteritems(mapping):
|
2014-11-12 11:30:42 +00:00
|
|
|
account.set(target_fieldname, support.get(source_fieldname))
|
2014-09-15 11:29:38 +00:00
|
|
|
|
|
|
|
account.enable_outgoing = 0
|
2015-03-30 17:05:51 +00:00
|
|
|
account.append_to = "Issue"
|
2014-09-15 11:29:38 +00:00
|
|
|
|
2015-04-14 07:37:28 +00:00
|
|
|
insert_or_update(account)
|
2014-09-15 11:29:38 +00:00
|
|
|
|
2014-11-12 11:30:42 +00:00
|
|
|
# sales, jobs
|
|
|
|
for doctype in ("Sales Email Settings", "Jobs Email Settings"):
|
2014-11-19 13:58:34 +00:00
|
|
|
source = dict(frappe.db.sql("select field, value from tabSingles where doctype=%s", doctype))
|
2015-05-12 09:32:33 +00:00
|
|
|
if source and source.get('host') and source.get('username'):
|
2014-11-12 11:30:42 +00:00
|
|
|
account = frappe.new_doc("Email Account")
|
|
|
|
mapping = {
|
|
|
|
"enable_incoming": "extract_emails",
|
|
|
|
"email_id": "username",
|
|
|
|
"password": "password",
|
2015-12-03 02:39:27 +00:00
|
|
|
"email_server": "host",
|
2014-11-12 11:30:42 +00:00
|
|
|
"use_ssl": "use_ssl",
|
|
|
|
}
|
|
|
|
|
2018-02-15 05:58:55 +00:00
|
|
|
for target_fieldname, source_fieldname in iteritems(mapping):
|
2014-11-12 11:30:42 +00:00
|
|
|
account.set(target_fieldname, source.get(source_fieldname))
|
|
|
|
|
|
|
|
account.enable_outgoing = 0
|
|
|
|
account.append_to = "Lead" if doctype=="Sales Email Settings" else "Job Applicant"
|
|
|
|
|
2015-04-14 07:37:28 +00:00
|
|
|
insert_or_update(account)
|
2014-11-12 11:30:42 +00:00
|
|
|
|
2014-09-15 11:29:38 +00:00
|
|
|
for doctype in ("Outgoing Email Settings", "Support Email Settings",
|
|
|
|
"Sales Email Settings", "Jobs Email Settings"):
|
|
|
|
frappe.delete_doc("DocType", doctype)
|
|
|
|
|
2015-04-14 07:37:28 +00:00
|
|
|
def insert_or_update(account):
|
|
|
|
try:
|
|
|
|
account.insert()
|
2017-07-27 05:08:35 +00:00
|
|
|
except frappe.NameError as e:
|
2015-04-14 07:37:28 +00:00
|
|
|
if e.args[0]=="Email Account":
|
2015-04-15 09:06:09 +00:00
|
|
|
existing_account = frappe.get_doc("Email Account", e.args[1])
|
2015-04-14 07:37:28 +00:00
|
|
|
for key, value in account.as_dict().items():
|
|
|
|
if not existing_account.get(key) and value \
|
|
|
|
and key not in default_fields \
|
|
|
|
and key != "__islocal":
|
|
|
|
existing_account.set(key, value)
|
|
|
|
|
|
|
|
existing_account.save()
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|