Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Anand Doshi | de85a3a | 2014-07-22 19:48:26 +0530 | [diff] [blame] | 2 | # MIT License. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
| 6 | from frappe.celery_app import celery_task, task_logger |
Anand Doshi | 76568f0 | 2015-12-24 15:13:54 +0530 | [diff] [blame] | 7 | from frappe.utils.scheduler import log |
Anand Doshi | de85a3a | 2014-07-22 19:48:26 +0530 | [diff] [blame] | 8 | |
| 9 | @celery_task() |
Anand Doshi | 46256cd | 2014-08-27 12:23:52 +0530 | [diff] [blame] | 10 | def send_newsletter(site, newsletter, event): |
| 11 | # hack! pass event="bulk_long" to queue in longjob queue |
Anand Doshi | de85a3a | 2014-07-22 19:48:26 +0530 | [diff] [blame] | 12 | try: |
| 13 | frappe.connect(site=site) |
| 14 | doc = frappe.get_doc("Newsletter", newsletter) |
| 15 | doc.send_bulk() |
| 16 | |
| 17 | except: |
| 18 | frappe.db.rollback() |
Anand Doshi | 76568f0 | 2015-12-24 15:13:54 +0530 | [diff] [blame] | 19 | |
| 20 | task_logger.error(site) |
| 21 | task_logger.error(frappe.get_traceback()) |
Anand Doshi | de85a3a | 2014-07-22 19:48:26 +0530 | [diff] [blame] | 22 | |
| 23 | # wasn't able to send emails :( |
| 24 | doc.db_set("email_sent", 0) |
| 25 | frappe.db.commit() |
| 26 | |
Anand Doshi | 76568f0 | 2015-12-24 15:13:54 +0530 | [diff] [blame] | 27 | log("send_newsletter") |
| 28 | |
Anand Doshi | de85a3a | 2014-07-22 19:48:26 +0530 | [diff] [blame] | 29 | raise |
| 30 | |
| 31 | else: |
| 32 | frappe.db.commit() |
| 33 | |
| 34 | finally: |
| 35 | frappe.destroy() |