From e5556e15580cc99f0026d575ef320a34f06f7ae1 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 1 Apr 2015 21:10:14 +0530 Subject: [PATCH] [bulk] flush fix, saved by test case --- frappe/email/bulk.py | 9 ++++++--- frappe/tests/test_email.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/frappe/email/bulk.py b/frappe/email/bulk.py index f2f317c3ce..2dd862874a 100644 --- a/frappe/email/bulk.py +++ b/frappe/email/bulk.py @@ -9,7 +9,7 @@ from frappe.email.smtp import SMTPServer, get_outgoing_email_account from frappe.email.email_body import get_email, get_formatted_html from frappe.utils.verified_command import get_signed_params, verify_request from html2text import html2text -from frappe.utils import get_url, nowdate, encode, now_datetime +from frappe.utils import get_url, nowdate, encode, now_datetime, add_days class BulkLimitCrossedError(frappe.ValidationError): pass @@ -29,7 +29,7 @@ def send(recipients=None, sender=None, subject=None, message=None, reference_doc :param attachments: Attachments to be sent. :param reply_to: Reply to be captured here (default inbox) :param message_id: Used for threading. If a reply is received to this email, Message-Id is sent back as In-Reply-To in received email. - :param send_after: Send this email after the given datetime. + :param send_after: Send this email after the given datetime. If value is in integer, then `send_after` will be the automatically set to no of days from current date. """ if not unsubscribe_method: unsubscribe_method = "/api/method/frappe.email.bulk.unsubscribe" @@ -40,6 +40,9 @@ def send(recipients=None, sender=None, subject=None, message=None, reference_doc if isinstance(recipients, basestring): recipients = recipients.split(",") + if isinstance(send_after, int): + send_after = add_days(nowdate(), send_after) + if not sender or sender == "Administrator": email_account = get_outgoing_email_account() sender = email_account.get("sender") or email_account.email_id @@ -175,7 +178,7 @@ def flush(from_test=False): for i in xrange(500): email = frappe.db.sql("""select * from `tabBulk Email` where - status='Not Sent' and ifnull(send_after, "2000-01-01") > %s + status='Not Sent' and ifnull(send_after, "2000-01-01 00:00:00") < %s order by creation asc limit 1 for update""", now_datetime(), as_dict=1) if email: email = email[0] diff --git a/frappe/tests/test_email.py b/frappe/tests/test_email.py index 6ff09390fb..25f42e743d 100644 --- a/frappe/tests/test_email.py +++ b/frappe/tests/test_email.py @@ -18,12 +18,12 @@ class TestEmail(unittest.TestCase): from frappe.email import sendmail sendmail('test@example.com', subject='Test Mail', msg="Test Content") - def test_bulk(self): + def test_bulk(self, send_after=None): from frappe.email.bulk import send send(recipients = ['test@example.com', 'test1@example.com'], sender="admin@example.com", reference_doctype='User', reference_name='Administrator', - subject='Testing Bulk', message='This is a bulk mail!') + subject='Testing Bulk', message='This is a bulk mail!', send_after=send_after) bulk = frappe.db.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1) self.assertEquals(len(bulk), 2) @@ -32,6 +32,13 @@ class TestEmail(unittest.TestCase): self.assertTrue('Unsubscribe' in bulk[0]['message']) def test_flush(self): + self.test_bulk(send_after = 1) + from frappe.email.bulk import flush + flush(from_test=True) + bulk = frappe.db.sql("""select * from `tabBulk Email` where status='Sent'""", as_dict=1) + self.assertEquals(len(bulk), 0) + + def test_send_after(self): self.test_bulk() from frappe.email.bulk import flush flush(from_test=True) @@ -40,6 +47,7 @@ class TestEmail(unittest.TestCase): self.assertTrue('test@example.com' in [d['recipient'] for d in bulk]) self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk]) + def test_unsubscribe(self): from frappe.email.bulk import unsubscribe, send unsubscribe(doctype="User", name="Administrator", email="test@example.com")