brotherton-erpnext/erpnext/patches/v4_4/make_email_accounts.py

97 lines
2.9 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
import frappe
from frappe.model import default_fields
2018-02-15 11:28:55 +05:30
from six import iteritems
def execute():
frappe.reload_doc("email", "doctype", "email_account")
# outgoing
2014-11-19 19:28:34 +05:30
outgoing = dict(frappe.db.sql("select field, value from tabSingles where doctype='Outgoing Email Settings'"))
2015-05-12 15:02:33 +05:30
if outgoing and outgoing['mail_server'] and outgoing['mail_login']:
2014-11-12 17:00:42 +05:30
account = frappe.new_doc("Email Account")
mapping = {
"login_id_is_different": 1,
"email_id": "auto_email_id",
"login_id": "mail_login",
2014-11-12 17:00:42 +05:30
"password": "mail_password",
"footer": "footer",
"smtp_server": "mail_server",
"smtp_port": "mail_port",
"use_tls": "use_ssl"
}
2018-02-15 11:28:55 +05:30
for target_fieldname, source_fieldname in iteritems(mapping):
2014-11-12 17:00:42 +05:30
account.set(target_fieldname, outgoing.get(source_fieldname))
2014-11-12 17:00:42 +05:30
account.enable_outgoing = 1
account.enable_incoming = 0
2014-11-12 17:00:42 +05:30
account.insert()
# support
2014-11-19 19:28:34 +05:30
support = dict(frappe.db.sql("select field, value from tabSingles where doctype='Support Email Settings'"))
2015-05-12 15:02:33 +05:30
if support and support['mail_server'] and support['mail_login']:
account = frappe.new_doc("Email Account")
mapping = {
2014-11-12 17:00:42 +05:30
"enable_incoming": "sync_support_mails",
"email_id": "mail_login",
"password": "mail_password",
2015-12-03 08:09:27 +05:30
"email_server": "mail_server",
"use_ssl": "use_ssl",
2014-11-12 17:00:42 +05:30
"signature": "support_signature",
"enable_auto_reply": "send_autoreply",
"auto_reply_message": "support_autoreply"
}
2018-02-15 11:28:55 +05:30
for target_fieldname, source_fieldname in iteritems(mapping):
2014-11-12 17:00:42 +05:30
account.set(target_fieldname, support.get(source_fieldname))
account.enable_outgoing = 0
account.append_to = "Issue"
2015-04-14 13:07:28 +05:30
insert_or_update(account)
2014-11-12 17:00:42 +05:30
# sales, jobs
for doctype in ("Sales Email Settings", "Jobs Email Settings"):
2014-11-19 19:28:34 +05:30
source = dict(frappe.db.sql("select field, value from tabSingles where doctype=%s", doctype))
2015-05-12 15:02:33 +05:30
if source and source.get('host') and source.get('username'):
2014-11-12 17:00:42 +05:30
account = frappe.new_doc("Email Account")
mapping = {
"enable_incoming": "extract_emails",
"email_id": "username",
"password": "password",
2015-12-03 08:09:27 +05:30
"email_server": "host",
2014-11-12 17:00:42 +05:30
"use_ssl": "use_ssl",
}
2018-02-15 11:28:55 +05:30
for target_fieldname, source_fieldname in iteritems(mapping):
2014-11-12 17:00:42 +05:30
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 13:07:28 +05:30
insert_or_update(account)
2014-11-12 17:00:42 +05:30
for doctype in ("Outgoing Email Settings", "Support Email Settings",
"Sales Email Settings", "Jobs Email Settings"):
frappe.delete_doc("DocType", doctype)
2015-04-14 13:07:28 +05:30
def insert_or_update(account):
try:
account.insert()
except frappe.NameError as e:
2015-04-14 13:07:28 +05:30
if e.args[0]=="Email Account":
2015-04-15 14:36:09 +05:30
existing_account = frappe.get_doc("Email Account", e.args[1])
2015-04-14 13:07:28 +05:30
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