Django

Code

Changeset 3057

Show
Ignore:
Timestamp:
06/01/06 23:38:16 (2 years ago)
Author:
adrian
Message:

Fixed #2025 -- Fixed some issues with URL reversal, which still isn't ready for prime time. Thanks, medhat

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/urlresolvers.py

    r2910 r3057  
    2323    dot = callback.rindex('.') 
    2424    return callback[:dot], callback[dot+1:] 
     25 
     26def reverse_helper(regex, *args, **kwargs): 
     27    """ 
     28    Does a "reverse" lookup -- returns the URL for the given args/kwargs. 
     29    The args/kwargs are applied to the given compiled regular expression. 
     30    For example: 
     31 
     32        >>> reverse_helper(re.compile('^places/(\d+)/$'), 3) 
     33        'places/3/' 
     34        >>> reverse_helper(re.compile('^places/(?P<id>\d+)/$'), id=3) 
     35        'places/3/' 
     36        >>> reverse_helper(re.compile('^people/(?P<state>\w\w)/(\w+)/$'), 'adrian', state='il') 
     37        'people/il/adrian/' 
     38 
     39    Raises NoReverseMatch if the args/kwargs aren't valid for the regex. 
     40    """ 
     41    # TODO: Handle nested parenthesis in the following regex. 
     42    result = re.sub(r'\(([^)]+)\)', MatchChecker(args, kwargs), regex.pattern) 
     43    return result.replace('^', '').replace('$', '') 
    2544 
    2645class MatchChecker(object): 
     
    109128 
    110129    def reverse_helper(self, *args, **kwargs): 
    111         """ 
    112         Does a "reverse" lookup -- returns the URL for the given args/kwargs. 
    113         The args/kwargs are applied to the regular expression in this 
    114         RegexURLPattern. For example: 
    115  
    116             >>> RegexURLPattern('^places/(\d+)/$').reverse_helper(3) 
    117             'places/3/' 
    118             >>> RegexURLPattern('^places/(?P<id>\d+)/$').reverse_helper(id=3) 
    119             'places/3/' 
    120             >>> RegexURLPattern('^people/(?P<state>\w\w)/(\w+)/$').reverse_helper('adrian', state='il') 
    121             'people/il/adrian/' 
    122  
    123         Raises NoReverseMatch if the args/kwargs aren't valid for the RegexURLPattern. 
    124         """ 
    125         # TODO: Handle nested parenthesis in the following regex. 
    126         result = re.sub(r'\(([^)]+)\)', MatchChecker(args, kwargs), self.regex.pattern) 
    127         return result.replace('^', '').replace('$', '') 
     130        return reverse_helper(self.regex, *args, **kwargs) 
    128131 
    129132class RegexURLResolver(object): 
     
    183186    def reverse(self, viewname, *args, **kwargs): 
    184187        for pattern in self.urlconf_module.urlpatterns: 
    185             if pattern.callback == viewname: 
     188            if isinstance(pattern, RegexURLResolver): 
     189                try: 
     190                    return pattern.reverse_helper(viewname, *args, **kwargs) 
     191                except NoReverseMatch: 
     192                    continue 
     193            elif pattern.callback == viewname: 
    186194                try: 
    187195                    return pattern.reverse_helper(*args, **kwargs) 
     
    189197                    continue 
    190198        raise NoReverseMatch 
     199 
     200    def reverse_helper(self, viewname, *args, **kwargs): 
     201        sub_match = self.reverse(viewname, *args, **kwargs) 
     202        result = reverse_helper(self.regex, *args, **kwargs) 
     203        return result + sub_match