Ticket #4915: smarter-urlresolvers.8461.diff
File smarter-urlresolvers.8461.diff, 2.2 KB (added by , 16 years ago) |
---|
-
django/core/urlresolvers.py
205 205 def reverse_helper(self, *args, **kwargs): 206 206 return reverse_helper(self.regex, *args, **kwargs) 207 207 208 def args_schemas(self): 209 named_args = [] 210 positional_args = 0 211 for m in re.findall(r'\((?:\?P<(\w+)>)?[^)]+\)', self.regex.pattern, re.UNICODE): 212 if m: 213 named_args.append(m) 214 else: 215 positional_args += 1 216 return [ 217 tuple([positional_args] + sorted(named_args)), 218 (positional_args+len(named_args),) 219 ] 220 208 221 class RegexURLResolver(object): 209 222 def __init__(self, regex, urlconf_name, default_kwargs=None): 210 223 # regex is a string representing a regular expression. … … 227 240 else: 228 241 self._reverse_dict[pattern.callback] = (pattern,) 229 242 self._reverse_dict[pattern.name] = (pattern,) 243 for args_schema in pattern.args_schemas(): 244 self._reverse_dict[pattern.callback, args_schema] = (pattern,) 245 self._reverse_dict[pattern.name, args_schema] = (pattern,) 230 246 return self._reverse_dict 231 247 reverse_dict = property(_get_reverse_dict) 232 248 … … 286 302 lookup_view = get_callable(lookup_view, True) 287 303 except (ImportError, AttributeError): 288 304 raise NoReverseMatch("'%s' is not a callable." % lookup_view) 289 if lookup_view in self.reverse_dict: 290 return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]]) 305 args_schema = tuple([len(args)] + sorted(kwargs.keys())) 306 for key in ((lookup_view,args_schema), lookup_view): 307 if key in self.reverse_dict: 308 return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[key]]) 291 309 raise NoReverseMatch("Reverse for '%s' not found." % lookup_view) 292 310 293 311 def reverse_helper(self, lookup_view, *args, **kwargs):