2021-04-20 11:37:36 +00:00
|
|
|
from django.views.generic import CreateView, UpdateView, View, ListView, DetailView, FormView
|
2021-04-15 08:34:31 +00:00
|
|
|
from django.shortcuts import redirect, get_object_or_404
|
2021-04-08 09:30:19 +00:00
|
|
|
from django.contrib import messages
|
2021-04-20 06:36:59 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2021-04-08 09:33:00 +00:00
|
|
|
from django.contrib.gis.geos import Point
|
2021-04-18 06:12:15 +00:00
|
|
|
from django.contrib.gis.db.models.functions import Distance
|
2021-04-18 14:46:57 +00:00
|
|
|
from django.conf import settings
|
2021-04-20 11:37:36 +00:00
|
|
|
from django.utils import timezone
|
2021-03-01 09:58:08 +00:00
|
|
|
|
2021-04-08 09:30:19 +00:00
|
|
|
from public.mixins import InConstructionMixin
|
2021-04-18 06:12:15 +00:00
|
|
|
from partners.mixins import PartnerProfileRequiredMixin
|
2021-05-26 16:42:22 +00:00
|
|
|
from clients.mixins import ClientBaseMixin
|
2021-04-12 07:49:00 +00:00
|
|
|
from localauth.helpers import name_to_coords
|
2021-04-18 06:12:15 +00:00
|
|
|
from partners.models import Establishment
|
2021-04-18 10:02:17 +00:00
|
|
|
from payment.models import BillingAddress, Invoice, InvoiceItem, InvoicePayment
|
2021-04-18 14:46:57 +00:00
|
|
|
from clients.models import ClientProfile
|
2021-04-08 09:30:19 +00:00
|
|
|
|
2021-04-15 08:34:31 +00:00
|
|
|
from .models import Inquiry, Offer
|
2021-04-20 11:37:36 +00:00
|
|
|
from .forms import InquiryProcessForm, OfferSelectionForm
|
2021-04-08 09:30:19 +00:00
|
|
|
|
2021-05-26 16:42:22 +00:00
|
|
|
class InquiryCreateView(ClientBaseMixin, CreateView):
|
2021-04-08 09:30:19 +00:00
|
|
|
model = Inquiry
|
2021-04-08 11:38:10 +00:00
|
|
|
fields = ["destination_name", "budget", "arrival", "min_nights", "adults", "children"]
|
2021-04-08 09:30:19 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return redirect("/")
|
|
|
|
|
2021-04-08 11:38:10 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.destination_coords = self.clean_destination_coords()
|
2021-04-18 10:02:17 +00:00
|
|
|
form.instance.destination_radius = 5000
|
2021-04-08 11:38:10 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
2021-04-08 09:30:19 +00:00
|
|
|
def form_invalid(self, form, *args, **kwargs):
|
|
|
|
for field in form:
|
|
|
|
for error in field.errors:
|
|
|
|
messages.error(self.request, f"{field.name}: {error}")
|
|
|
|
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2021-04-08 11:38:10 +00:00
|
|
|
return reverse("auction:process_inquiry", args=(self.object.uuid,))
|
2021-04-08 09:30:19 +00:00
|
|
|
|
|
|
|
def clean_destination_coords(self):
|
2021-04-17 06:36:26 +00:00
|
|
|
lat, lon = name_to_coords(self.request.POST.get("destination_name"))
|
2021-04-08 11:38:10 +00:00
|
|
|
return Point(lon, lat)
|
|
|
|
|
2021-05-26 16:42:22 +00:00
|
|
|
class InquiryProcessView(ClientBaseMixin, UpdateView):
|
2021-04-13 12:44:22 +00:00
|
|
|
form_class = InquiryProcessForm
|
2021-04-08 11:38:10 +00:00
|
|
|
model = Inquiry
|
|
|
|
template_name = "auction/process.html"
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return Inquiry.objects.get(uuid=self.kwargs["uuid"])
|
2021-04-08 09:30:19 +00:00
|
|
|
|
2021-04-13 12:44:22 +00:00
|
|
|
def get_initial(self):
|
|
|
|
initial = super().get_initial()
|
|
|
|
|
|
|
|
try:
|
|
|
|
initial["country"] = self.request.user.clientprofile.country.code
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return initial
|
|
|
|
|
2021-04-18 10:02:17 +00:00
|
|
|
def form_valid(self, form):
|
2021-04-18 14:46:57 +00:00
|
|
|
profile, _ = ClientProfile.objects.get_or_create(user=self.request.user)
|
2021-04-18 10:02:17 +00:00
|
|
|
profile.first_name = form.cleaned_data["first_name"]
|
|
|
|
profile.last_name = form.cleaned_data["last_name"]
|
|
|
|
profile.street = form.cleaned_data["street"]
|
|
|
|
profile.city = form.cleaned_data["city"]
|
|
|
|
profile.zip = form.cleaned_data["zip"]
|
|
|
|
profile.state = form.cleaned_data["state"]
|
|
|
|
profile.country = form.cleaned_data["country"]
|
|
|
|
profile.save()
|
|
|
|
|
2021-04-18 14:46:57 +00:00
|
|
|
form.instance.client = profile
|
|
|
|
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def form_invalid(self, form, *args, **kwargs):
|
|
|
|
for field in form:
|
|
|
|
for error in field.errors:
|
|
|
|
messages.error(self.request, f"{field.name}: {error}")
|
|
|
|
|
|
|
|
return redirect("/")
|
|
|
|
|
2021-04-18 10:02:17 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse("auction:inquiry_payment", args=(self.object.uuid,))
|
|
|
|
|
2021-05-26 16:42:22 +00:00
|
|
|
class InquiryPaymentView(View):
|
2021-04-18 10:02:17 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
inquiry = Inquiry.objects.get(uuid=kwargs["uuid"])
|
2021-04-18 14:46:57 +00:00
|
|
|
try:
|
|
|
|
invoice = inquiry.invoice
|
|
|
|
except Invoice.DoesNotExist:
|
|
|
|
invoice = Invoice.from_inquiry(inquiry)
|
|
|
|
payment_url = InvoicePayment.from_invoice(invoice, inquiry.gateway)
|
|
|
|
|
|
|
|
return redirect(payment_url)
|
2021-04-15 08:34:31 +00:00
|
|
|
|
2021-05-26 16:42:22 +00:00
|
|
|
class OfferSelectionView(ClientBaseMixin, FormView, DetailView):
|
2021-04-15 08:34:31 +00:00
|
|
|
model = Inquiry
|
|
|
|
template_name = "auction/offer_select.html"
|
2021-04-20 11:37:36 +00:00
|
|
|
form_class = OfferSelectionForm
|
2021-04-15 08:34:31 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
2021-04-20 11:37:36 +00:00
|
|
|
return get_object_or_404(Inquiry, uuid=self.kwargs["uuid"], client=self.request.user.clientprofile)
|
2021-04-15 08:34:31 +00:00
|
|
|
|
2021-04-20 11:37:36 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
inquiry = self.get_object()
|
|
|
|
offer = get_object_or_404(Offer, inquiry=inquiry, uuid=form.cleaned_data["offer"])
|
|
|
|
offer.accepted = timezone.now()
|
|
|
|
offer.save()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy("clients:booking_view", args=[self.kwargs["uuid"]])
|
|
|
|
|
2021-05-26 16:42:22 +00:00
|
|
|
class OfferSelectionTableView(ClientBaseMixin, ListView):
|
2021-04-15 08:34:31 +00:00
|
|
|
model = Offer
|
|
|
|
content_type = "text/javascript"
|
|
|
|
template_name = "auction/offer_table.js"
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2021-04-20 11:37:36 +00:00
|
|
|
inquiry = get_object_or_404(Inquiry, uuid=self.kwargs["uuid"], client=self.request.user.clientprofile)
|
2021-04-20 06:36:59 +00:00
|
|
|
return inquiry.offer_set.all()
|
2021-04-19 10:51:50 +00:00
|
|
|
|
|
|
|
class OfferCreationView(InConstructionMixin, PartnerProfileRequiredMixin, CreateView):
|
|
|
|
model = Offer
|
|
|
|
template_name = "auction/offer_create.html"
|
2021-04-20 06:36:59 +00:00
|
|
|
fields = ["roomcategory", "departure", "comment"]
|
2021-04-19 10:51:50 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.establishment = self.get_establishment()
|
2021-04-20 06:36:59 +00:00
|
|
|
self.inquiry = self.get_inquiry()
|
2021-04-19 10:51:50 +00:00
|
|
|
|
|
|
|
if not self.establishment:
|
|
|
|
messages.warning(request, "Um bieten zu können, muss zuerst eine Unterkunft im System hinterlegt werden!")
|
|
|
|
return redirect("partners:establishment_register")
|
|
|
|
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_establishment(self):
|
|
|
|
establishment = self.kwargs.get("establishment", None)
|
|
|
|
kwargs = {"owner": self.request.user.partnerprofile}
|
|
|
|
|
|
|
|
if establishment:
|
|
|
|
kwargs["id"] = establishment
|
|
|
|
return get_object_or_404(Establishment, **kwargs)
|
|
|
|
else:
|
|
|
|
return Establishment.objects.filter(**kwargs).first()
|
|
|
|
|
2021-04-20 06:36:59 +00:00
|
|
|
def get_inquiry(self):
|
|
|
|
return get_object_or_404(Inquiry, uuid=self.kwargs.get("inquiry"))
|
|
|
|
|
2021-04-19 10:51:50 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["establishment"] = self.establishment
|
2021-04-20 06:36:59 +00:00
|
|
|
context["inquiry"] = self.inquiry
|
2021-04-19 10:51:50 +00:00
|
|
|
return context
|
2021-04-20 06:36:59 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.inquiry = self.inquiry
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
messages.success(self.request, "Angebot erfolgreich erstellt! Viel Erfolg!")
|
|
|
|
return reverse_lazy("auction:bidding", args=[self.establishment.id])
|
2021-04-18 06:12:15 +00:00
|
|
|
|
|
|
|
class BiddingListView(InConstructionMixin, PartnerProfileRequiredMixin, ListView):
|
|
|
|
model = Inquiry
|
|
|
|
template_name = "auction/bidding_list.html"
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.establishment = self.get_establishment()
|
|
|
|
|
|
|
|
if not self.establishment:
|
|
|
|
messages.warning(request, "Um bieten zu können, muss zuerst eine Unterkunft im System hinterlegt werden!")
|
|
|
|
return redirect("partners:establishment_register")
|
|
|
|
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_establishment(self):
|
|
|
|
establishment = self.kwargs.get("id", None)
|
|
|
|
kwargs = {"owner": self.request.user.partnerprofile}
|
|
|
|
|
|
|
|
if establishment:
|
|
|
|
kwargs["id"] = establishment
|
|
|
|
return get_object_or_404(Establishment, **kwargs)
|
|
|
|
else:
|
|
|
|
return Establishment.objects.filter(**kwargs).first()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
establishment = self.get_establishment()
|
2021-04-20 06:36:59 +00:00
|
|
|
excluded = [offer.inquiry.id for offer in establishment.offer_set.all()]
|
|
|
|
return Inquiry.objects.annotate(distance=Distance("destination_coords", establishment.coords)).exclude(activated__isnull=True).exclude(id__in=excluded)
|
2021-04-19 10:51:50 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["establishment"] = self.establishment
|
|
|
|
return context
|