Ticket #4915: django-smarter-resolver-3.patch
File django-smarter-resolver-3.patch, 5.1 KB (added by , 17 years ago) |
---|
-
django/core/urlresolvers.py
145 145 self.name = name 146 146 147 147 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)) 149 149 150 150 def add_prefix(self, prefix): 151 151 """ … … 209 209 self._reverse_dict = {} 210 210 211 211 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)) 213 213 214 214 def _get_reverse_dict(self): 215 def _add(key, val): 216 self._reverse_dict.setdefault(key, []).append(val) 215 217 if not self._reverse_dict and hasattr(self.urlconf_module, 'urlpatterns'): 216 218 for pattern in reversed(self.urlconf_module.urlpatterns): 217 219 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) 220 223 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,)) 223 227 return self._reverse_dict 224 228 reverse_dict = property(_get_reverse_dict) 225 229 … … 274 278 return self._resolve_special('500') 275 279 276 280 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()]) 277 285 try: 278 286 lookup_view = get_callable(lookup_view, True) 279 287 except (ImportError, AttributeError): 280 288 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 283 298 raise NoReverseMatch 284 299 285 300 def reverse_helper(self, lookup_view, *args, **kwargs): -
tests/regressiontests/templates/views.py
5 5 6 6 def client(request, id): 7 7 pass 8 9 def client_action(request, id, action):10 pass -
tests/regressiontests/templates/tests.py
732 732 ### URL TAG ######################################################## 733 733 # Successes 734 734 'url01' : ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'), 735 'url02' : ('{% url regressiontests.templates.views.client _actionclient.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/'), 736 736 'url03' : ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'), 737 737 'url04' : ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'), 738 738 'url05' : (u'{% url метка_оператора v %}', {'v': u'Ω'}, -
tests/regressiontests/templates/urls.py
7 7 # Test urls for testing reverse lookups 8 8 (r'^$', views.index), 9 9 (r'^client/(\d+)/$', views.client), 10 (r'^client/( \d+)/(?P<action>[^/]+)/$', views.client_action),10 (r'^client/(?P<id>\d+)/(?P<action>[^/]+)/$', views.client), 11 11 url(r'^named-client/(\d+)/$', views.client, name="named.client"), 12 12 13 13 # Unicode strings are permitted everywhere.