Ticket #4915: reverse-with-args-2.patch

File reverse-with-args-2.patch, 2.1 KB (added by Ilya Semenov <semenov@…>, 17 years ago)

alternative approach, removed debug output

  • core/urlresolvers.py

     
    198198    def reverse_helper(self, *args, **kwargs):
    199199        return reverse_helper(self.regex, *args, **kwargs)
    200200
     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
    201211class RegexURLResolver(object):
    202212    def __init__(self, regex, urlconf_name, default_kwargs=None):
    203213        # regex is a string representing a regular expression.
     
    218228                    for key, value in pattern.reverse_dict.iteritems():
    219229                        self._reverse_dict[key] = (pattern,) + value
    220230                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,)
    223234        return self._reverse_dict
    224235    reverse_dict = property(_get_reverse_dict)
    225236
     
    278289            lookup_view = get_callable(lookup_view, True)
    279290        except (ImportError, AttributeError):
    280291            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]])
    283295        raise NoReverseMatch
    284296
    285297    def reverse_helper(self, lookup_view, *args, **kwargs):
Back to Top