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
|
|
|
|
|
|
|
|
class SuperUserRequiredMixin:
|
2021-03-21 15:50:50 +00:00
|
|
|
"""
|
|
|
|
View mixin which requires that the authenticated user is a super user
|
|
|
|
(i.e. `is_superuser` is True).
|
|
|
|
"""
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
'You do not have the permission required to perform the '
|
|
|
|
'requested operation.')
|
|
|
|
return redirect(settings.LOGIN_URL)
|
2021-03-22 17:42:07 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class LoginRequiredMixin:
|
|
|
|
"""
|
|
|
|
View mixin which verifies that the user has authenticated.
|
|
|
|
|
|
|
|
NOTE:
|
|
|
|
This should be the left-most mixin of a view.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2021-04-08 09:30:19 +00:00
|
|
|
messages.info(self.request, "Um diese Seite anzuzeigen, musst du dich einloggen.")
|
2021-03-22 17:42:07 +00:00
|
|
|
return super().dispatch(*args, **kwargs)
|