Ticket #4915: django-smarter-resolver.patch

File django-smarter-resolver.patch, 2.1 KB (added by dusk@…, 17 years ago)

Preliminary resolver patch

  • django/core/urlresolvers.py

     
    212212        return '<%s %s %s>' % (self.__class__.__name__, self.urlconf_name, self.regex.pattern)
    213213
    214214    def _get_reverse_dict(self):
     215        def _add(key, val):
     216            self._reverse_dict.setdefault(key, []).append(val)
    215217        if not self._reverse_dict and hasattr(self.urlconf_module, 'urlpatterns'):
    216218            for pattern in reversed(self.urlconf_module.urlpatterns):
    217219                if isinstance(pattern, RegexURLResolver):
    218                     for key, value in pattern.reverse_dict.iteritems():
    219                         self._reverse_dict[key] = (pattern,) + value
     220                    for key, values in pattern.reverse_dict.iteritems():
     221                        for value in values:
     222                            _add(key, (pattern,) + value)
    220223                else:
    221                     self._reverse_dict[pattern.callback] = (pattern,)
    222                     self._reverse_dict[pattern.name] = (pattern,)
     224                    _add(pattern.callback, (pattern,))
     225                    if pattern.name:
     226                        _add(pattern.name, (pattern,))
    223227        return self._reverse_dict
    224228    reverse_dict = property(_get_reverse_dict)
    225229
     
    278282            lookup_view = get_callable(lookup_view, True)
    279283        except (ImportError, AttributeError):
    280284            raise NoReverseMatch
    281         if lookup_view in self.reverse_dict:
    282             return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]])
     285        for k, v in self.reverse_dict.iteritems():
     286            print "%s -> %s" % (repr(k), repr(v))
     287        for candidate in self.reverse_dict.get(lookup_view, []):
     288            result = u''.join([reverse_helper(part.regex, *args, **kwargs) for part in candidate])
     289            if self.resolve(u'/' + result) == (lookup_view, args, kwargs):
     290                return result
    283291        raise NoReverseMatch
    284292
    285293    def reverse_helper(self, lookup_view, *args, **kwargs):
Back to Top