expephalon/core/mixins/auth.py
2020-06-01 18:29:22 +02:00

20 lines
No EOL
777 B
Python

from django.contrib.auth.mixins import AccessMixin
from django.contrib.messages import error
from django.views.decorators.cache import never_cache
from core.models.profiles import AdminProfile
class AdminMixin(AccessMixin):
@never_cache
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
self.permission_denied_message = "You must be logged in to access this area."
else:
try:
AdminProfile.objects.get(user=request.user)
return super().dispatch(request, *args, **kwargs)
except AdminProfile.DoesNotExist:
self.permission_denied_message = "You must be an administrator to access this area."
return self.handle_no_permission()