Add mixins to check for partner/client profiles

This commit is contained in:
Kumi 2021-04-11 08:36:19 +02:00
parent 17b9ca72c0
commit e2d32e0903
4 changed files with 30 additions and 20 deletions

9
clients/mixins.py Normal file
View file

@ -0,0 +1,9 @@
from localauth.mixins import LoginRequiredMixin, UserPassesTestMixin
class ClientProfileRequiredMixin(LoginRequiredMixin, UserPassesTestMixin):
def test_func(self):
try:
assert self.request.user.clientprofile
return True
except:
return False

View file

@ -3,6 +3,7 @@ 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
class SuperUserRequiredMixin: class SuperUserRequiredMixin:
""" """
@ -19,17 +20,3 @@ class SuperUserRequiredMixin:
'requested operation.') 'requested operation.')
return redirect(settings.LOGIN_URL) return redirect(settings.LOGIN_URL)
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
class LoginRequiredMixin:
"""
View mixin which verifies that the user has authenticated.
NOTE:
This should be the left-most mixin of a view.
"""
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
messages.info(self.request, "Um diese Seite anzuzeigen, musst du dich einloggen.")
return super().dispatch(*args, **kwargs)

9
partners/mixins.py Normal file
View file

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

View file

@ -4,7 +4,8 @@ from django.http import HttpResponseRedirect
from django.db.models import F from django.db.models import F
from django.shortcuts import get_list_or_404 from django.shortcuts import get_list_or_404
from .models import PartnerProfile, Establishment from .models import PartnerProfile, Establishment,
from .mixins import PartnerProfileRequiredMixin
from auction.models import Inquiry, Offer from auction.models import Inquiry, Offer
from public.mixins import InConstructionMixin from public.mixins import InConstructionMixin
@ -49,7 +50,7 @@ class PartnerRegistrationView(InConstructionMixin, LoginRequiredMixin, CreateVie
except: except:
return {} return {}
class PartnerProfileView(LoginRequiredMixin, UpdateView): class PartnerProfileView(PartnerProfileRequiredMixin, UpdateView):
model = PartnerProfile model = PartnerProfile
exclude = ["user"] exclude = ["user"]
template_name = "partners/profile.html" template_name = "partners/profile.html"
@ -61,7 +62,7 @@ class PartnerProfileView(LoginRequiredMixin, UpdateView):
def get_object(self, queryset=None): def get_object(self, queryset=None):
return self.request.user.partnerprofile return self.request.user.partnerprofile
class BiddingListView(LoginRequiredMixin, ListView): class BiddingListView(PartnerProfileRequiredMixin, ListView):
model = Inquiry model = Inquiry
template_name = "partners/bidding_list.html" template_name = "partners/bidding_list.html"
@ -81,16 +82,20 @@ class BiddingListView(LoginRequiredMixin, ListView):
def get_queryset(self): def get_queryset(self):
return Inquiry.objects.filter(active=True).distance(self.establishment.coords).filter(distance__lte=F("destination_radius")) return Inquiry.objects.filter(active=True).distance(self.establishment.coords).filter(distance__lte=F("destination_radius"))
class OffersListView(LoginRequiredMixin, ListView): class OffersListView(PartnerProfileRequiredMixin, ListView):
model = Offer model = Offer
template_name = "partners/offer_list.html" template_name = "partners/offer_list.html"
def get_queryset(self): def get_queryset(self):
return Offer.objects.filter(establishment__in=self.request.user.partnerprofile.establishment_set.all()) return Offer.objects.filter(establishment__in=self.request.user.partnerprofile.establishment_set.all())
class EstablishmentsListView(LoginRequiredMixin, ListView): class EstablishmentsListView(PartnerProfileRequiredMixin, ListView):
model = Establishment model = Establishment
template_name = "partners/establishment_list.html" template_name = "partners/establishment_list.html"
def get_queryset(self): def get_queryset(self):
return self.request.user.partnerprofile.establishment_set return self.request.user.partnerprofile.establishment_set
class EstablishmentRequestView(PartnerProfileRequiredMixin, ListView):
model = Establishment
template_name = "partners/establishment_edit.html"