Ticket #4915: smarter-urlresolvers.8695.diff

File smarter-urlresolvers.8695.diff, 2.2 KB (added by Ilya Semenov, 14 years ago)

revision bump

  • django/core/urlresolvers.py

     
    209209    def reverse_helper(self, *args, **kwargs):
    210210        return reverse_helper(self.regex, *args, **kwargs)
    211211
     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
    212225class RegexURLResolver(object):
    213226    def __init__(self, regex, urlconf_name, default_kwargs=None):
    214227        # regex is a string representing a regular expression.
     
    231244                else:
    232245                    self._reverse_dict[pattern.callback] = (pattern,)
    233246                    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,)
    234250        return self._reverse_dict
    235251    reverse_dict = property(_get_reverse_dict)
    236252
     
    285301            lookup_view = get_callable(lookup_view, True)
    286302        except (ImportError, AttributeError), e:
    287303            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]])
    290308        raise NoReverseMatch("Reverse for '%s' with arguments '%s' and keyword "
    291309                "arguments '%s' not found." % (lookup_view, args, kwargs))
    292310
Back to Top