from django.shortcuts import render, redirect from django.views.generic import CreateView, DetailView, FormView from django.urls import reverse_lazy from django.contrib.gis.geos import Point from django.contrib.messages import error from django.conf import settings from geopy.geocoders import Nominatim from auction.models import Inquiry from profiles.models import ClientProfile, ContactProfile from auction.forms import PostPaymentForm, InquiryForm from payment.models import KlarnaPayment, PaypalPayment, StripePayment, DummyPayment # Create your views here. class InquiryView(FormView): form_class = InquiryForm def get(self, request, *args, **kwargs): return redirect(reverse_lazy("frontend:index")) def form_invalid(self, form): print(repr(form.errors)) return redirect(reverse_lazy("frontend:index") + "?invalid=verytrue") def form_valid(self, form): try: user = ClientProfile.objects.get(user=self.request.user) except: user = None lat, lon = self.request.POST.get("destination_lat", None), self.request.POST.get("destination_lon", None) if (not lat) or (not lon): location = Nominatim(user_agent="UrlaubsAuktion 1.0").geocode(form.instance.destination_name, country_codes="at") lat, lon = location.latitude, location.longitude inquiry = Inquiry.objects.create( user = user, amount = form.cleaned_data["amount"], first_date = form.cleaned_data["first_date"], last_date = form.cleaned_data["last_date"], destination_name = form.cleaned_data["destination_name"], adults = form.cleaned_data["adults"], children = form.cleaned_data["children"], currency = "eur", destination_geo = Point(lon, lat) ) return redirect(inquiry.get_absolute_url()) class PaymentView(DetailView): model = Inquiry template_name = "auction/payment.html" class PostPaymentView(FormView): form_class = PostPaymentForm def form_invalid(self, form): #super().form_invalid(form) for _dumbo, errormsg in form.errors: error(self.request, errormsg) return redirect(reverse_lazy("auction:payment", kwargs={'pk': self.kwargs["pk"]})) def form_valid(self, form): #super().form_valid(form) # ClientProfile try: client = ClientProfile.objects.get(user=self.request.user) except ClientProfile.DoesNotExist: # pylint: disable=no-member client = ClientProfile.objects.create( user = self.request.user, first_name = form.cleaned_data["first_name"], last_name = form.cleaned_data["last_name"], address = form.cleaned_data["address"], address2 = form.cleaned_data["address2"], zipcode = form.cleaned_data["zipcode"], city = form.cleaned_data["city"], country = form.cleaned_data["country"], phone = form.cleaned_data["phone"] ) self.request.user.email = form.cleaned_data["email"] self.request.user.save() # ContactProfile contact = ContactProfile.objects.create( user = self.request.user, first_name = form.cleaned_data["first_name"], last_name = form.cleaned_data["last_name"], address = form.cleaned_data["address"], address2 = form.cleaned_data["address2"], zipcode = form.cleaned_data["zipcode"], city = form.cleaned_data["city"], country = form.cleaned_data["country"], phone = form.cleaned_data["phone"], email = form.cleaned_data["email"] ) # Inquiry inquiry = Inquiry.objects.get(uuid=self.kwargs["pk"]) # pylint: disable=no-member inquiry.user = client inquiry.contact = contact inquiry.save() # Payment gateway = self.request.POST.get("gateway").lower() if gateway == "paypal": handler = PaypalPayment elif gateway == "dummy" and settings.DEBUG: handler = DummyPayment elif gateway == "klarna": handler = KlarnaPayment else: handler = StripePayment payment = handler.objects.create(invoice=inquiry) return payment.start()