expephalon-demomodule/core/mixins/auth.py

18 lines
706 B
Python
Raw Normal View History

2020-05-21 12:54:59 +00:00
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()