Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. from django.test import TestCase
  2. from django.conf import settings
  3. from django.core.urlresolvers import reverse, NoReverseMatch
  4. import os
  5. import re
  6.  
  7. class TestUnresolveableUrls(TestCase):
  8. """ Look for url tags that wont reverse in templates """
  9.  
  10. def test_broken_urls(self):
  11. """ Look for url tags that wont reverse """
  12. pattern = re.compile(r'{% +url +[\'"]([^\'"]+)[\'"] +%}')
  13. template_urls = self._find_matches_from_templates(pattern)
  14. for filename, url in template_urls:
  15. self._test_url_reverses(url, filename)
  16.  
  17. def _test_url_reverses(self, url, template):
  18. try:
  19. result = reverse(url)
  20. self.assertIsNotNone(result, msg="url '%s' from template %s did not reverse" % (url, template))
  21. except NoReverseMatch, e:
  22. raise Exception("url '%s' from template %s did not reverse" % (url, template))
  23.  
  24. def _find_matches_from_templates(self, pattern):
  25. template_dirs = settings.TEMPLATE_DIRS
  26. template_includes = []
  27. for template_dir in template_dirs:
  28. for subdir, dirs, files in os.walk(template_dir):
  29. for f in files:
  30. template_path = os.path.join(subdir, f)
  31. with open(os.path.join(template_dir, template_path), "r") as ifile:
  32. matches = pattern.findall(ifile.read())
  33. for i in matches:
  34. template_includes.append((template_path, i))
  35.  
  36. return template_includes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement