Ticket #13409: wrap_kwarg_for_url.patch
File wrap_kwarg_for_url.patch, 4.8 KB (added by , 13 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
116 116 return callback[:dot], callback[dot+1:] 117 117 118 118 class RegexURLPattern(object): 119 def __init__(self, regex, callback, default_args=None, name=None ):119 def __init__(self, regex, callback, default_args=None, name=None, wrap=None): 120 120 # regex is a string representing a regular expression. 121 121 # callback is either a string like 'foo.views.news.stories.story_detail' 122 122 # which represents the path to a module and a view function name, or a … … 129 129 self._callback_str = callback 130 130 self.default_args = default_args or {} 131 131 self.name = name 132 if isinstance(wrap, (list, tuple)): 133 self.wrap = wrap 134 elif wrap: 135 self.wrap = [wrap] 136 else: 137 self.wrap = [] 132 138 133 139 def __repr__(self): 134 140 return smart_str(u'<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern)) … … 159 165 160 166 def _get_callback(self): 161 167 if self._callback is not None: 162 return self._ callback168 return self._decorate(self._callback) 163 169 try: 164 170 self._callback = get_callable(self._callback_str) 165 171 except ImportError, e: … … 168 174 except AttributeError, e: 169 175 mod_name, func_name = get_mod_func(self._callback_str) 170 176 raise ViewDoesNotExist("Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))) 171 return self._ callback177 return self._decorate(self._callback) 172 178 callback = property(_get_callback) 173 179 180 def _decorate(self, callback): 181 for decorator in reversed(self.wrap): 182 callback = decorator(callback) 183 return callback 184 174 185 class RegexURLResolver(object): 175 def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None ):186 def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None, wrap=None): 176 187 # regex is a string representing a regular expression. 177 188 # urlconf_name is a string representing the module containing URLconfs. 178 189 self.regex = re.compile(regex, re.UNICODE) … … 186 197 self._reverse_dict = None 187 198 self._namespace_dict = None 188 199 self._app_dict = None 200 if isinstance(wrap, (tuple, list)): 201 self.wrap = wrap 202 elif wrap: 203 self.wrap = [wrap] 204 else: 205 self.wrap = [] 189 206 190 207 def __repr__(self): 191 208 return smart_str(u'<%s %s (%s:%s) %s>' % (self.__class__.__name__, self.urlconf_name, self.app_name, self.namespace, self.regex.pattern)) … … 262 279 sub_match_dict.update(self.default_kwargs) 263 280 for k, v in sub_match.kwargs.iteritems(): 264 281 sub_match_dict[smart_str(k)] = v 265 return ResolverMatch(s ub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces)282 return ResolverMatch(self._decorate(sub_match.func), sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces) 266 283 tried.append([pattern]) 267 284 raise Resolver404({'tried': tried, 'path': new_path}) 268 285 raise Resolver404({'path' : path}) 269 286 287 def _decorate(self, callback): 288 for decorator in reversed(self.wrap): 289 callback = decorator(callback) 290 return callback 291 270 292 def _get_urlconf_module(self): 271 293 try: 272 294 return self._urlconf_module