Skip to content

Commit

Permalink
added strip signature, strip_quotes refactored in strip_mail
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio Sestito committed Dec 16, 2011
1 parent 88270a2 commit 1b3cd73
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 2 additions & 2 deletions threaded_messages/listeners.py
Expand Up @@ -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:
Expand All @@ -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)

Expand Down
12 changes: 9 additions & 3 deletions threaded_messages/tests.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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. <messaging@email.gidsy.com>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<http://email.gidsy.com/wf/click?c=e36x8iH5CyW6UFPc7U%2FiBSpwHwOcqQc55u6Od0IAvnJWLQwR0RdOslgfJYtFkOT0&rp=7%2Bq%2FuBUXPhfnWd079jPZDJw1s3xtQcNITJcDWjO98HB8tJ6%2BYeP23y9SaOFiXvnpboQhDnEJRnrEZfRP9WnHQiL7q9Y0Plign2S9mx7i8%2Bk%3D&u=icfx5E9JS66UPX7QM9UvGw%2Fh0>\n>\n> Sincerely,\n> the *Gidsy team*<http://email.gidsy.com/wf/click?c=nun%2FbaehJTxhIK1KvYwhU5Tg16XMq0b2DKd6IxvO%2F%2Bw%3D&rp=7%2Bq%2FuBUXPhfnWd079jPZDJw1s3xtQcNITJcDWjO98HB8tJ6%2BYeP23y9SaOFiXvnpboQhDnEJRnrEZfRP9WnHQiL7q9Y0Plign2S9mx7i8%2Bk%3D&u=icfx5E9JS66UPX7QM9UvGw%2Fh1>\n>\n> This email was intended for fabrizio@gidsy.com. If you do not want to\n> receive emails like this from staging.gidsy.com<http://email.gidsy.com/wf/click?c=e36x8iH5CyW6UFPc7U%2FiBY72qxV4NIiQfC%2BfF%2BpSEec%3D&rp=7%2Bq%2FuBUXPhfnWd079jPZDJw1s3xtQcNITJcDWjO98HB8tJ6%2BYeP23y9SaOFiXvnpboQhDnEJRnrEZfRP9WnHQiL7q9Y0Plign2S9mx7i8%2Bk%3D&u=icfx5E9JS66UPX7QM9UvGw%2Fh2>anymore, then please change your Email\n> notification settings <http://notice-email-setting/>.\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 <fabrizio@gidsy.com>wrote:\n\n> test\n>\n> asd\n>\n>\n> On Fri, Dec 16, 2011 at 11:05 AM, Fabrizio Sestito <fabrizio@gidsy.com>wrote:\n>\n>> hey\n>>\n>>\n>> On Thu, Dec 15, 2011 at 4:08 PM, Fabrizio S. <messaging@email.gidsy.com>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<http://email.gidsy.com/wf/click?c=e36x8iH5CyW6UFPc7U%2FiBSpwHwOcqQc55u6Od0IAvnLUol8UpZle1eFZgQF40o%2FA&rp=7%2Bq%2FuBUXPhfnWd079jPZDJw1s3xtQcNITJcDWjO98HC6dm1r92yU0SpAJEPbb%2B6TYHFx5ZDq5B8IwoyftFTyY2YZCtQ%2F66rRPRshi2lf8V8%3D&u=wYte5RSXQ3KUXaXN31g4LQ%2Fh0>\n>>>\n>>> Sincerely,\n>>> the *Gidsy team*<http://email.gidsy.com/wf/click?c=nun%2FbaehJTxhIK1KvYwhU5Tg16XMq0b2DKd6IxvO%2F%2Bw%3D&rp=7%2Bq%2FuBUXPhfnWd079jPZDJw1s3xtQcNITJcDWjO98HC6dm1r92yU0SpAJEPbb%2B6TYHFx5ZDq5B8IwoyftFTyY2YZCtQ%2F66rRPRshi2lf8V8%3D&u=wYte5RSXQ3KUXaXN31g4LQ%2Fh1>\n>>>\n>>> This email was intended for fabrizio@gidsy.com. If you do not want to\n>>> receive emails like this from staging.gidsy.com<http://email.gidsy.com/wf/click?c=e36x8iH5CyW6UFPc7U%2FiBY72qxV4NIiQfC%2BfF%2BpSEec%3D&rp=7%2Bq%2FuBUXPhfnWd079jPZDJw1s3xtQcNITJcDWjO98HC6dm1r92yU0SpAJEPbb%2B6TYHFx5ZDq5B8IwoyftFTyY2YZCtQ%2F66rRPRshi2lf8V8%3D&u=wYte5RSXQ3KUXaXN31g4LQ%2Fh2>anymore, then please change your Email\n>>> notification settings <http://notice-email-setting/>.\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())
21 changes: 18 additions & 3 deletions threaded_messages/utils.py
Expand Up @@ -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:
Expand All @@ -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)

0 comments on commit 1b3cd73

Please sign in to comment.