Newsletter: Bulk Send via worker
This commit is contained in:
parent
cb86d597b7
commit
de85a3af15
@ -8,11 +8,12 @@ import frappe.utils
|
|||||||
from frappe.utils import cstr
|
from frappe.utils import cstr
|
||||||
from frappe import throw, _
|
from frappe import throw, _
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
import erpnext.tasks
|
||||||
|
|
||||||
class Newsletter(Document):
|
class Newsletter(Document):
|
||||||
def onload(self):
|
def onload(self):
|
||||||
if self.email_sent:
|
if self.email_sent:
|
||||||
self.get("__onload").status_count = dict(frappe.db.sql("""select status, count(*)
|
self.get("__onload").status_count = dict(frappe.db.sql("""select status, count(name)
|
||||||
from `tabBulk Email` where ref_doctype=%s and ref_docname=%s
|
from `tabBulk Email` where ref_doctype=%s and ref_docname=%s
|
||||||
group by status""", (self.doctype, self.name))) or None
|
group by status""", (self.doctype, self.name))) or None
|
||||||
|
|
||||||
@ -28,7 +29,13 @@ class Newsletter(Document):
|
|||||||
throw(_("Newsletter has already been sent"))
|
throw(_("Newsletter has already been sent"))
|
||||||
|
|
||||||
self.recipients = self.get_recipients()
|
self.recipients = self.get_recipients()
|
||||||
self.send_bulk()
|
|
||||||
|
if frappe.local.is_ajax:
|
||||||
|
# to avoid request timed out!
|
||||||
|
self.validate_send()
|
||||||
|
erpnext.tasks.send_newsletter.delay(frappe.local.site, self.name)
|
||||||
|
else:
|
||||||
|
self.send_bulk()
|
||||||
|
|
||||||
frappe.msgprint(_("Scheduled to send to {0} recipients").format(len(self.recipients)))
|
frappe.msgprint(_("Scheduled to send to {0} recipients").format(len(self.recipients)))
|
||||||
|
|
||||||
@ -77,6 +84,10 @@ class Newsletter(Document):
|
|||||||
return email_list
|
return email_list
|
||||||
|
|
||||||
def send_bulk(self):
|
def send_bulk(self):
|
||||||
|
if not self.get("recipients"):
|
||||||
|
# in case it is called via worker
|
||||||
|
self.recipients = self.get_recipients()
|
||||||
|
|
||||||
self.validate_send()
|
self.validate_send()
|
||||||
|
|
||||||
sender = self.send_from or frappe.utils.get_formatted_email(self.owner)
|
sender = self.send_from or frappe.utils.get_formatted_email(self.owner)
|
||||||
@ -98,10 +109,6 @@ class Newsletter(Document):
|
|||||||
if self.get("__islocal"):
|
if self.get("__islocal"):
|
||||||
throw(_("Please save the Newsletter before sending"))
|
throw(_("Please save the Newsletter before sending"))
|
||||||
|
|
||||||
from frappe import conf
|
|
||||||
if (conf.get("status") or None) == "Trial":
|
|
||||||
throw(_("Newsletters is not allowed for Trial users"))
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_lead_options():
|
def get_lead_options():
|
||||||
return {
|
return {
|
||||||
|
29
erpnext/tasks.py
Normal file
29
erpnext/tasks.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
||||||
|
# MIT License. See license.txt
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
import frappe
|
||||||
|
from frappe.celery_app import celery_task, task_logger
|
||||||
|
|
||||||
|
@celery_task()
|
||||||
|
def send_newsletter(site, newsletter):
|
||||||
|
try:
|
||||||
|
frappe.connect(site=site)
|
||||||
|
doc = frappe.get_doc("Newsletter", newsletter)
|
||||||
|
doc.send_bulk()
|
||||||
|
|
||||||
|
except:
|
||||||
|
frappe.db.rollback()
|
||||||
|
task_logger.warn(frappe.get_traceback())
|
||||||
|
|
||||||
|
# wasn't able to send emails :(
|
||||||
|
doc.db_set("email_sent", 0)
|
||||||
|
frappe.db.commit()
|
||||||
|
|
||||||
|
raise
|
||||||
|
|
||||||
|
else:
|
||||||
|
frappe.db.commit()
|
||||||
|
|
||||||
|
finally:
|
||||||
|
frappe.destroy()
|
Loading…
x
Reference in New Issue
Block a user