Ticket #4915: django-smarter-resolver-3.patch

File django-smarter-resolver-3.patch, 5.1 KB (added by dusk@…, 17 years ago)
  • django/core/urlresolvers.py

     
    145145        self.name = name
    146146
    147147    def __repr__(self):
    148         return '<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern)
     148        return '<%s %s %s>' % (self.__class__.__name__, repr(self.name), repr(self.regex.pattern))
    149149
    150150    def add_prefix(self, prefix):
    151151        """
     
    209209        self._reverse_dict = {}
    210210
    211211    def __repr__(self):
    212         return '<%s %s %s>' % (self.__class__.__name__, self.urlconf_name, self.regex.pattern)
     212        return '<%s %s %s>' % (self.__class__.__name__, self.urlconf_name, repr(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
     
    274278        return self._resolve_special('500')
    275279
    276280    def reverse(self, lookup_view, *args, **kwargs):
     281        # To compare results, we have to coerce all the values into Unicode
     282        # (to match the behavior of MatchChecker)
     283        args = tuple(map(unicode, args))
     284        kwargs = dict([(key, unicode(val)) for key, val in kwargs.items()])
    277285        try:
    278286            lookup_view = get_callable(lookup_view, True)
    279287        except (ImportError, AttributeError):
    280288            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]])
     289        # reversed() to match earlier behavior
     290        for candidate in reversed(self.reverse_dict.get(lookup_view, [])):
     291            try:
     292                result = u''.join([reverse_helper(part.regex, *args, **kwargs) for part in candidate])
     293                [resview, resargs, reskwargs] = self.resolve(u'/' + result)
     294                if args == resargs and kwargs == reskwargs:
     295                    return result
     296            except (NoReverseMatch, Resolver404), e:
     297                pass
    283298        raise NoReverseMatch
    284299
    285300    def reverse_helper(self, lookup_view, *args, **kwargs):
  • tests/regressiontests/templates/views.py

     
    55
    66def client(request, id):
    77    pass
    8 
    9 def client_action(request, id, action):
    10     pass
  • tests/regressiontests/templates/tests.py

     
    732732            ### URL TAG ########################################################
    733733            # Successes
    734734            'url01' : ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),
    735             'url02' : ('{% url regressiontests.templates.views.client_action client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
     735            'url02' : ('{% url regressiontests.templates.views.client id=client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
    736736            'url03' : ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'),
    737737            'url04' : ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
    738738            'url05' : (u'{% url метка_оператора v %}', {'v': u'Ω'},
  • tests/regressiontests/templates/urls.py

     
    77    # Test urls for testing reverse lookups
    88    (r'^$', views.index),
    99    (r'^client/(\d+)/$', views.client),
    10     (r'^client/(\d+)/(?P<action>[^/]+)/$', views.client_action),
     10    (r'^client/(?P<id>\d+)/(?P<action>[^/]+)/$', views.client),
    1111    url(r'^named-client/(\d+)/$', views.client, name="named.client"),
    1212
    1313    # Unicode strings are permitted everywhere.
Back to Top