expephalon-playsms/core/mixins/auth.py

18 lines
No EOL
706 B
Python

from django.contrib.auth.mixins import AccessMixin
from django.contrib.messages import error
from core.models.profiles import AdminProfile
class AdminMixin(AccessMixin):
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()