Ticket #4915: reverse-with-args-2.patch
File reverse-with-args-2.patch, 2.1 KB (added by , 17 years ago) |
---|
-
core/urlresolvers.py
198 198 def reverse_helper(self, *args, **kwargs): 199 199 return reverse_helper(self.regex, *args, **kwargs) 200 200 201 def args_schema(self): 202 named_args = [] 203 positional_args = 0 204 for m in re.findall(r'\((?:\?P<(\w+)>)?[^)]+\)', self.regex.pattern, re.UNICODE): 205 if m: 206 named_args.append(m) 207 else: 208 positional_args += 1 209 return tuple([positional_args] + sorted(named_args)) 210 201 211 class RegexURLResolver(object): 202 212 def __init__(self, regex, urlconf_name, default_kwargs=None): 203 213 # regex is a string representing a regular expression. … … 218 228 for key, value in pattern.reverse_dict.iteritems(): 219 229 self._reverse_dict[key] = (pattern,) + value 220 230 else: 221 self._reverse_dict[pattern.callback] = (pattern,) 222 self._reverse_dict[pattern.name] = (pattern,) 231 args_schema = pattern.args_schema() 232 self._reverse_dict[pattern.callback, args_schema] = (pattern,) 233 self._reverse_dict[pattern.name, args_schema] = (pattern,) 223 234 return self._reverse_dict 224 235 reverse_dict = property(_get_reverse_dict) 225 236 … … 278 289 lookup_view = get_callable(lookup_view, True) 279 290 except (ImportError, AttributeError): 280 291 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]]) 292 args_schema = tuple([len(args)] + sorted(kwargs.keys())) 293 if (lookup_view,args_schema) in self.reverse_dict: 294 return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view,args_schema]]) 283 295 raise NoReverseMatch 284 296 285 297 def reverse_helper(self, lookup_view, *args, **kwargs):