1
|
# combo - content management system
|
2
|
# Copyright (C) 2015 Entr'ouvert
|
3
|
#
|
4
|
# This program is free software: you can redistribute it and/or modify it
|
5
|
# under the terms of the GNU Affero General Public License as published
|
6
|
# by the Free Software Foundation, either version 3 of the License, or
|
7
|
# (at your option) any later version.
|
8
|
#
|
9
|
# This program is distributed in the hope that it will be useful,
|
10
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
# GNU Affero General Public License for more details.
|
13
|
#
|
14
|
# You should have received a copy of the GNU Affero General Public License
|
15
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
|
17
|
# Decorating URL includes, <https://djangosnippets.org/snippets/2532/>
|
18
|
|
19
|
from django.contrib.auth.decorators import user_passes_test
|
20
|
from django.core.exceptions import PermissionDenied
|
21
|
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
|
22
|
|
23
|
class DecoratedURLPattern(RegexURLPattern):
|
24
|
def resolve(self, *args, **kwargs):
|
25
|
result = super(DecoratedURLPattern, self).resolve(*args, **kwargs)
|
26
|
if result:
|
27
|
result.func = self._decorate_with(result.func)
|
28
|
return result
|
29
|
|
30
|
class DecoratedRegexURLResolver(RegexURLResolver):
|
31
|
def resolve(self, *args, **kwargs):
|
32
|
result = super(DecoratedRegexURLResolver, self).resolve(*args, **kwargs)
|
33
|
if result:
|
34
|
result.func = self._decorate_with(result.func)
|
35
|
return result
|
36
|
|
37
|
def decorated_includes(func, includes, *args, **kwargs):
|
38
|
urlconf_module, app_name, namespace = includes
|
39
|
|
40
|
for item in urlconf_module:
|
41
|
if isinstance(item, RegexURLPattern):
|
42
|
item.__class__ = DecoratedURLPattern
|
43
|
item._decorate_with = func
|
44
|
|
45
|
elif isinstance(item, RegexURLResolver):
|
46
|
item.__class__ = DecoratedRegexURLResolver
|
47
|
item._decorate_with = func
|
48
|
|
49
|
return urlconf_module, app_name, namespace
|
50
|
|
51
|
def manager_required(function=None, login_url=None):
|
52
|
def check_manager(user):
|
53
|
if user and user.is_staff:
|
54
|
return True
|
55
|
if user and not user.is_anonymous():
|
56
|
raise PermissionDenied()
|
57
|
# As the last resort, show the login form
|
58
|
return False
|
59
|
actual_decorator = user_passes_test(check_manager, login_url=login_url)
|
60
|
if function:
|
61
|
return actual_decorator(function)
|
62
|
return actual_decorator
|