From 364633a40e2bfb820e8ddb48252eb614c2985753 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Fri, 28 Sep 2012 00:29:02 -0300 Subject: hopefully fixed the image urls in the email alerts --- askbot/mail/__init__.py | 3 +++ askbot/templates/email/macros.html | 2 +- askbot/tests/email_alert_tests.py | 35 ++++++++++++++++++++++++++++++++++- askbot/tests/utils_tests.py | 24 ++++++++++++++++++++---- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/askbot/mail/__init__.py b/askbot/mail/__init__.py index 2d314dbc..74aa27e9 100644 --- a/askbot/mail/__init__.py +++ b/askbot/mail/__init__.py @@ -17,6 +17,7 @@ from askbot import const from askbot.conf import settings as askbot_settings from askbot.utils import url_utils from askbot.utils.file_utils import store_file +from askbot.utils.html import absolutize_urls from bs4 import BeautifulSoup #todo: maybe send_mail functions belong to models @@ -116,6 +117,7 @@ def send_mail( if raise_on_failure is True, exceptions.EmailNotSent is raised """ + body_text = absolutize_urls(body_text) try: assert(subject_line is not None) subject_line = prefix_the_subject_line(subject_line) @@ -143,6 +145,7 @@ def mail_moderators( ): """sends email to forum moderators and admins """ + body_text = absolutize_urls(body_text) from django.db.models import Q from askbot.models import User recipient_list = User.objects.filter( diff --git a/askbot/templates/email/macros.html b/askbot/templates/email/macros.html index f1b06fc8..125705e2 100644 --- a/askbot/templates/email/macros.html +++ b/askbot/templates/email/macros.html @@ -70,7 +70,7 @@ {% endif %}

{% endif %} - {{ post.html|absolutize_urls }} + {{ post.html }} {{ end_quote(quote_level) }} {% endspaceless %} {% endmacro %} diff --git a/askbot/tests/email_alert_tests.py b/askbot/tests/email_alert_tests.py index 2618cf9a..f4ae052b 100644 --- a/askbot/tests/email_alert_tests.py +++ b/askbot/tests/email_alert_tests.py @@ -1,6 +1,7 @@ +import bs4 +import copy import datetime import functools -import copy import time from django.conf import settings as django_settings from django.core import management @@ -1035,6 +1036,38 @@ class PostApprovalTests(utils.AskbotTestCase): #self.assertEquals(outbox[1].recipients(), [u1.email,])#approval +class AbsolutizeUrlsInEmailsTests(utils.AskbotTestCase): + @with_settings(MIN_REP_TO_TRIGGER_EMAIL=1, APP_URL='http://example.com/') + def test_urls_are_absolute(self): + u1 = self.create_user('u1') + max_email = models.EmailFeedSetting.MAX_EMAIL_SCHEDULE + u2 = self.create_user('u2', notification_schedule=max_email) + text = 'home' + \ + 'an image' + question = self.post_question(user=u1, body_text=text) + outbox = django.core.mail.outbox + html_message = outbox[0].alternatives[0][0] + content_type = outbox[0].alternatives[0][1] + self.assertEqual(content_type, 'text/html') + + soup = bs4.BeautifulSoup(html_message) + links = soup.find_all('a') + url_bits = {} + for link in links: + url_bits[link.attrs['href'][:4]] = 1 + + self.assertEqual(len(url_bits.keys()), 1) + self.assertEqual(url_bits.keys()[0], 'http') + + images = soup.find_all('img') + url_bits = {} + for img in images: + url_bits[img.attrs['src'][:4]] = 1 + + self.assertEqual(len(url_bits.keys()), 1) + self.assertEqual(url_bits.keys()[0], 'http') + + class MailMessagesTests(utils.AskbotTestCase): def test_ask_for_signature(self): from askbot.mail import messages diff --git a/askbot/tests/utils_tests.py b/askbot/tests/utils_tests.py index bc3bb0bb..c8526ad1 100644 --- a/askbot/tests/utils_tests.py +++ b/askbot/tests/utils_tests.py @@ -21,9 +21,9 @@ class UrlUtilsTests(TestCase): class HTMLUtilsTests(TestCase): """tests for :mod:`askbot.utils.html` module""" - + @with_settings(APP_URL='http://example.com') - def test_absolutize_image_urls(self): + def test_absolutize_urls(self): text = """ """ #jinja register.filter decorator works in a weird way self.assertEqual( @@ -31,11 +31,27 @@ class HTMLUtilsTests(TestCase): ' ' ) - @with_settings(APP_URL='http://example.com') - def test_absolutize_anchor_urls(self): text = """link link""" #jinja register.filter decorator works in a weird way self.assertEqual( absolutize_urls(text), 'link link' ) + + text = '' + self.assertEqual( + absolutize_urls(text), + '' + ) + + text = 'ohaouhaosthoanstoahuaou
Evgeny4 gravatar image' + self.assertEqual( + absolutize_urls(text), + 'ohaouhaosthoanstoahuaou
Evgeny4 gravatar image' + ) + + text = 'and some text
aouaosutoaehut' + self.assertEqual( + absolutize_urls(text), + 'and some text
aouaosutoaehut' + ) -- cgit v1.2.3-1-g7c22