Ticket #4824: duplicate_urlnames.diff

File duplicate_urlnames.diff, 1.7 KB (added by drdee, 14 years ago)

New patch to check for duplicate urlnames

  • trunk_patched/django/conf/urls/defaults.py

     
     1import sys
     2fromd django.conf import settings
    13from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
    24from django.core.exceptions import ImproperlyConfigured
    35
     
    68handler404 = 'django.views.defaults.page_not_found'
    79handler500 = 'django.views.defaults.server_error'
    810
     11def _detect_duplicate_urlnames():
     12    '''
     13    This function loads for each installed app the associated url file, it assumes that this this file is in the root
     14    of a project. Then it constructs a list of all urlnames and if it finds a duplicate urlname will raise an ImproperlyConfigured error
     15    '''
     16    urlnames = []
     17    for app in settings.INSTALLED_APPS:
     18        urlconf = app + '.urls'
     19        try:
     20            # Not all apps will necesarrily have a urls file, hence the try / except clause
     21            __import__(urlconf)
     22            urls = []
     23            urls.append(sys.modules[urlconf])
     24            for url in urls:
     25                for pattern in url.urlpatterns:
     26                    name = getattr(pattern, 'name')
     27                    if name == None:
     28                        continue
     29                    elif name not in urlnames:
     30                        urlnames.append(name)
     31                    else:
     32                        raise ImproperlyConfigured('Duplicate named URL specified ("%s")' % name)
     33        except ImportError:
     34            pass
     35
    936def include(arg, namespace=None, app_name=None):
    1037    if isinstance(arg, tuple):
    1138        # callable returning a namespace hint
Back to Top