2021-03-22 17:42:07 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.conf import settings
|
2021-05-26 12:08:15 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
|
|
|
from django.urls import reverse_lazy
|
2021-03-22 17:42:07 +00:00
|
|
|
|
2021-05-26 12:08:15 +00:00
|
|
|
class SuperUserRequiredMixin(UserPassesTestMixin):
|
|
|
|
def test_func(self):
|
|
|
|
try:
|
|
|
|
return self.request.user.is_superuser
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_login_url(self):
|
|
|
|
return reverse_lazy("localauth:login")
|
|
|
|
|
|
|
|
class LoginRequiredMixin(UserPassesTestMixin):
|
|
|
|
def test_func(self):
|
|
|
|
try:
|
|
|
|
return self.request.user.is_authenticated
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_login_url(self):
|
|
|
|
return reverse_lazy("localauth:login")
|
|
|
|
|
|
|
|
class MultiPermissionMixin:
|
|
|
|
MIXINS = []
|
2021-03-21 15:50:50 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2021-05-26 12:08:15 +00:00
|
|
|
for mixin in self.MIXINS:
|
|
|
|
if not mixin.test_func(self):
|
|
|
|
return redirect(mixin.get_login_url(self))
|
|
|
|
|
2021-03-22 17:42:07 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|