akarpov/akarpov/common/views.py

18 lines
621 B
Python

from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic.detail import SingleObjectMixin
class SuperUserRequiredMixin(LoginRequiredMixin, UserPassesTestMixin):
def test_func(self):
return self.request.user.is_superuser
class HasPermissions(SingleObjectMixin):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
has_perm = False
if self.request.user.is_authenticated:
has_perm = self.object.user == self.request.user
context["has_permissions"] = has_perm
return context