JourneyJoker/auction/views.py

117 lines
4.4 KiB
Python
Raw Normal View History

2020-01-26 10:53:25 +00:00
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
2020-01-22 17:32:13 +00:00
from auction.models import Inquiry
2020-01-26 10:53:25 +00:00
from profiles.models import ClientProfile, ContactProfile
from auction.forms import PostPaymentForm
from payment.models import KlarnaPayment, PaypalPayment, StripePayment, DummyPayment
2019-12-24 13:41:49 +00:00
# Create your views here.
2020-01-22 17:32:13 +00:00
class InquiryView(CreateView):
2020-01-26 10:53:25 +00:00
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()