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

94 lines
2.8 KiB
Python
Raw Normal View History

import frappe
from frappe.model import default_fields
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 = {
"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-11-12 11:30:42 +00:00
for target_fieldname, source_fieldname in mapping.iteritems():
account.set(target_fieldname, outgoing.get(source_fieldname))
2014-11-12 11:30:42 +00:00
account.enable_outgoing = 1
account.enable_incoming = 0
2014-11-12 11:30:42 +00:00
account.insert()
# 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']:
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",
"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"
}
for target_fieldname, source_fieldname in mapping.iteritems():
2014-11-12 11:30:42 +00:00
account.set(target_fieldname, support.get(source_fieldname))
account.enable_outgoing = 0
account.append_to = "Issue"
2015-04-14 07:37:28 +00:00
insert_or_update(account)
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",
}
for target_fieldname, source_fieldname in mapping.iteritems():
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
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()
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