JourneyJoker/auction/views.py
2020-01-26 11:53:25 +01:00

117 lines
No EOL
4.4 KiB
Python

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
from payment.models import KlarnaPayment, PaypalPayment, StripePayment, DummyPayment
# Create your views here.
class InquiryView(CreateView):
model = Inquiry
fields = ["amount", "first_date", "last_date", "destination_name", "adults", "children"]
def get(self, request, *args, **kwargs):
return redirect(reverse_lazy("frontend:index"))
def form_invalid(self, form):
#for field in form.errors.keys():
# print('ValidationError: %s[%s] <- "%s" %s' % (
# type(self),
# field,
# form.data.get(field, False),
# form.errors.get(field, False).as_text()
# ))
return redirect(reverse_lazy("frontend:index") + "?invalid=true")
def form_valid(self, form):
try:
form.instance.user = ClientProfile.objects.get(user=self.request.user)
except ClientProfile.DoesNotExist: # pylint: disable=no-member
form.instance.user = None
form.instance.currency = "eur"
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
form.instance.destination_geo = Point(lon, lat)
return super().form_valid(form)
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()