Ticket #4915: reverse-with-args.patch
File reverse-with-args.patch, 2.2 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 print m 206 if m: 207 named_args.append(m) 208 else: 209 positional_args += 1 210 return tuple([positional_args] + sorted(named_args)) 211 201 212 class RegexURLResolver(object): 202 213 def __init__(self, regex, urlconf_name, default_kwargs=None): 203 214 # regex is a string representing a regular expression. … … 218 229 for key, value in pattern.reverse_dict.iteritems(): 219 230 self._reverse_dict[key] = (pattern,) + value 220 231 else: 221 self._reverse_dict[pattern.callback] = (pattern,) 222 self._reverse_dict[pattern.name] = (pattern,) 232 args_schema = pattern.args_schema() 233 self._reverse_dict[pattern.callback, args_schema] = (pattern,) 234 self._reverse_dict[pattern.name, args_schema] = (pattern,) 223 235 return self._reverse_dict 224 236 reverse_dict = property(_get_reverse_dict) 225 237 … … 274 286 return self._resolve_special('500') 275 287 276 288 def reverse(self, lookup_view, *args, **kwargs): 289 ttt = lookup_view 277 290 try: 278 291 lookup_view = get_callable(lookup_view, True) 279 292 except (ImportError, AttributeError): 280 293 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]]) 294 args_schema = tuple([len(args)] + sorted(kwargs.keys())) 295 if (lookup_view,args_schema) in self.reverse_dict: 296 return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view,args_schema]]) 283 297 raise NoReverseMatch 284 298 285 299 def reverse_helper(self, lookup_view, *args, **kwargs):