Fix client/partner profile requirement check

This commit is contained in:
Kumi 2021-05-26 14:08:15 +02:00
parent 4edbf8a922
commit 60026bfa9c
4 changed files with 50 additions and 24 deletions

View file

@ -1,9 +1,14 @@
from django.urls import reverse_lazy
from localauth.mixins import LoginRequiredMixin, UserPassesTestMixin from localauth.mixins import LoginRequiredMixin, UserPassesTestMixin
class ClientProfileRequiredMixin(LoginRequiredMixin, UserPassesTestMixin): class ClientProfileRequiredMixin(UserPassesTestMixin):
def test_func(self): def test_func(self):
try: try:
assert self.request.user.clientprofile assert self.request.user.clientprofile
return True return True
except: except:
return False return False
def get_login_url(self):
return reverse_lazy("clients:register")

View file

@ -6,11 +6,14 @@ from django.contrib import messages
from .models import ClientProfile from .models import ClientProfile
from .mixins import ClientProfileRequiredMixin from .mixins import ClientProfileRequiredMixin
from localauth.mixins import LoginRequiredMixin from localauth.mixins import LoginRequiredMixin, MultiPermissionMixin
from public.mixins import InConstructionMixin from public.mixins import InConstructionMixin
from auction.models import Inquiry from auction.models import Inquiry
class ClientRegistrationView(InConstructionMixin, LoginRequiredMixin, CreateView): class ClientBaseMixin(MultiPermissionMixin):
MIXINS = [LoginRequiredMixin, ClientProfileRequiredMixin]
class ClientRegistrationView(LoginRequiredMixin, CreateView):
model = ClientProfile model = ClientProfile
exclude = ["user"] exclude = ["user"]
template_name = "clients/signup.html" template_name = "clients/signup.html"
@ -53,7 +56,7 @@ class ClientRegistrationView(InConstructionMixin, LoginRequiredMixin, CreateView
"phone": "+43" "phone": "+43"
} }
class ClientProfileView(InConstructionMixin, LoginRequiredMixin, UpdateView): class ClientProfileView(ClientBaseMixin, UpdateView):
model = ClientProfile model = ClientProfile
exclude = ["user"] exclude = ["user"]
template_name = "clients/profile.html" template_name = "clients/profile.html"
@ -71,21 +74,21 @@ class ClientProfileView(InConstructionMixin, LoginRequiredMixin, UpdateView):
except ClientProfile.DoesNotExist: except ClientProfile.DoesNotExist:
return redirect("clients:register") return redirect("clients:register")
class ClientDashboardView(InConstructionMixin, ClientProfileRequiredMixin, DetailView): class ClientDashboardView(ClientBaseMixin, DetailView):
model = ClientProfile model = ClientProfile
template_name = "clients/dashboard.html" template_name = "clients/dashboard.html"
def get_object(self): def get_object(self):
return self.request.user.clientprofile return self.request.user.clientprofile
class ClientBookingsView(InConstructionMixin, ClientProfileRequiredMixin, ListView): class ClientBookingsView(ClientBaseMixin, ListView):
model = Inquiry model = Inquiry
template_name = "clients/bookings.html" template_name = "clients/bookings.html"
def get_queryset(self): def get_queryset(self):
return Inquiry.objects.filter(client=self.request.user.clientprofile) return Inquiry.objects.filter(client=self.request.user.clientprofile)
class ClientBookingView(InConstructionMixin, ClientProfileRequiredMixin, DetailView): class ClientBookingView(ClientBaseMixin, DetailView):
model = Inquiry model = Inquiry
template_name = "clients/booking.html" template_name = "clients/booking.html"

View file

@ -3,20 +3,35 @@ from django.shortcuts import redirect
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.conf import settings from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import reverse_lazy
class SuperUserRequiredMixin: class SuperUserRequiredMixin(UserPassesTestMixin):
""" def test_func(self):
View mixin which requires that the authenticated user is a super user try:
(i.e. `is_superuser` is True). return self.request.user.is_superuser
""" except:
return False
def get_login_url(self):
return reverse_lazy("localauth:login")
class LoginRequiredMixin(UserPassesTestMixin):
def test_func(self):
try:
return self.request.user.is_authenticated
except:
return False
def get_login_url(self):
return reverse_lazy("localauth:login")
class MultiPermissionMixin:
MIXINS = []
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
if not request.user.is_superuser: for mixin in self.MIXINS:
messages.error( if not mixin.test_func(self):
request, return redirect(mixin.get_login_url(self))
'You do not have the permission required to perform the '
'requested operation.')
return redirect(settings.LOGIN_URL)
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)

View file

@ -1,9 +1,12 @@
from localauth.mixins import LoginRequiredMixin, UserPassesTestMixin from localauth.mixins import UserPassesTestMixin
class PartnerProfileRequiredMixin(LoginRequiredMixin, UserPassesTestMixin): class PartnerProfileRequiredMixin(UserPassesTestMixin):
def test_func(self): def test_func(self):
try: try:
assert self.request.user.partnerprofile assert self.request.user.partnerprofile
return True return True
except: except:
return False return False
def get_login_url(self):
return reverse_lazy("partners:register")