| | 25 | |
|---|
| | 26 | def 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('$', '') |
|---|
| 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) |
|---|