Ticket #4915: smarter-urlresolvers.8695.diff
File smarter-urlresolvers.8695.diff, 2.2 KB (added by , 16 years ago) |
---|
-
django/core/urlresolvers.py
209 209 def reverse_helper(self, *args, **kwargs): 210 210 return reverse_helper(self.regex, *args, **kwargs) 211 211 212 def args_schemas(self): 213 named_args = [] 214 positional_args = 0 215 for m in re.findall(r'\((?:\?P<(\w+)>)?[^)]+\)', self.regex.pattern, re.UNICODE): 216 if m: 217 named_args.append(m) 218 else: 219 positional_args += 1 220 return [ 221 tuple([positional_args] + sorted(named_args)), 222 (positional_args+len(named_args),) 223 ] 224 212 225 class RegexURLResolver(object): 213 226 def __init__(self, regex, urlconf_name, default_kwargs=None): 214 227 # regex is a string representing a regular expression. … … 231 244 else: 232 245 self._reverse_dict[pattern.callback] = (pattern,) 233 246 self._reverse_dict[pattern.name] = (pattern,) 247 for args_schema in pattern.args_schemas(): 248 self._reverse_dict[pattern.callback, args_schema] = (pattern,) 249 self._reverse_dict[pattern.name, args_schema] = (pattern,) 234 250 return self._reverse_dict 235 251 reverse_dict = property(_get_reverse_dict) 236 252 … … 285 301 lookup_view = get_callable(lookup_view, True) 286 302 except (ImportError, AttributeError), e: 287 303 raise NoReverseMatch("Error importing '%s': %s." % (lookup_view, e)) 288 if lookup_view in self.reverse_dict: 289 return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]]) 304 args_schema = tuple([len(args)] + sorted(kwargs.keys())) 305 for key in ((lookup_view,args_schema), lookup_view): 306 if key in self.reverse_dict: 307 return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[key]]) 290 308 raise NoReverseMatch("Reverse for '%s' with arguments '%s' and keyword " 291 309 "arguments '%s' not found." % (lookup_view, args, kwargs)) 292 310