JourneyJoker/localauth/mixins.py

38 lines
1.1 KiB
Python
Raw Normal View History

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
from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import reverse_lazy
2021-03-22 17:42:07 +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 = []
def dispatch(self, request, *args, **kwargs):
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)