From 1e47fb23634dbdf406a3e8893120793b7e065da5 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Thu, 27 Sep 2012 15:09:37 -0400 Subject: refactored the absolutize_urls function and the with_settings decorator for the test cases --- askbot/models/post.py | 2 +- askbot/templatetags/extra_filters_jinja.py | 18 ++---------------- askbot/tests/__init__.py | 1 - askbot/tests/db_api_tests.py | 5 +---- askbot/tests/email_alert_tests.py | 2 +- askbot/tests/page_load_tests.py | 12 ++++++------ askbot/tests/templatefilter_tests.py | 23 ----------------------- askbot/tests/utils.py | 2 +- askbot/tests/utils_tests.py | 24 ++++++++++++++++++++++++ askbot/utils/html.py | 18 ++++++++++++++++++ 10 files changed, 54 insertions(+), 53 deletions(-) delete mode 100644 askbot/tests/templatefilter_tests.py diff --git a/askbot/models/post.py b/askbot/models/post.py index 57847088..c65caeea 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -834,7 +834,7 @@ class Post(models.Model): return filtered_candidates def format_for_email( - self, quote_level = 0, is_leaf_post = False, format = None + self, quote_level=0, is_leaf_post=False, format=None ): """format post for the output in email, if quote_level > 0, the post will be indented that number of times diff --git a/askbot/templatetags/extra_filters_jinja.py b/askbot/templatetags/extra_filters_jinja.py index ba13166b..146de6d1 100644 --- a/askbot/templatetags/extra_filters_jinja.py +++ b/askbot/templatetags/extra_filters_jinja.py @@ -14,6 +14,7 @@ from askbot import exceptions as askbot_exceptions from askbot.conf import settings as askbot_settings from django.conf import settings as django_settings from askbot.skins import utils as skin_utils +from askbot.utils.html import absolutize_urls from askbot.utils import functions from askbot.utils import url_utils from askbot.utils.slug import slugify @@ -24,22 +25,7 @@ from django_countries import settings as countries_settings register = coffin_template.Library() -@register.filter -def absolutize_urls(text): - #temporal fix for bad regex with wysiwyg editor - url_re1 = re.compile(r'(?P[/\..][^"]+)"', re.I) - url_re2 = re.compile(r"(?P[/\..][^']+)'", re.I) - url_re3 = re.compile(r'(?P/[^"]+)"', re.I) - url_re4 = re.compile(r"(?P/[^']+)'", re.I) - img_replacement = '\g"%s/\g" style="max-width:500px;"' % askbot_settings.APP_URL - replacement = '\g"%s\g"' % askbot_settings.APP_URL - text = url_re1.sub(img_replacement, text) - text = url_re2.sub(img_replacement, text) - text = url_re3.sub(replacement, text) - #temporal fix for bad regex with wysiwyg editor - return url_re4.sub(replacement, text).replace('%s//' % askbot_settings.APP_URL, - '%s/' % askbot_settings.APP_URL) - +absolutize_urls = register.filter(absolutize_urls) TIMEZONE_STR = pytz.timezone( django_settings.TIME_ZONE diff --git a/askbot/tests/__init__.py b/askbot/tests/__init__.py index 8a018ea1..fcef288b 100644 --- a/askbot/tests/__init__.py +++ b/askbot/tests/__init__.py @@ -10,7 +10,6 @@ from askbot.tests.management_command_tests import * from askbot.tests.search_state_tests import * from askbot.tests.form_tests import * from askbot.tests.follow_tests import * -from askbot.tests.templatefilter_tests import * from askbot.tests.markup_test import * from askbot.tests.post_model_tests import * from askbot.tests.thread_model_tests import * diff --git a/askbot/tests/db_api_tests.py b/askbot/tests/db_api_tests.py index ec4210e8..65b0a950 100644 --- a/askbot/tests/db_api_tests.py +++ b/askbot/tests/db_api_tests.py @@ -166,10 +166,7 @@ class DBApiTests(AskbotTestCase): count = models.Tag.objects.filter(name='one-tag').count() self.assertEquals(count, 0) - @with_settings({ - 'MAX_TAG_LENGTH': 200, - 'MAX_TAGS_PER_POST': 50 - }) + @with_settings(MAX_TAG_LENGTH=200, MAX_TAGS_PER_POST=50) def test_retag_tags_too_long_raises(self): tags = "aoaoesuouooeueooeuoaeuoeou aostoeuoaethoeastn oasoeoa nuhoasut oaeeots aoshootuheotuoehao asaoetoeatuoasu o aoeuethut aoaoe uou uoetu uouuou ao aouosutoeh" question = self.post_question(user=self.user) diff --git a/askbot/tests/email_alert_tests.py b/askbot/tests/email_alert_tests.py index f5b5e43b..2618cf9a 100644 --- a/askbot/tests/email_alert_tests.py +++ b/askbot/tests/email_alert_tests.py @@ -965,7 +965,7 @@ class EmailAlertTestsWithGroupsEnabled(utils.AskbotTestCase): def tearDown(self): askbot_settings.update('GROUPS_ENABLED', self.backup) - @with_settings({'MIN_REP_TO_TRIGGER_EMAIL': 1}) + @with_settings(MIN_REP_TO_TRIGGER_EMAIL=1) def test_notification_for_global_group_works(self): sender = self.create_user('sender') recipient = self.create_user( diff --git a/askbot/tests/page_load_tests.py b/askbot/tests/page_load_tests.py index 293cb78d..3805d012 100644 --- a/askbot/tests/page_load_tests.py +++ b/askbot/tests/page_load_tests.py @@ -152,14 +152,14 @@ class PageLoadTestCase(AskbotTestCase): def test_ask_page_allowed_anonymous(self): self.proto_test_ask_page(True, 200) - @with_settings({'GROUPS_ENABLED': False}) + @with_settings(GROUPS_ENABLED=False) def test_api_get_questions_groups_disabled(self): data = {'query': 'Question'} response = self.client.get(reverse('api_get_questions'), data) data = simplejson.loads(response.content) self.assertTrue(len(data) > 1) - @with_settings({'GROUPS_ENABLED': True}) + @with_settings(GROUPS_ENABLED=True) def test_api_get_questions_groups_enabled(self): group = models.Group(name='secret group', openness=models.Group.OPEN) @@ -443,7 +443,7 @@ class PageLoadTestCase(AskbotTestCase): @skipIf('askbot.middleware.forum_mode.ForumModeMiddleware' \ not in settings.MIDDLEWARE_CLASSES, 'no ForumModeMiddleware set') - @with_settings({'ASKBOT_CLOSED_FORUM_MODE': True}) + @with_settings(ASKBOT_CLOSED_FORUM_MODE=True) def test_non_user_urls_in_closed_forum_mode(self): self.proto_test_non_user_urls(status_code=302) @@ -514,7 +514,7 @@ class PageLoadTestCase(AskbotTestCase): @skipIf('askbot.middleware.forum_mode.ForumModeMiddleware' \ not in settings.MIDDLEWARE_CLASSES, 'no ForumModeMiddleware set') - @with_settings({'ASKBOT_CLOSED_FORUM_MODE': True}) + @with_settings(ASKBOT_CLOSED_FORUM_MODE=True) def test_user_urls_in_closed_forum_mode(self): self.proto_test_user_urls(status_code=302) @@ -550,11 +550,11 @@ class PageLoadTestCase(AskbotTestCase): template='user_inbox/responses_and_flags.html', ) - @with_settings({'GROUPS_ENABLED': True}) + @with_settings(GROUPS_ENABLED=True) def test_user_page_with_groups_enabled(self): self.try_url('users', status_code=302) - @with_settings({'GROUPS_ENABLED': False}) + @with_settings(GROUPS_ENABLED=False) def test_user_page_with_groups_disabled(self): self.try_url('users', status_code=200) diff --git a/askbot/tests/templatefilter_tests.py b/askbot/tests/templatefilter_tests.py deleted file mode 100644 index 3902aad4..00000000 --- a/askbot/tests/templatefilter_tests.py +++ /dev/null @@ -1,23 +0,0 @@ -from unittest import TestCase -from askbot.templatetags import extra_filters_jinja as filters -from askbot.conf import settings as askbot_settings - -class AbsolutizeUrlsTests(TestCase): - def setUp(self): - askbot_settings.update('APP_URL', 'http://example.com') - def test_absolutize_image_urls(self): - text = """ """ - #jinja register.filter decorator works in a weird way - output = filters.absolutize_urls[0](text) - self.assertEqual( - output, - ' ' - ) - def test_absolutize_anchor_urls(self): - text = """link link""" - #jinja register.filter decorator works in a weird way - output = filters.absolutize_urls[0](text) - self.assertEqual( - output, - 'link link' - ) diff --git a/askbot/tests/utils.py b/askbot/tests/utils.py index 1cd174c1..ee3cd37e 100644 --- a/askbot/tests/utils.py +++ b/askbot/tests/utils.py @@ -4,7 +4,7 @@ from django.test import TestCase from functools import wraps from askbot import models -def with_settings(settings_dict): +def with_settings(**settings_dict): """a decorator that will run function with settings then apply previous settings and return the result of the function. diff --git a/askbot/tests/utils_tests.py b/askbot/tests/utils_tests.py index 7f252b69..bc3bb0bb 100644 --- a/askbot/tests/utils_tests.py +++ b/askbot/tests/utils_tests.py @@ -1,5 +1,8 @@ from django.test import TestCase +from askbot.tests.utils import with_settings from askbot.utils.url_utils import urls_equal +from askbot.utils.html import absolutize_urls +from askbot.conf import settings as askbot_settings class UrlUtilsTests(TestCase): @@ -15,3 +18,24 @@ class UrlUtilsTests(TestCase): self.assertTrue(e('http://cnn.com/path', 'http://cnn.com/path/', True)) self.assertFalse(e('http://cnn.com/path', 'http://cnn.com/path/')) + +class HTMLUtilsTests(TestCase): + """tests for :mod:`askbot.utils.html` module""" + + @with_settings(APP_URL='http://example.com') + def test_absolutize_image_urls(self): + text = """ """ + #jinja register.filter decorator works in a weird way + self.assertEqual( + absolutize_urls(text), + ' ' + ) + + @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' + ) diff --git a/askbot/utils/html.py b/askbot/utils/html.py index 44e3f1df..49eddee2 100644 --- a/askbot/utils/html.py +++ b/askbot/utils/html.py @@ -6,6 +6,7 @@ import htmlentitydefs from urlparse import urlparse from django.core.urlresolvers import reverse from django.utils.html import escape +from askbot.conf import settings as askbot_settings class HTMLSanitizerMixin(sanitizer.HTMLSanitizerMixin): acceptable_elements = ('a', 'abbr', 'acronym', 'address', 'b', 'big', @@ -43,6 +44,23 @@ class HTMLSanitizer(tokenizer.HTMLTokenizer, HTMLSanitizerMixin): if token: yield token +def absolutize_urls(html): + """turns relative urls in and tags to absolute, + starting with the ``askbot_settings.APP_URL``""" + #temporal fix for bad regex with wysiwyg editor + url_re1 = re.compile(r'(?P/[^"]+)"', re.I) + url_re2 = re.compile(r"(?P/[^']+)'", re.I) + url_re3 = re.compile(r'(?P/[^"]+)"', re.I) + url_re4 = re.compile(r"(?P/[^']+)'", re.I) + img_replacement = '\g"%s/\g" style="max-width:500px;"' % askbot_settings.APP_URL + replacement = '\g"%s\g"' % askbot_settings.APP_URL + html = url_re1.sub(img_replacement, html) + html= url_re2.sub(img_replacement, html) + html = url_re3.sub(replacement, html) + #temporal fix for bad regex with wysiwyg editor + return url_re4.sub(replacement, html).replace('%s//' % askbot_settings.APP_URL, + '%s/' % askbot_settings.APP_URL) + def sanitize_html(html): """Sanitizes an HTML fragment.""" p = html5lib.HTMLParser(tokenizer=HTMLSanitizer, -- cgit v1.2.3-1-g7c22