Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.test import TestCase
- from django.conf import settings
- from django.core.urlresolvers import reverse, NoReverseMatch
- import os
- import re
- class TestUnresolveableUrls(TestCase):
- """ Look for url tags that wont reverse in templates """
- def test_broken_urls(self):
- """ Look for url tags that wont reverse """
- pattern = re.compile(r'{% +url +[\'"]([^\'"]+)[\'"] +%}')
- template_urls = self._find_matches_from_templates(pattern)
- for filename, url in template_urls:
- self._test_url_reverses(url, filename)
- def _test_url_reverses(self, url, template):
- try:
- result = reverse(url)
- self.assertIsNotNone(result, msg="url '%s' from template %s did not reverse" % (url, template))
- except NoReverseMatch, e:
- raise Exception("url '%s' from template %s did not reverse" % (url, template))
- def _find_matches_from_templates(self, pattern):
- template_dirs = settings.TEMPLATE_DIRS
- template_includes = []
- for template_dir in template_dirs:
- for subdir, dirs, files in os.walk(template_dir):
- for f in files:
- template_path = os.path.join(subdir, f)
- with open(os.path.join(template_dir, template_path), "r") as ifile:
- matches = pattern.findall(ifile.read())
- for i in matches:
- template_includes.append((template_path, i))
- return template_includes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement