added Email List to Newsletter
This commit is contained in:
parent
b936039c79
commit
39652843bb
@ -18,6 +18,8 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import webnotes
|
import webnotes
|
||||||
import webnotes.utils
|
import webnotes.utils
|
||||||
|
from webnotes.utils import cstr
|
||||||
|
from webnotes.model.doc import Document
|
||||||
|
|
||||||
class DocType():
|
class DocType():
|
||||||
def __init__(self, d, dl):
|
def __init__(self, d, dl):
|
||||||
@ -46,8 +48,7 @@ class DocType():
|
|||||||
}
|
}
|
||||||
|
|
||||||
def send_emails(self):
|
def send_emails(self):
|
||||||
"""send emails to leads and customers"""
|
"""send emails to leads and customers"""
|
||||||
# TODO: create unsubscribed check in customer
|
|
||||||
if self.doc.email_sent:
|
if self.doc.email_sent:
|
||||||
webnotes.msgprint("""Newsletter has already been sent""", raise_exception=1)
|
webnotes.msgprint("""Newsletter has already been sent""", raise_exception=1)
|
||||||
|
|
||||||
@ -67,6 +68,14 @@ class DocType():
|
|||||||
|
|
||||||
if self.doc.blog_subscribers:
|
if self.doc.blog_subscribers:
|
||||||
self.send("blog_subscribers", "Lead")
|
self.send("blog_subscribers", "Lead")
|
||||||
|
|
||||||
|
if self.doc.email_list:
|
||||||
|
email_list = [cstr(email).strip() for email in self.doc.email_list.split(",")]
|
||||||
|
for email in email_list:
|
||||||
|
if not webnotes.conn.exists({"doctype": "Lead", "email_id": email}):
|
||||||
|
create_lead(email)
|
||||||
|
|
||||||
|
self.send(email_list, "Lead")
|
||||||
|
|
||||||
webnotes.conn.set(self.doc, "email_sent", 1)
|
webnotes.conn.set(self.doc, "email_sent", 1)
|
||||||
webnotes.msgprint("""Scheduled to send to %s""" % \
|
webnotes.msgprint("""Scheduled to send to %s""" % \
|
||||||
@ -78,9 +87,8 @@ class DocType():
|
|||||||
recipients = self.doc.test_email_id.split(",")
|
recipients = self.doc.test_email_id.split(",")
|
||||||
from webnotes.utils.email_lib.bulk import send
|
from webnotes.utils.email_lib.bulk import send
|
||||||
send(recipients = recipients, sender = sender,
|
send(recipients = recipients, sender = sender,
|
||||||
subject = self.doc.subject, message = self.get_message(),
|
subject = self.doc.subject, message = self.doc.message,
|
||||||
doctype = doctype, email_field = args["email_field"],
|
doctype = doctype, email_field = args["email_field"])
|
||||||
first_name_field = args["first_name_field"], last_name_field = "")
|
|
||||||
webnotes.msgprint("""Scheduled to send to %s""" % self.doc.test_email_id)
|
webnotes.msgprint("""Scheduled to send to %s""" % self.doc.test_email_id)
|
||||||
|
|
||||||
def get_recipients(self, key):
|
def get_recipients(self, key):
|
||||||
@ -89,21 +97,47 @@ class DocType():
|
|||||||
self.all_recipients += recipients
|
self.all_recipients += recipients
|
||||||
return recipients
|
return recipients
|
||||||
|
|
||||||
def get_message(self):
|
|
||||||
if not hasattr(self, "message"):
|
|
||||||
import markdown2
|
|
||||||
self.message = markdown2.markdown(self.doc.message)
|
|
||||||
return self.message
|
|
||||||
|
|
||||||
def send(self, query_key, doctype):
|
def send(self, query_key, doctype):
|
||||||
webnotes.conn.auto_commit_on_many_writes = True
|
webnotes.conn.auto_commit_on_many_writes = True
|
||||||
recipients = self.get_recipients(query_key)
|
if isinstance(query_key, basestring) and self.query_map.has_key(query_key):
|
||||||
|
recipients = self.get_recipients(query_key)
|
||||||
|
else:
|
||||||
|
recipients = query_key
|
||||||
sender = webnotes.utils.get_email_id(self.doc.owner)
|
sender = webnotes.utils.get_email_id(self.doc.owner)
|
||||||
args = self.dt_map[doctype]
|
args = self.dt_map[doctype]
|
||||||
self.send_count[doctype] = self.send_count.setdefault(doctype, 0) + len(recipients)
|
self.send_count[doctype] = self.send_count.setdefault(doctype, 0) + \
|
||||||
|
len(recipients)
|
||||||
|
|
||||||
from webnotes.utils.email_lib.bulk import send
|
from webnotes.utils.email_lib.bulk import send
|
||||||
send(recipients = recipients, sender = sender,
|
send(recipients = recipients, sender = sender,
|
||||||
subject = self.doc.subject, message = self.get_message(),
|
subject = self.doc.subject, message = self.doc.message,
|
||||||
doctype = doctype, email_field = args["email_field"],
|
doctype = doctype, email_field = args["email_field"])
|
||||||
first_name_field = args["first_name_field"], last_name_field = "")
|
|
||||||
|
lead_naming_series = None
|
||||||
|
def create_lead(email):
|
||||||
|
"""create a lead if it does not exist"""
|
||||||
|
lead = Document("Lead")
|
||||||
|
lead.fields["__islocal"] = 1
|
||||||
|
lead.lead_name = email
|
||||||
|
lead.email_id = email
|
||||||
|
lead.status = "Open"
|
||||||
|
lead.naming_series = lead_naming_series or get_lead_naming_series()
|
||||||
|
lead.company = webnotes.conn.get_default("company")
|
||||||
|
lead.source = "Email"
|
||||||
|
lead.save()
|
||||||
|
|
||||||
|
def get_lead_naming_series():
|
||||||
|
"""gets lead's default naming series"""
|
||||||
|
global lead_naming_series
|
||||||
|
naming_series_field = webnotes.get_doctype("Lead").get_field("naming_series")
|
||||||
|
if naming_series_field.default:
|
||||||
|
lead_naming_series = naming_series_field.default
|
||||||
|
else:
|
||||||
|
latest_naming_series = webnotes.conn.sql("""select naming_series
|
||||||
|
from `tabLead` order by creation desc limit 1""")
|
||||||
|
if latest_naming_series:
|
||||||
|
lead_naming_series = latest_naming_series[0][0]
|
||||||
|
else:
|
||||||
|
lead_naming_series = filter(None, naming_series_field.options.split("\n"))[0]
|
||||||
|
|
||||||
|
return lead_naming_series
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
{
|
{
|
||||||
"owner": "Administrator",
|
"owner": "Administrator",
|
||||||
"docstatus": 0,
|
"docstatus": 0,
|
||||||
"creation": "2012-11-28 11:26:23",
|
"creation": "2012-12-04 11:45:44",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"modified": "2012-12-03 17:10:41"
|
"modified": "2012-12-06 13:07:47"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"autoname": "naming_series:",
|
"autoname": "naming_series:",
|
||||||
@ -106,6 +106,15 @@
|
|||||||
"fieldtype": "Check",
|
"fieldtype": "Check",
|
||||||
"permlevel": 0
|
"permlevel": 0
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"description": "Comma separated list of email addresses",
|
||||||
|
"colour": "White:FFF",
|
||||||
|
"doctype": "DocField",
|
||||||
|
"label": "Send to this list",
|
||||||
|
"fieldname": "email_list",
|
||||||
|
"fieldtype": "Text",
|
||||||
|
"permlevel": 0
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"doctype": "DocField",
|
"doctype": "DocField",
|
||||||
"label": "Newsletter Content",
|
"label": "Newsletter Content",
|
||||||
@ -126,9 +135,8 @@
|
|||||||
"colour": "White:FFF",
|
"colour": "White:FFF",
|
||||||
"doctype": "DocField",
|
"doctype": "DocField",
|
||||||
"label": "Message",
|
"label": "Message",
|
||||||
"options": "Markdown",
|
|
||||||
"fieldname": "message",
|
"fieldname": "message",
|
||||||
"fieldtype": "Code",
|
"fieldtype": "Text Editor",
|
||||||
"reqd": 0,
|
"reqd": 0,
|
||||||
"permlevel": 0
|
"permlevel": 0
|
||||||
},
|
},
|
||||||
|
@ -119,8 +119,6 @@ def add_comment(args=None):
|
|||||||
send(recipients=list(set(commentors + [blog['owner']])),
|
send(recipients=list(set(commentors + [blog['owner']])),
|
||||||
doctype='Comment',
|
doctype='Comment',
|
||||||
email_field='comment_by',
|
email_field='comment_by',
|
||||||
first_name_field="comment_by_fullname",
|
|
||||||
last_name_field="NA",
|
|
||||||
subject='New Comment on Blog: ' + blog['title'],
|
subject='New Comment on Blog: ' + blog['title'],
|
||||||
message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args)
|
message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args)
|
||||||
|
|
||||||
|
@ -44,8 +44,7 @@ class DocType(website.web_page.Page):
|
|||||||
|
|
||||||
# send the blog
|
# send the blog
|
||||||
send(recipients = recipients, doctype='Lead', email_field='email_id',
|
send(recipients = recipients, doctype='Lead', email_field='email_id',
|
||||||
first_name_field = 'lead_name', last_name_field="", subject=self.doc.title,
|
subject=self.doc.title, message = markdown(content))
|
||||||
message = markdown(content))
|
|
||||||
|
|
||||||
webnotes.conn.set(self.doc, 'email_sent', 1)
|
webnotes.conn.set(self.doc, 'email_sent', 1)
|
||||||
webnotes.msgprint("""Scheduled to send to %s subscribers""" % len(recipients))
|
webnotes.msgprint("""Scheduled to send to %s subscribers""" % len(recipients))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user