Ticket #4915: smarter-urlresolvers.8461.diff

File smarter-urlresolvers.8461.diff, 2.2 KB (added by Ilya Semenov, 16 years ago)
  • django/core/urlresolvers.py

     
    205205    def reverse_helper(self, *args, **kwargs):
    206206        return reverse_helper(self.regex, *args, **kwargs)
    207207
     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
    208221class RegexURLResolver(object):
    209222    def __init__(self, regex, urlconf_name, default_kwargs=None):
    210223        # regex is a string representing a regular expression.
     
    227240                else:
    228241                    self._reverse_dict[pattern.callback] = (pattern,)
    229242                    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,)
    230246        return self._reverse_dict
    231247    reverse_dict = property(_get_reverse_dict)
    232248
     
    286302            lookup_view = get_callable(lookup_view, True)
    287303        except (ImportError, AttributeError):
    288304            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]])
    291309        raise NoReverseMatch("Reverse for '%s' not found." % lookup_view)
    292310
    293311    def reverse_helper(self, lookup_view, *args, **kwargs):
Back to Top