from django.views.generic import CreateView, UpdateView, TemplateView, ListView, DetailView from django.urls import reverse_lazy from django.shortcuts import redirect from django.contrib import messages from .models import ClientProfile from .mixins import ClientBaseMixin from localauth.mixins import LoginRequiredMixin, RedirectToNextMixin from public.mixins import InConstructionMixin from auction.models import Inquiry class ClientRegistrationView(LoginRequiredMixin, RedirectToNextMixin, CreateView): model = ClientProfile exclude = ["user"] template_name = "clients/signup.html" fields = ["company", "vat_id", "first_name", "last_name", "street", "city", "zip", "state", "country", "phone"] def dispatch(self, request, *args, **kwargs): try: ClientProfile.objects.get(user=request.user) return redirect(reverse_lazy("clients:profile")) except (ClientProfile.DoesNotExist, TypeError): return super().dispatch(request, *args, **kwargs) def form_valid(self, form): form.instance.user = self.request.user return super().form_valid(form) def get_success_url(self): messages.success(self.request, "Profil erfolgreich angelegt!") return redirect(self.get_redirect_url()) if self.get_redirect_url() else reverse_lazy("clients:profile") def get_initial(self): try: partner = self.request.user.partnerprofile return { "company": partner.company, "vat_id": partner.vat_id, "first_name": partner.first_name, "last_name": partner.last_name, "street": partner.street, "city": partner.city, "zip": partner.zip, "state": partner.state, "country": partner.country, "phone": partner.phone, } except: return { "country": "AT", "phone": "+43" } class ClientProfileView(ClientBaseMixin, UpdateView): model = ClientProfile exclude = ["user"] template_name = "clients/profile.html" fields = ["company", "vat_id", "first_name", "last_name", "street", "city", "zip", "state", "country"] def get_success_url(self): return reverse_lazy("clients:profile") def get_object(self, queryset=None): return self.request.user.clientprofile def get(self, request, *args, **kwargs): try: return super().get(request, *args, **kwargs) except ClientProfile.DoesNotExist: return redirect("clients:register") class ClientDashboardView(ClientBaseMixin, DetailView): model = ClientProfile template_name = "clients/dashboard.html" def get_object(self): return self.request.user.clientprofile 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(ClientBaseMixin, DetailView): model = Inquiry template_name = "clients/booking.html" def get_object(self): return Inquiry.objects.get(uuid=self.kwargs["uuid"], client=self.request.user.clientprofile)