import re
import os

from django.test import TestCase

from .utils import get_template_dirs

re_url = re.compile(r'\shref="(?P<url>(?!https?:|mailto:|\?|{|#)[^"]*)"')

class TestTemplates(TestCase):
    def assertValidURLs(self, filename):
        with open(filename) as f:
            urls = [m.group('url') for m in re_url.finditer(f.read())]

        self.failIf(urls, "%s contains hardcoded URLs: %r" % (
            filename,
            urls,
        ))

    idx = 0
    for x in get_template_dirs():
        for root, _, filenames in os.walk(x):
            for y in filenames:
                def wrapper(self, filename=os.path.join(root, y)):
                    self.assertValidURLs(filename)
                idx += 1
                locals()['test_template_idx_%04d' % idx] = wrapper

import re
import os

from django.test import TestCase
from django.utils.text import slugify

from .utils import get_template_dirs

re_url = re.compile(r'\shref="(?P<url>(?!https?:|mailto:|\?|{|#)[^"]*)"')

class TestTemplatesMeta(type):
    def __new__(cls, name, bases, attrs):
        def generate(template):
            def fn(self):
                self.assertValidURLs(template)
            return fn

        for x in get_template_dirs():
            for root, _, templates in os.walk(x):
                for y in templates:
                    template = os.path.join(root, y)

                    attrs['test_%s' % slugify(template)] = generate(template)

        return super(TestTemplatesMeta, cls).__new__(cls, name, bases, attrs)

class TestTemplates(TestCase):
    __metaclass__ = TestTemplatesMeta

    def assertValidURLs(self, template):
        with open(template) as f:
            urls = [m.group('url') for m in re_url.finditer(f.read())]

        self.failIf(urls, "%s contains hardcoded URLs: %r" % (
            template,
            urls,
        ))

Use a metaclass instead of dirty dict()-mangling.
Use a metaclass instead of dirty dict()-mangling.

  import re
  import os
  
  from django.test import TestCase
+ from django.utils.text import slugify
  
  from .utils import get_template_dirs
  
  re_url = re.compile(r'\shref="(?P<url>(?!https?:|mailto:|\?|{|#)[^"]*)"')
  
+ class TestTemplatesMeta(type):
+     def __new__(cls, name, bases, attrs):
+         def generate(template):
+             def fn(self):
+                 self.assertValidURLs(template)
+             return fn
+ 
+         for x in get_template_dirs():
+             for root, _, templates in os.walk(x):
+                 for y in templates:
+                     template = os.path.join(root, y)
+ 
+                     attrs['test_%s' % slugify(template)] = generate(template)
+ 
+         return super(TestTemplatesMeta, cls).__new__(cls, name, bases, attrs)
+ 
  class TestTemplates(TestCase):
+     __metaclass__ = TestTemplatesMeta
+ 
-     def assertValidURLs(self, filename):
+     def assertValidURLs(self, template):
-         with open(filename) as f:
+         with open(template) as f:
              urls = [m.group('url') for m in re_url.finditer(f.read())]
  
          self.failIf(urls, "%s contains hardcoded URLs: %r" % (
-             filename,
+             template,
              urls,
          ))
  
-     idx = 0
-     for x in get_template_dirs():
-         for root, _, filenames in os.walk(x):
-             for y in filenames:
-                 def wrapper(self, filename=os.path.join(root, y)):
-                     self.assertValidURLs(filename)
-                 idx += 1
-                 locals()['test_template_idx_%04d' % idx] = wrapper
- 
Use a metaclass instead of dirty dict()-mangling.
## Code Before:
import re
import os

from django.test import TestCase

from .utils import get_template_dirs

re_url = re.compile(r'\shref="(?P<url>(?!https?:|mailto:|\?|{|#)[^"]*)"')

class TestTemplates(TestCase):
    def assertValidURLs(self, filename):
        with open(filename) as f:
            urls = [m.group('url') for m in re_url.finditer(f.read())]

        self.failIf(urls, "%s contains hardcoded URLs: %r" % (
            filename,
            urls,
        ))

    idx = 0
    for x in get_template_dirs():
        for root, _, filenames in os.walk(x):
            for y in filenames:
                def wrapper(self, filename=os.path.join(root, y)):
                    self.assertValidURLs(filename)
                idx += 1
                locals()['test_template_idx_%04d' % idx] = wrapper

## Instruction:
Use a metaclass instead of dirty dict()-mangling.
## Code After:
import re
import os

from django.test import TestCase
from django.utils.text import slugify

from .utils import get_template_dirs

re_url = re.compile(r'\shref="(?P<url>(?!https?:|mailto:|\?|{|#)[^"]*)"')

class TestTemplatesMeta(type):
    def __new__(cls, name, bases, attrs):
        def generate(template):
            def fn(self):
                self.assertValidURLs(template)
            return fn

        for x in get_template_dirs():
            for root, _, templates in os.walk(x):
                for y in templates:
                    template = os.path.join(root, y)

                    attrs['test_%s' % slugify(template)] = generate(template)

        return super(TestTemplatesMeta, cls).__new__(cls, name, bases, attrs)

class TestTemplates(TestCase):
    __metaclass__ = TestTemplatesMeta

    def assertValidURLs(self, template):
        with open(template) as f:
            urls = [m.group('url') for m in re_url.finditer(f.read())]

        self.failIf(urls, "%s contains hardcoded URLs: %r" % (
            template,
            urls,
        ))
