JourneyJoker/auction/views.py

119 lines
4.4 KiB
Python
Raw Permalink 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
2020-04-08 14:07:43 +00:00
from auction.forms import PostPaymentForm, InquiryForm
2020-01-26 10:53:25 +00:00
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
2020-04-08 14:07:43 +00:00
class InquiryView(FormView):
form_class = InquiryForm
2020-01-26 10:53:25 +00:00
def get(self, request, *args, **kwargs):
return redirect(reverse_lazy("frontend:index"))
def form_invalid(self, form):
2020-04-08 13:06:24 +00:00
print(repr(form.errors))
2020-04-13 10:03:57 +00:00
return redirect(reverse_lazy("frontend:index") + "?invalid=true")
2020-01-26 10:53:25 +00:00
def form_valid(self, form):
try:
2020-04-08 14:07:43 +00:00
user = ClientProfile.objects.get(user=self.request.user)
except:
user = None
2020-01-26 10:53:25 +00:00
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
2020-04-08 14:07:43 +00:00
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())
2020-01-26 10:53:25 +00:00
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)
2020-04-08 13:06:24 +00:00
return payment.start()