From 1b3cd7305eef042a14eecd3e079792dd1dabea2a Mon Sep 17 00:00:00 2001 From: Fabrizio Sestito Date: Fri, 16 Dec 2011 11:46:11 +0100 Subject: [PATCH] added strip signature, strip_quotes refactored in strip_mail --- threaded_messages/listeners.py | 4 ++-- threaded_messages/tests.py | 12 +++++++++--- threaded_messages/utils.py | 21 ++++++++++++++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/threaded_messages/listeners.py b/threaded_messages/listeners.py index 3e4c47c..ef6dbd6 100644 --- a/threaded_messages/listeners.py +++ b/threaded_messages/listeners.py @@ -9,7 +9,7 @@ email_received = None def signal_received_email(sender, sma, app_id, html, text, from_field, **kwargs): - from utils import reply_to_thread, strip_quotes # circular dependency fix + from utils import reply_to_thread, strip_mail # circular dependency fix logger.debug("Sendgrid signal receive: %s, %s, %s, %s, %s, %s"%(sender, sma, app_id, html, repr(text), from_field) ) if app_id == sendgrid_settings.THREADED_MESSAGES_ID: @@ -23,7 +23,7 @@ def signal_received_email(sender, sma, app_id, html, text, from_field, **kwargs) if body: body = strip_tags(body) - body = strip_quotes(body) + body = strip_mail(body) thread = sma.content_object reply_to_thread(thread, sma.user, body) diff --git a/threaded_messages/tests.py b/threaded_messages/tests.py index d9a7298..737475f 100644 --- a/threaded_messages/tests.py +++ b/threaded_messages/tests.py @@ -2,7 +2,7 @@ from django.test import TestCase from django.contrib.auth.models import User from models import Message -from utils import strip_quotes +from utils import strip_mail from django.utils.html import strip_tags class UtilsTest(TestCase): @@ -35,11 +35,17 @@ def test_strip_quotes(self): nyan nyan nyan nyan nyan """ - self.assertEquals(body_stripped.strip(), strip_quotes(body).strip()) + self.assertEquals(body_stripped.strip(), strip_mail(body).strip()) def test_single_line_quotes(self): body = 'asfasf\n\nOn Thu, Dec 15, 2011 at 12:42 PM, Fabrizio S. wrote:\n\n> [image: Gidsy] New message\n> Hi Fabrizio, Andrew M. sent you a message\n>\n> *blabla*\n> gasg\n>\n> View and reply\n>\n> Sincerely,\n> the *Gidsy team*\n>\n> This email was intended for fabrizio@gidsy.com. If you do not want to\n> receive emails like this from staging.gidsy.comanymore, then please change your Email\n> notification settings .\n>\n> Copyright \ufffd 2011 Gidsy.com, All rights reserved.\n>\n' body_stripped = "asfasf" - self.assertEquals(body_stripped.strip(), strip_quotes(body).strip()) + self.assertEquals(body_stripped.strip(), strip_mail(body).strip()) + + def test_strip_signature(self): + body = 'signature test\n\nOn Fri, Dec 16, 2011 at 11:06 AM, Fabrizio Sestito wrote:\n\n> test\n>\n> asd\n>\n>\n> On Fri, Dec 16, 2011 at 11:05 AM, Fabrizio Sestito wrote:\n>\n>> hey\n>>\n>>\n>> On Thu, Dec 15, 2011 at 4:08 PM, Fabrizio S. wrote:\n>>\n>>> [image: Gidsy] New message\n>>> Hi Fabrizio, Andrew M. sent you a message\n>>>\n>>> *sdfsdf*\n>>> sadasdasdasd\n>>>\n>>> View and reply\n>>>\n>>> Sincerely,\n>>> the *Gidsy team*\n>>>\n>>> This email was intended for fabrizio@gidsy.com. If you do not want to\n>>> receive emails like this from staging.gidsy.comanymore, then please change your Email\n>>> notification settings .\n>>>\n>>> Copyright \ufffd 2011 Gidsy.com, All rights reserved.\n>>>\n>>\n>>\n>\n\n\n-- \nFabrizio Sestito\n' + + body_stripped = "signature test" + self.assertEquals(body_stripped.strip(), strip_mail(body).strip()) diff --git a/threaded_messages/utils.py b/threaded_messages/utils.py index 05f5f4e..83c099f 100644 --- a/threaded_messages/utils.py +++ b/threaded_messages/utils.py @@ -83,13 +83,27 @@ def reply_to_thread(thread,sender, body): "message": new_message}, sender=sender) return (thread, new_message) + -def strip_quotes(body): - - custom_line_no = None +def get_lines(body): body = body.replace('\r', ' ') lines = [x.strip() for x in body.splitlines(True)] + return lines + + +def strip_mail(body): + + custom_line_no = None + + lines = get_lines(body) + + # strip signature + for l in reversed(lines): + lines.remove(l) + if l.strip().startswith('>'): + break + # strip quotes for i,l in enumerate(lines): if l.lstrip().startswith('>'): if not custom_line_no: @@ -106,4 +120,5 @@ def strip_quotes(body): # strip last empty string in the list if it exists if not stripped_lines[-1]: stripped_lines.pop() + # stripped message return ('\n').join(stripped_lines)