mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-05 06:28:29 +00:00
upgraded test_runner, now can run multiple tests
This commit is contained in:
@@ -29,4 +29,13 @@ cur_frm.cscript.refresh = function(doc) {
|
||||
cur_frm.set_value("send_from",
|
||||
repl("%(fullname)s <%(email)s>", wn.user_info(doc.owner)));
|
||||
}
|
||||
|
||||
wn.call({
|
||||
method: "support.doctype.newsletter.newsletter.get_lead_options",
|
||||
type: "GET",
|
||||
callback: function(r) {
|
||||
set_field_options("lead_source", r.message.sources.join("\n"))
|
||||
set_field_options("lead_status", r.message.statuses.join("\n"))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -25,100 +25,72 @@ from webnotes import _
|
||||
class DocType():
|
||||
def __init__(self, d, dl):
|
||||
self.doc, self.doclist = d, dl
|
||||
self.dt_map = {
|
||||
"Contact": {
|
||||
"email_field": "email_id",
|
||||
"first_name_field": "first_name",
|
||||
},
|
||||
"Lead": {
|
||||
"email_field": "email_id",
|
||||
"first_name_field": "lead_name"
|
||||
}
|
||||
}
|
||||
self.query_map = {
|
||||
"contacts": """select distinct email_id from `tabContact`
|
||||
where ifnull(email_id, '') != '' """,
|
||||
"customer_contacts": """select distinct email_id from `tabContact`
|
||||
where ifnull(customer, '') != '' and ifnull(email_id, '') != '' """,
|
||||
"leads": """select distinct email_id from `tabLead`
|
||||
where ifnull(email_id, '') != '' """,
|
||||
"active_leads": """select distinct email_id from `tabLead`
|
||||
where status = "Open" and ifnull(email_id, '') != '' """,
|
||||
"blog_subscribers": """select distinct email_id from `tabLead`
|
||||
where ifnull(blog_subscriber,0) = 1 and ifnull(email_id, '') != '' """
|
||||
}
|
||||
|
||||
|
||||
def test_send(self, doctype="Lead"):
|
||||
self.recipients = self.doc.test_email_id.split(",")
|
||||
self.send_bulk()
|
||||
webnotes.msgprint("""Scheduled to send to %s""" % self.doc.test_email_id)
|
||||
|
||||
def send_emails(self):
|
||||
"""send emails to leads and customers"""
|
||||
if self.doc.email_sent:
|
||||
webnotes.msgprint("""Newsletter has already been sent""", raise_exception=1)
|
||||
|
||||
self.all_recipients = []
|
||||
self.send_count = {}
|
||||
self.recipients = self.get_recipients()
|
||||
self.send_bulk()
|
||||
|
||||
if self.doc.contacts:
|
||||
self.send("contacts", "Contact")
|
||||
elif self.doc.customer_contacts:
|
||||
self.send("customer_contacts", "Contact")
|
||||
|
||||
if self.doc.leads:
|
||||
self.send("leads", "Lead")
|
||||
else:
|
||||
if self.doc.active_leads:
|
||||
self.send("active_leads", "Lead")
|
||||
webnotes.msgprint("""Scheduled to send to %d %s(s)""" % (len(self.recipients),
|
||||
self.send_to_doctype))
|
||||
|
||||
webnotes.conn.set(self.doc, "email_sent", 1)
|
||||
|
||||
def get_recipients(self):
|
||||
if self.doc.send_to_type=="Contact":
|
||||
self.send_to_doctype = "Contact"
|
||||
if self.doc.contact_type == "Customer":
|
||||
return webnotes.conn.sql_list("""select email_id from tabContact
|
||||
where ifnull(email_id, '') != '' and ifnull(customer, '') != ''""")
|
||||
|
||||
elif self.doc.contact_type == "Supplier":
|
||||
return webnotes.conn.sql_list("""select email_id from tabContact
|
||||
where ifnull(email_id, '') != '' and ifnull(supplier, '') != ''""")
|
||||
|
||||
elif self.doc.send_to_type=="Lead":
|
||||
self.send_to_doctype = "Lead"
|
||||
conditions = []
|
||||
if self.doc.lead_source and self.doc.lead_source != "All":
|
||||
conditions.append(" and source='%s'" % self.doc.lead_source)
|
||||
if self.doc.lead_status and self.doc.lead_status != "All":
|
||||
conditions.append(" and status='%s'" % self.doc.lead_status)
|
||||
|
||||
if conditions:
|
||||
conditions = "".join(conditions)
|
||||
|
||||
if self.doc.blog_subscribers:
|
||||
self.send("blog_subscribers", "Lead")
|
||||
|
||||
if self.doc.email_list:
|
||||
return webnotes.conn.sql_list("""select email_id from tabLead
|
||||
where ifnull(email_id, '') != '' %s""" % (conditions or ""))
|
||||
|
||||
elif 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.msgprint("""Scheduled to send to %s""" % \
|
||||
", ".join(["%d %s(s)" % (self.send_count[s], s) for s in self.send_count]))
|
||||
|
||||
def test_send(self, doctype="Lead"):
|
||||
|
||||
self.send_to_doctype = "Lead"
|
||||
return email_list
|
||||
|
||||
def send_bulk(self):
|
||||
self.validate_send()
|
||||
|
||||
args = self.dt_map[doctype]
|
||||
|
||||
sender = self.doc.send_from or webnotes.utils.get_formatted_email(self.doc.owner)
|
||||
recipients = self.doc.test_email_id.split(",")
|
||||
|
||||
from webnotes.utils.email_lib.bulk import send
|
||||
send(recipients = recipients, sender = sender,
|
||||
subject = self.doc.subject, message = self.doc.message,
|
||||
doctype = doctype, email_field = args["email_field"])
|
||||
webnotes.msgprint("""Scheduled to send to %s""" % self.doc.test_email_id)
|
||||
|
||||
def get_recipients(self, key):
|
||||
recipients = webnotes.conn.sql(self.query_map[key])
|
||||
recipients = [r[0] for r in recipients if r not in self.all_recipients]
|
||||
self.all_recipients += recipients
|
||||
return recipients
|
||||
|
||||
def send(self, query_key, doctype):
|
||||
self.validate_send()
|
||||
|
||||
webnotes.conn.auto_commit_on_many_writes = True
|
||||
if isinstance(query_key, basestring) and self.query_map.has_key(query_key):
|
||||
recipients = self.get_recipients(query_key)
|
||||
else:
|
||||
recipients = query_key
|
||||
sender = self.doc.send_from or webnotes.utils.get_formatted_email(self.doc.owner)
|
||||
args = self.dt_map[doctype]
|
||||
self.send_count[doctype] = self.send_count.setdefault(doctype, 0) + \
|
||||
len(recipients)
|
||||
|
||||
from webnotes.utils.email_lib.bulk import send
|
||||
send(recipients = recipients, sender = sender,
|
||||
send(recipients = self.recipients, sender = sender,
|
||||
subject = self.doc.subject, message = self.doc.message,
|
||||
doctype = doctype, email_field = args["email_field"])
|
||||
doctype = self.send_to_doctype, email_field = "email_id")
|
||||
|
||||
webnotes.conn.set(self.doc, "email_sent", 1)
|
||||
webnotes.conn.auto_commit_on_many_writes = False
|
||||
|
||||
def validate_send(self):
|
||||
if self.doc.fields.get("__islocal"):
|
||||
@@ -130,6 +102,14 @@ class DocType():
|
||||
webnotes.msgprint(_("""Sending newsletters is not allowed for Trial users, \
|
||||
to prevent abuse of this feature."""), raise_exception=1)
|
||||
|
||||
@webnotes.whitelist()
|
||||
def get_lead_options():
|
||||
return {
|
||||
"sources": ["All"] + webnotes.conn.sql_list("""select distinct source from tabLead"""),
|
||||
"statuses": ["All"]+ webnotes.conn.sql_list("""select distinct status from tabLead""")
|
||||
}
|
||||
|
||||
|
||||
lead_naming_series = None
|
||||
def create_lead(email_id):
|
||||
"""create a lead if it does not exist"""
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
"creation": "2013-01-10 16:34:31",
|
||||
"docstatus": 0,
|
||||
"modified": "2013-01-28 15:28:59",
|
||||
"modified": "2013-02-11 17:23:08",
|
||||
"modified_by": "Administrator",
|
||||
"owner": "Administrator"
|
||||
},
|
||||
@@ -58,33 +58,10 @@
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"fieldname": "contacts",
|
||||
"fieldtype": "Check",
|
||||
"label": "All Contacts"
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"fieldname": "customer_contacts",
|
||||
"fieldtype": "Check",
|
||||
"label": "All Customer Contacts"
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"fieldname": "leads",
|
||||
"fieldtype": "Check",
|
||||
"label": "All Leads"
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"fieldname": "active_leads",
|
||||
"fieldtype": "Check",
|
||||
"label": "All Active Leads"
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"fieldname": "blog_subscribers",
|
||||
"fieldtype": "Check",
|
||||
"label": "All Blog Subscribers"
|
||||
"fieldname": "send_to_type",
|
||||
"fieldtype": "Select",
|
||||
"label": "Send To Type",
|
||||
"options": "Lead\nContact\nCustom"
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
@@ -92,6 +69,29 @@
|
||||
"fieldtype": "Column Break"
|
||||
},
|
||||
{
|
||||
"depends_on": "eval:doc.send_to_type==\"Lead\"",
|
||||
"doctype": "DocField",
|
||||
"fieldname": "lead_source",
|
||||
"fieldtype": "Select",
|
||||
"label": "Lead Source"
|
||||
},
|
||||
{
|
||||
"depends_on": "eval:doc.send_to_type==\"Lead\"",
|
||||
"doctype": "DocField",
|
||||
"fieldname": "lead_status",
|
||||
"fieldtype": "Select",
|
||||
"label": "Lead Status"
|
||||
},
|
||||
{
|
||||
"depends_on": "eval:doc.send_to_type==\"Contact\"",
|
||||
"doctype": "DocField",
|
||||
"fieldname": "contact_type",
|
||||
"fieldtype": "Select",
|
||||
"label": "Contact Type",
|
||||
"options": "Customer\nSupplier\nCustom"
|
||||
},
|
||||
{
|
||||
"depends_on": "eval:doc.send_to_type==\"Custom\"",
|
||||
"description": "Comma separated list of email addresses",
|
||||
"doctype": "DocField",
|
||||
"fieldname": "email_list",
|
||||
|
||||
58
support/doctype/newsletter/test_newsletter.py
Normal file
58
support/doctype/newsletter/test_newsletter.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import webnotes, unittest
|
||||
|
||||
class TestNewsletter(unittest.TestCase):
|
||||
def test_get_recipients_lead(self):
|
||||
w = webnotes.model_wrapper(test_records[0])
|
||||
w.insert()
|
||||
self.assertTrue("test_lead@example.com" in w.controller.get_recipients())
|
||||
webnotes.conn.sql("""delete from `tabBulk Email`""")
|
||||
w.controller.send_emails()
|
||||
self.assertTrue(webnotes.conn.get_value("Bulk Email", {"recipient": "test_lead@example.com"}))
|
||||
|
||||
def test_get_recipients_lead_by_status(self):
|
||||
w = webnotes.model_wrapper(test_records[0])
|
||||
w.doc.lead_status="Converted"
|
||||
w.insert()
|
||||
self.assertTrue("test_lead3@example.com" in w.controller.get_recipients())
|
||||
|
||||
def test_get_recipients_contact_customer(self):
|
||||
w = webnotes.model_wrapper(test_records[1])
|
||||
w.insert()
|
||||
self.assertTrue("test_contact_customer@example.com" in w.controller.get_recipients())
|
||||
|
||||
def test_get_recipients_contact_supplier(self):
|
||||
w = webnotes.model_wrapper(test_records[1])
|
||||
w.doc.contact_type="Supplier"
|
||||
w.insert()
|
||||
self.assertTrue("test_contact_supplier@example.com" in w.controller.get_recipients())
|
||||
|
||||
def test_get_recipients_custom(self):
|
||||
w = webnotes.model_wrapper(test_records[2])
|
||||
w.insert()
|
||||
self.assertTrue("test_custom2@example.com" in w.controller.get_recipients())
|
||||
self.assertTrue(webnotes.conn.get("Lead",
|
||||
{"email_id": "test_custom2@example.com"}))
|
||||
|
||||
|
||||
test_dependencies = ["Lead", "Contact"]
|
||||
|
||||
test_records =[
|
||||
[{
|
||||
"subject": "_Test Newsletter to Lead",
|
||||
"send_to_type": "Lead",
|
||||
"lead_source": "All",
|
||||
"message": "This is a test newsletter"
|
||||
}],
|
||||
[{
|
||||
"subject": "_Test Newsletter to Contact",
|
||||
"send_to_type": "Contact",
|
||||
"contact_type": "Customer",
|
||||
"message": "This is a test newsletter"
|
||||
}],
|
||||
[{
|
||||
"subject": "_Test Newsletter to Custom",
|
||||
"send_to_type": "Custom",
|
||||
"email_list": "test_custom@example.com, test_custom1@example.com, test_custom2@example.com",
|
||||
"message": "This is a test newsletter"
|
||||
}],
|
||||
]
|
||||
Reference in New Issue
Block a user