-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixins.py
55 lines (45 loc) · 1.79 KB
/
mixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! -*- encoding: UTF-8 -*-
from django.contrib.auth.mixins import LoginRequiredMixin, AccessMixin, PermissionRequiredMixin
from django.contrib import messages
from django import http
try:
from django.core.urlresolvers import reverse_lazy
except ImportError:
from django.urls import reverse_lazy
from django.template.loader import get_template
from django import http
from . import conf
class CustomLoginRequiredMixin(LoginRequiredMixin):
"""
Generic Mixin to make an user to be authenticated before see system information
"""
login_url = conf.LOGIN_URL
class CustomPermissionRequiredMixin(PermissionRequiredMixin):
def handle_no_permission(self):
return http.HttpResponseForbidden(get_template(conf.AUTH_PAGE_403).render())
class AlreadyAuthenticatedMixin(object):
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
return http.HttpResponseRedirect(
reverse_lazy(conf.AUTH_INDEX_URL_NAME)
)
else:
return super(AlreadyAuthenticatedMixin, self).dispatch(request, *args, **kwargs)
class SuperAdminRequiredMixin(AccessMixin):
"""
Generic mixin to ensure user logged is super_user
CBV mixin which verifies that the current user is authenticated.
"""
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return self.handle_no_permission()
else:
if request.user.is_staff:
return super(SuperAdminRequiredMixin, self).dispatch(request, *args, **kwargs)
else:
messages.add_message(
request,
messages.ERROR,
conf.PERMISSION_DENIED
)
return self.handle_no_permission()