# coding=utf-8
import logging
from django.contrib.auth.views import redirect_to_login
from django.http import HttpResponseForbidden
from django.utils.translation import ugettext_lazy as _
from . import ExceptionResponse, ExceptionResponseView
[docs]class AbstractAccessView(ExceptionResponseView):
"""Allows you to handle authorization before dispatch is called"""
[docs] def check_authorization(self, *args, **kwargs):
raise NotImplementedError()
[docs] def dispatch(self, request, *args, **kwargs):
try:
return self.check_authorization() or super(AbstractAccessView, self).dispatch(request, *args, **kwargs)
except ExceptionResponse as ex:
logging.debug("ex.response: %s", ex.response)
return ex.response
[docs]class AuthenticatedView(AbstractAccessView):
"""redirects unauthenticated users to login"""
[docs] def check_authorization(self, *args, **kwargs):
if not self.is_authenticated(*args, **kwargs):
return self.handle_anonymous(*args, **kwargs)
# noinspection PyMethodMayBeStatic,PyUnusedLocal
[docs] def is_authenticated(self, *args, **kwargs):
return self.request.user.is_authenticated
# noinspection PyUnusedLocal
[docs] def handle_anonymous(self, *args, **kwargs):
path = self.request.get_full_path()
return redirect_to_login(path)
# Backward compatibility alias
AccessMixin = AuthenticatedView
[docs]class AbstractAuthorizedView(AuthenticatedView):
forbidden_message = _("You are not authorized to view this page")
[docs] def is_authorized(self, *args, **kwargs):
raise NotImplementedError()
[docs] def get_forbidden_message(self):
return self.forbidden_message
[docs] def handle_forbidden(self):
return HttpResponseForbidden(self.get_forbidden_message())
[docs] def check_authorization(self, *args, **kwargs):
if not self.request.user.is_authenticated:
return self.handle_anonymous(*args, **kwargs)
if not self.is_authorized(*args, **kwargs):
return self.handle_forbidden()
[docs]class StaffRequiredMixin(AbstractAuthorizedView):
[docs] def is_authorized(self, *args, **kwargs):
return self.request.user.is_staff