Ticket #13409: wrap_kwarg_for_url.diff
File wrap_kwarg_for_url.diff, 4.6 KB (added by , 15 years ago) |
---|
-
conf/urls/defaults.py
27 27 pattern_list.append(t) 28 28 return pattern_list 29 29 30 def url(regex, view, kwargs=None, name=None, prefix='' ):30 def url(regex, view, kwargs=None, name=None, prefix='', wrap=None): 31 31 if isinstance(view, (list,tuple)): 32 32 # For include(...) processing. 33 33 urlconf_module, app_name, namespace = view 34 return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace )34 return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace, wrap=wrap) 35 35 else: 36 36 if isinstance(view, basestring): 37 37 if not view: 38 38 raise ImproperlyConfigured('Empty URL pattern view name not permitted (for pattern %r)' % regex) 39 39 if prefix: 40 40 view = prefix + '.' + view 41 return RegexURLPattern(regex, view, kwargs, name )41 return RegexURLPattern(regex, view, kwargs, name, wrap=wrap) 42 42 -
core/urlresolvers.py
87 87 return callback[:dot], callback[dot+1:] 88 88 89 89 class RegexURLPattern(object): 90 def __init__(self, regex, callback, default_args=None, name=None ):90 def __init__(self, regex, callback, default_args=None, name=None, wrap=None): 91 91 # regex is a string representing a regular expression. 92 92 # callback is either a string like 'foo.views.news.stories.story_detail' 93 93 # which represents the path to a module and a view function name, or a … … 100 100 self._callback_str = callback 101 101 self.default_args = default_args or {} 102 102 self.name = name 103 if isinstance(wrap, (list, tuple)): 104 self.wrap = wrap 105 elif wrap: 106 self.wrap = [wrap] 107 else: 108 self.wrap = [] 103 109 104 110 def __repr__(self): 105 111 return '<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern) … … 130 136 131 137 def _get_callback(self): 132 138 if self._callback is not None: 133 return self._ callback139 return self._decorate(self._callback) 134 140 try: 135 141 self._callback = get_callable(self._callback_str) 136 142 except ImportError, e: … … 139 145 except AttributeError, e: 140 146 mod_name, func_name = get_mod_func(self._callback_str) 141 147 raise ViewDoesNotExist("Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))) 142 return self._ callback148 return self._decorate(self._callback) 143 149 callback = property(_get_callback) 144 150 151 def _decorate(self, callback): 152 for decorator in reversed(self.wrap): 153 callback = decorator(callback) 154 return callback 155 145 156 class RegexURLResolver(object): 146 def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None ):157 def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None, wrap=None): 147 158 # regex is a string representing a regular expression. 148 159 # urlconf_name is a string representing the module containing URLconfs. 149 160 self.regex = re.compile(regex, re.UNICODE) … … 157 168 self._reverse_dict = None 158 169 self._namespace_dict = None 159 170 self._app_dict = None 171 if isinstance(wrap, (tuple, list)): 172 self.wrap = wrap 173 elif wrap: 174 self.wrap = [wrap] 175 else: 176 self.wrap = [] 160 177 161 178 def __repr__(self): 162 179 return '<%s %s (%s:%s) %s>' % (self.__class__.__name__, self.urlconf_name, self.app_name, self.namespace, self.regex.pattern) … … 232 249 sub_match_dict.update(self.default_kwargs) 233 250 for k, v in sub_match[2].iteritems(): 234 251 sub_match_dict[smart_str(k)] = v 235 return s ub_match[0], sub_match[1], sub_match_dict252 return self._decorate(sub_match[0]), sub_match[1], sub_match_dict 236 253 tried.append(pattern.regex.pattern) 237 254 raise Resolver404({'tried': tried, 'path': new_path}) 238 255 raise Resolver404({'path' : path}) 239 256 257 def _decorate(self, callback): 258 for decorator in reversed(self.wrap): 259 callback = decorator(callback) 260 return callback 261 240 262 def _get_urlconf_module(self): 241 263 try: 242 264 return self._urlconf_module