2018-06-03 13:34:04 +00:00
|
|
|
from django.contrib import messages
|
2020-02-22 13:50:09 +00:00
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin, UserPassesTestMixin
|
2020-02-12 12:10:41 +00:00
|
|
|
from django.http import HttpResponseForbidden
|
2018-06-03 13:34:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StaffMemberRequiredMixin(object):
|
|
|
|
"""
|
|
|
|
A CBV mixin for when a view should only be permitted for staff users
|
|
|
|
"""
|
2019-06-16 12:32:24 +00:00
|
|
|
|
2018-06-03 13:34:04 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
# only permit staff users
|
|
|
|
if not request.user.is_staff:
|
|
|
|
messages.error(request, "No thanks")
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
|
|
# continue with the request
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
2018-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
class RaisePermissionRequiredMixin(PermissionRequiredMixin):
|
|
|
|
"""
|
|
|
|
A subclass of PermissionRequiredMixin which raises an exception to return 403 rather than a redirect to the login page
|
|
|
|
We use this to avoid a redirect loop since our login page redirects back to the ?next= url when a user is logged in...
|
|
|
|
"""
|
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
raise_exception = True
|
2020-02-22 13:50:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserIsObjectOwnerMixin(UserPassesTestMixin):
|
|
|
|
def test_func(self):
|
|
|
|
return self.get_object().user == self.request.user
|