Fix client/partner profile requirement check
This commit is contained in:
parent
4edbf8a922
commit
60026bfa9c
4 changed files with 50 additions and 24 deletions
|
@ -1,9 +1,14 @@
|
|||
from django.urls import reverse_lazy
|
||||
|
||||
from localauth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
||||
|
||||
class ClientProfileRequiredMixin(LoginRequiredMixin, UserPassesTestMixin):
|
||||
class ClientProfileRequiredMixin(UserPassesTestMixin):
|
||||
def test_func(self):
|
||||
try:
|
||||
assert self.request.user.clientprofile
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def get_login_url(self):
|
||||
return reverse_lazy("clients:register")
|
|
@ -6,11 +6,14 @@ from django.contrib import messages
|
|||
from .models import ClientProfile
|
||||
from .mixins import ClientProfileRequiredMixin
|
||||
|
||||
from localauth.mixins import LoginRequiredMixin
|
||||
from localauth.mixins import LoginRequiredMixin, MultiPermissionMixin
|
||||
from public.mixins import InConstructionMixin
|
||||
from auction.models import Inquiry
|
||||
|
||||
class ClientRegistrationView(InConstructionMixin, LoginRequiredMixin, CreateView):
|
||||
class ClientBaseMixin(MultiPermissionMixin):
|
||||
MIXINS = [LoginRequiredMixin, ClientProfileRequiredMixin]
|
||||
|
||||
class ClientRegistrationView(LoginRequiredMixin, CreateView):
|
||||
model = ClientProfile
|
||||
exclude = ["user"]
|
||||
template_name = "clients/signup.html"
|
||||
|
@ -53,7 +56,7 @@ class ClientRegistrationView(InConstructionMixin, LoginRequiredMixin, CreateView
|
|||
"phone": "+43"
|
||||
}
|
||||
|
||||
class ClientProfileView(InConstructionMixin, LoginRequiredMixin, UpdateView):
|
||||
class ClientProfileView(ClientBaseMixin, UpdateView):
|
||||
model = ClientProfile
|
||||
exclude = ["user"]
|
||||
template_name = "clients/profile.html"
|
||||
|
@ -71,21 +74,21 @@ class ClientProfileView(InConstructionMixin, LoginRequiredMixin, UpdateView):
|
|||
except ClientProfile.DoesNotExist:
|
||||
return redirect("clients:register")
|
||||
|
||||
class ClientDashboardView(InConstructionMixin, ClientProfileRequiredMixin, DetailView):
|
||||
class ClientDashboardView(ClientBaseMixin, DetailView):
|
||||
model = ClientProfile
|
||||
template_name = "clients/dashboard.html"
|
||||
|
||||
def get_object(self):
|
||||
return self.request.user.clientprofile
|
||||
|
||||
class ClientBookingsView(InConstructionMixin, ClientProfileRequiredMixin, ListView):
|
||||
class ClientBookingsView(ClientBaseMixin, ListView):
|
||||
model = Inquiry
|
||||
template_name = "clients/bookings.html"
|
||||
|
||||
def get_queryset(self):
|
||||
return Inquiry.objects.filter(client=self.request.user.clientprofile)
|
||||
|
||||
class ClientBookingView(InConstructionMixin, ClientProfileRequiredMixin, DetailView):
|
||||
class ClientBookingView(ClientBaseMixin, DetailView):
|
||||
model = Inquiry
|
||||
template_name = "clients/booking.html"
|
||||
|
||||
|
|
|
@ -3,20 +3,35 @@ from django.shortcuts import redirect
|
|||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
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:
|
||||
"""
|
||||
View mixin which requires that the authenticated user is a super user
|
||||
(i.e. `is_superuser` is True).
|
||||
"""
|
||||
class SuperUserRequiredMixin(UserPassesTestMixin):
|
||||
def test_func(self):
|
||||
try:
|
||||
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):
|
||||
if not request.user.is_superuser:
|
||||
messages.error(
|
||||
request,
|
||||
'You do not have the permission required to perform the '
|
||||
'requested operation.')
|
||||
return redirect(settings.LOGIN_URL)
|
||||
for mixin in self.MIXINS:
|
||||
if not mixin.test_func(self):
|
||||
return redirect(mixin.get_login_url(self))
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
|
|
@ -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):
|
||||
try:
|
||||
assert self.request.user.partnerprofile
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def get_login_url(self):
|
||||
return reverse_lazy("partners:register")
|
Loading…
Reference in a new issue