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

92 lines
2.7 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'"))
if outgoing and outgoing['mail_server']:
2014-11-12 11:30:42 +00:00
account = frappe.new_doc("Email Account")
mapping = {
"email_id": "mail_login",
"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'"))
if support and support['mail_server']:
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",
"pop3_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))
if source and source.get('host'):
2014-11-12 11:30:42 +00:00
account = frappe.new_doc("Email Account")
mapping = {
"enable_incoming": "extract_emails",
"email_id": "username",
"password": "password",
"pop3_server": "host",
"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, e:
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