Ticket #1192: url_resolver3.diff

File url_resolver3.diff, 4.5 KB (added by django@…, 19 years ago)

stripped down version of the patch + some changes to regexp urlresolver that allows easier customizations in custom resolvers. fixed typo

  • django/conf/global_settings.py

     
    132132# Whether to prepend the "www." subdomain to URLs that don't have it.
    133133PREPEND_WWW = False
    134134
     135# class that will resolve the url
     136URL_RESOLVER = 'django.core.urlresolvers.URLResolver'
     137
    135138# List of compiled regular expression objects representing User-Agent strings
    136139# that are not allowed to visit any page, systemwide. Use this for bad
    137140# robots/crawlers. Here are a few examples:
  • django/core/urlresolvers.py

     
    99
    1010from django.http import Http404
    1111from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
    12 import re
     12import re, types
    1313
    1414class Resolver404(Http404):
    1515    pass
     
    5959            raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
    6060
    6161class RegexURLResolver(object):
    62     def __init__(self, regex, urlconf_name):
     62    def __init__(self, regex, url_patterns):
    6363        # regex is a string representing a regular expression.
    64         # urlconf_name is a string representing the module containing urlconfs.
     64        # urlconf_name is a string representing the module containing urlconfs, or
     65        # a tupple with parsed urlpatterns
    6566        self.regex = re.compile(regex)
    66         self.urlconf_name = urlconf_name
     67        if isinstance(url_patterns, types.StringTypes):
     68            self.urlconf_name = url_patterns
     69        else:
     70            self._url_patterns = url_patterns
     71           
    6772
    6873    def resolve(self, path):
    6974        tried = []
    7075        match = self.regex.search(path)
    7176        if match:
    7277            new_path = path[match.end():]
    73             for pattern in self.urlconf_module.urlpatterns:
     78            for pattern in self.url_patterns:
    7479                try:
    7580                    sub_match = pattern.resolve(new_path)
    7681                except Resolver404, e:
     
    8792        except AttributeError:
    8893            try:
    8994                self._urlconf_module = __import__(self.urlconf_name, '', '', [''])
    90             except ValueError, e:
     95            except (ValueError, ImportError), e:
    9196                # Invalid urlconf_name, such as "foo.bar." (note trailing period)
    9297                raise ImproperlyConfigured, "Error while importing URLconf %r: %s" % (self.urlconf_name, e)
    9398            return self._urlconf_module
    9499    urlconf_module = property(_get_urlconf_module)
    95100
    96101    def _get_url_patterns(self):
    97         return self.urlconf_module.urlpatterns
     102        try:
     103            return self._url_patterns
     104        except AttributeError:
     105            self._url_patterns = self.urlconf_module.urlpatterns
     106            return self._url_patterns
    98107    url_patterns = property(_get_url_patterns)
    99108
    100109    def _resolve_special(self, view_type):
     
    110119
    111120    def resolve500(self):
    112121        return self._resolve_special('500')
     122       
     123class URLResolver(RegexURLResolver):
     124    def __init__(self, request, urlconf_name):
     125        RegexURLResolver.__init__(self, r'^/', urlconf_name)
  • django/core/handlers/base.py

     
    5959            if response:
    6060                return response
    6161
    62         resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)
    6362        try:
     63            dot = settings.URL_RESOLVER.rindex('.')
     64        except ValueError:
     65            raise exceptions.ImproperlyConfigured, '%s isn\'t a resolver module' % URL_RESOLVER
     66        ur_module, ur_classname = settings.URL_RESOLVER[:dot], settings.URL_RESOLVER[dot+1:]
     67        try:
     68            mod = __import__(ur_module, '', '', [''])
     69        except ImportError, e:
     70            raise exceptions.ImproperlyConfigured, 'Error importing urlresolver module %s: "%s"' % (ur_module, e)
     71        try:
     72                ur_class = getattr(mod, ur_classname)
     73        except AttributeError:
     74            raise exceptions.ImproperlyConfigured, 'Urlresolver module "%s" does not define a "%s" class' % (mw_module, mw_classname)
     75       
     76        resolver = ur_class(request, settings.ROOT_URLCONF)
     77       
     78        try:
    6479            callback, callback_args, callback_kwargs = resolver.resolve(path)
    6580
    6681            # Apply view middleware
Back to Top