|
|
@@ -2,29 +2,26 @@ import urlparse
|
|
2
|
2
|
|
|
3
|
3
|
from functools import wraps
|
|
4
|
4
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
|
5
|
|
-from django.core.urlresolvers import reverse
|
|
6
|
5
|
|
|
7
|
6
|
|
|
8
|
|
-def staff_member_required(view_func, login_url=None):
|
|
|
7
|
+def staff_member_required(view_func, login_url='/accounts/login/'):
|
|
9
|
8
|
"""
|
|
10
|
9
|
Decorator for views that checks that the user is logged in and is a staff
|
|
11
|
10
|
member. The user is redirected to the login page specified by *login_url*
|
|
12
|
11
|
if not authenticated.
|
|
|
12
|
+
|
|
13
|
13
|
This decorator is based on the admin decorator provided by the the django
|
|
14
|
14
|
``auth`` and ``admin`` packages.
|
|
15
|
15
|
"""
|
|
16
|
|
- if login_url:
|
|
17
|
|
- login_url = reverse('login')
|
|
18
|
16
|
|
|
19
|
17
|
@wraps(view_func)
|
|
20
|
18
|
def _checklogin(request, *args, **kwargs):
|
|
21
|
19
|
if request.user.is_active and request.user.is_staff:
|
|
22
|
|
- # The user is a valid staff member, continue to the redirect URL
|
|
23
|
20
|
return view_func(request, *args, **kwargs)
|
|
24
|
21
|
|
|
25
|
|
- path = request.build_absolute_uri()
|
|
26
|
22
|
# If the login url is the same scheme and net location then just
|
|
27
|
23
|
# use the path as the "next" url.
|
|
|
24
|
+ path = request.build_absolute_uri()
|
|
28
|
25
|
login_scheme, login_netloc = urlparse.urlparse(login_url)[:2]
|
|
29
|
26
|
current_scheme, current_netloc = urlparse.urlparse(path)[:2]
|
|
30
|
27
|
if ((not login_scheme or login_scheme == current_scheme) and
|
|
|
@@ -33,4 +30,5 @@ def staff_member_required(view_func, login_url=None):
|
|
33
|
30
|
|
|
34
|
31
|
from django.contrib.auth.views import redirect_to_login
|
|
35
|
32
|
return redirect_to_login(path, login_url, REDIRECT_FIELD_NAME)
|
|
|
33
|
+
|
|
36
|
34
|
return _checklogin
|