From def1cb19113690a1dac93b42d457ec4279dd61f3 Mon Sep 17 00:00:00 2001 From: Vasil Vangelovski Date: Wed, 18 Jan 2012 00:52:24 +0100 Subject: Reply by email basic use case working with some edge-case handling missing --- askbot/models/reply_by_email.py | 40 ++++++++++++++++++---- .../default/templates/reply_by_email_error.html | 4 +++ askbot/tests/reply_by_email_tests.py | 4 +-- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 askbot/skins/default/templates/reply_by_email_error.html diff --git a/askbot/models/reply_by_email.py b/askbot/models/reply_by_email.py index 0d187b46..8ec43974 100644 --- a/askbot/models/reply_by_email.py +++ b/askbot/models/reply_by_email.py @@ -14,14 +14,14 @@ from askbot.conf import settings as askbot_settings class ReplyAddressManager(BaseQuerySetManager): - def get_unused(self, address): - return self.get(address = address, used_at__isnull = True) + def get_unused(self, address, allowed_from_email): + return self.get(address = address, allowed_from_email = allowed_from_email, used_at__isnull = True) def create_new(self, post, user): reply_address = ReplyAddress(post = post, user = user, allowed_from_email = user.email) while True: reply_address.address = ''.join(random.choice(string.letters + - string.digits) for i in xrange(random.randint(12, 25))) + string.digits) for i in xrange(random.randint(12, 25))).lower() if self.filter(address = reply_address.address).count() == 0: break reply_address.save() @@ -59,7 +59,33 @@ class ReplyAddress(models.Model): #TODO move this function to a more appropriate module def process_reply_email(message, address, host): - reply_address = ReplyAddress.objects.get_unused(address) - separator = _("======= Reply above this line. ====-=-=") - reply_part = message.body().split(separator)[0] - reply_address.create_reply(reply_part) + + error = None + try: + reply_address = ReplyAddress.objects.get_unused(address, message.From) + separator = _("======= Reply above this line. ====-=-=") + parts = message.body().split(separator) + if len(parts) != 2 : + error = _("Your message was malformed. Please make sure to qoute \ + the original notification you received at the end of your reply.") + else: + reply_part = parts[0] + reply_address.create_reply(reply_part.strip()) + except ReplyAddress.DoesNotExist: + error = _("You were replying to an email address\ + unknown to the system or you were replying from a different address from the one where you\ + received the notification.") + if error is not None: + from askbot.utils import mail + from django.template import Context + from askbot.skins.loaders import get_template + + template = get_template('reply_by_email_error.html') + body_text = template.render(Context({'error':error})) + mail.send_mail( + subject_line = "Error posting your reply", + body_text = body_text, + recipient_list = [message.From], + ) + + diff --git a/askbot/skins/default/templates/reply_by_email_error.html b/askbot/skins/default/templates/reply_by_email_error.html new file mode 100644 index 00000000..6860b75f --- /dev/null +++ b/askbot/skins/default/templates/reply_by_email_error.html @@ -0,0 +1,4 @@ +{%trans%} +

The system was unable to process your message successfully, the reason being:

+{%endtrans%} +{{error}} \ No newline at end of file diff --git a/askbot/tests/reply_by_email_tests.py b/askbot/tests/reply_by_email_tests.py index f391827b..4efa440b 100644 --- a/askbot/tests/reply_by_email_tests.py +++ b/askbot/tests/reply_by_email_tests.py @@ -10,7 +10,7 @@ class MockMessage(object): def __init__(self, body, from_email): self._body = body - self.from_email = from_email + self.From= from_email def body(self): return self._body @@ -46,7 +46,7 @@ class EmailProcessingTests(AskbotTestCase): msg = MockMessage("This is a test reply \n%s\nlorem ipsum"%(separator), "user1@domain.com") process_reply_email(msg, addr, '') self.assertEquals(self.answer.comments.count(), 2) - self.assertEquals(self.answer.comments.all().order_by('-timestamp')[0].text, "This is a test reply") + self.assertEquals(self.answer.comments.all().order_by('-pk')[0].text, "This is a test reply") -- cgit v1.2.3-1-g7c22