Ticket #7537: dj-urlresolver-patch2.patch

File dj-urlresolver-patch2.patch, 2.9 KB (added by Kenneth Arnold, 16 years ago)

Patch 2: extract base functionality

  • urlresolvers.

    old new  
    198198    def reverse_helper(self, *args, **kwargs):
    199199        return reverse_helper(self.regex, *args, **kwargs)
    200200
    201 class RegexURLResolver(object):
     201class BaseRegexURLResolver(object):
    202202    def __init__(self, regex, urlconf_name, default_kwargs=None):
    203         # regex is a string representing a regular expression.
    204         # urlconf_name is a string representing the module containing urlconfs.
    205203        self.regex = re.compile(regex, re.UNICODE)
    206         self.urlconf_name = urlconf_name
    207         self.callback = None
     204        self.callback = None # FIXME: why does this class have `callback`?
    208205        self.default_kwargs = default_kwargs or {}
    209206
    210207    def __repr__(self):
    211         return '<%s %s %s>' % (self.__class__.__name__, self.urlconf_name, self.regex.pattern)
     208        return '<%s %s>' % (self.__class__.__name__, self.regex.pattern)
    212209
    213210    def _get_reverse_dict(self):
    214211        if not hasattr(self, '_reverse_dict'):
     
    245242                    tried.append(pattern.regex.pattern)
    246243            raise Resolver404, {'tried': tried, 'path': new_path}
    247244
     245    def reverse(self, lookup_view, *args, **kwargs):
     246        try:
     247            lookup_view = get_callable(lookup_view, True)
     248        except (ImportError, AttributeError):
     249            raise NoReverseMatch
     250        if lookup_view in self.reverse_dict:
     251            return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]])
     252        raise NoReverseMatch
     253
     254
     255class RegexURLResolver(BaseRegexURLResolver):
     256    def __init__(self, regex, urlconf_name, default_kwargs=None):
     257        # regex is a string representing a regular expression.
     258        # urlconf_name is a string representing the module containing urlconfs.
     259        super(RegexURLResolver, self).__init__(regex, default_kwargs)
     260        self.urlconf_name = urlconf_name
     261
     262    def __repr__(self):
     263        return '<%s %s %s>' % (self.__class__.__name__, self.urlconf_name, self.regex.pattern)
     264
    248265    def _get_urlconf_module(self):
    249266        try:
    250267            return self._urlconf_module
     
    275292    def resolve500(self):
    276293        return self._resolve_special('500')
    277294
    278     def reverse(self, lookup_view, *args, **kwargs):
    279         try:
    280             lookup_view = get_callable(lookup_view, True)
    281         except (ImportError, AttributeError):
    282             raise NoReverseMatch
    283         if lookup_view in self.reverse_dict:
    284             return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]])
    285         raise NoReverseMatch
    286 
     295    # FIXME: this seems to be unused.
    287296    def reverse_helper(self, lookup_view, *args, **kwargs):
    288297        sub_match = self.reverse(lookup_view, *args, **kwargs)
    289298        result = reverse_helper(self.regex, *args, **kwargs)
Back to Top