1
|
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
|
2
|
|
3
|
'''Decorators to apply a decorator to all views inside an urlpattern'''
|
4
|
|
5
|
__all__ = [ 'decorated_includes' ]
|
6
|
|
7
|
class DecoratedURLPattern(RegexURLPattern):
|
8
|
def resolve(self, *args, **kwargs):
|
9
|
result = super(DecoratedURLPattern, self).resolve(*args, **kwargs)
|
10
|
if result:
|
11
|
result.func = self._decorate_with(result.func)
|
12
|
return result
|
13
|
|
14
|
class DecoratedRegexURLResolver(RegexURLResolver):
|
15
|
def resolve(self, *args, **kwargs):
|
16
|
result = super(DecoratedRegexURLResolver, self).resolve(*args, **kwargs)
|
17
|
if result:
|
18
|
result.func = self._decorate_with(result.func)
|
19
|
return result
|
20
|
|
21
|
def decorated_includes(func, includes, *args, **kwargs):
|
22
|
urlconf_module, app_name, namespace = includes
|
23
|
|
24
|
for item in urlconf_module:
|
25
|
if isinstance(item, RegexURLPattern):
|
26
|
item.__class__ = DecoratedURLPattern
|
27
|
item._decorate_with = func
|
28
|
|
29
|
elif isinstance(item, RegexURLResolver):
|
30
|
item.__class__ = DecoratedRegexURLResolver
|
31
|
item._decorate_with = func
|
32
|
|
33
|
return urlconf_module, app_name, namespace
|