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
|
|
|
|
|
|
|
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-09-15 11:29:38 +00:00
|
|
|
|
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-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'"))
|
|
|
|
if support and support['mail_server']:
|
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",
|
|
|
|
"pop3_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
|
|
|
}
|
|
|
|
|
|
|
|
for target_fieldname, source_fieldname in mapping.iteritems():
|
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-03 08:59:44 +00:00
|
|
|
try:
|
|
|
|
account.insert()
|
|
|
|
except frappe.NameError, e:
|
|
|
|
if e.args[0]=="Email Account":
|
|
|
|
existing_account = frappe.get_doc("Email Account", e.args[1])
|
|
|
|
for key, value in account.as_dict().items():
|
|
|
|
if not existing_account.get(key) and value and key not in default_fields:
|
|
|
|
existing_account.set(key, value)
|
|
|
|
|
|
|
|
existing_account.save()
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
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))
|
|
|
|
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-03 08:59:44 +00:00
|
|
|
try:
|
|
|
|
account.insert()
|
|
|
|
except frappe.NameError, e:
|
|
|
|
if e.args[0]=="Email Account":
|
|
|
|
existing_account = frappe.get_doc("Email Account", e.args[1])
|
|
|
|
for key, value in account.as_dict().items():
|
|
|
|
if not existing_account.get(key) and value and key not in default_fields:
|
|
|
|
existing_account.set(key, value)
|
|
|
|
|
|
|
|
existing_account.save()
|
|
|
|
else:
|
|
|
|
raise
|
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)
|
|
|
|
|