2019-10-27 15:20:28 +00:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.views.generic import TemplateView, FormView, DetailView
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from frontend.forms import SMSAuthForm
|
|
|
|
from frontend.models import CardURL
|
|
|
|
from django.utils import timezone
|
|
|
|
from datetime import timedelta
|
|
|
|
from django.http import Http404
|
2021-05-24 16:13:26 +00:00
|
|
|
from smsauth.views import requestToken, SELLER, sendMessage
|
2020-02-01 15:05:10 +00:00
|
|
|
from buyer.views import getCard, sendStatus
|
2019-10-27 15:20:28 +00:00
|
|
|
from ledger.models import Payment
|
2021-05-24 12:02:50 +00:00
|
|
|
from dbsettings.functions import getValue
|
|
|
|
from decimal import Decimal
|
2019-10-26 15:25:21 +00:00
|
|
|
|
2019-10-27 15:20:28 +00:00
|
|
|
def makeCardURL(card):
|
|
|
|
return CardURL.objects.create(card=card) # pylint: disable=E1101
|
|
|
|
|
|
|
|
class IndexView(LoginRequiredMixin, TemplateView):
|
|
|
|
template_name = "frontend/index.html"
|
|
|
|
|
|
|
|
class SMSAuthView(LoginRequiredMixin, FormView):
|
|
|
|
template_name = "frontend/form.html"
|
|
|
|
form_class = SMSAuthForm
|
2020-10-24 14:50:04 +00:00
|
|
|
additional_context = dict()
|
2019-10-27 15:20:28 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
requestToken()
|
|
|
|
return super(SMSAuthView, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2020-10-24 14:50:04 +00:00
|
|
|
try:
|
|
|
|
card = Payment.objects.get(id=self.kwargs["id"]).card if self.kwargs.get("id") else getCard() # pylint: disable=E1101
|
|
|
|
return_url = "/card/%s/" % makeCardURL(card).uuid
|
|
|
|
except:
|
|
|
|
return redirect("unavailable")
|
|
|
|
if not card.delivered:
|
2021-05-24 12:02:50 +00:00
|
|
|
Payment.objects.create(description=getValue("ledger.description", "Paysafecard"), amount=Decimal(getValue("ledger.amount", 11)), card=card) # pylint: disable=E1101
|
2020-10-24 14:50:04 +00:00
|
|
|
return redirect(return_url)
|
2019-10-27 15:20:28 +00:00
|
|
|
|
|
|
|
class CardView(LoginRequiredMixin, DetailView):
|
|
|
|
template_name = "frontend/card.html"
|
|
|
|
model = CardURL
|
|
|
|
pk_url_kwarg = "uuid"
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
obj = super(CardView, self).get_object(queryset=queryset)
|
|
|
|
if timezone.now() - timedelta(seconds=300) > obj.created:
|
|
|
|
raise Http404()
|
2020-10-24 14:50:04 +00:00
|
|
|
if not obj.card.delivered:
|
|
|
|
obj.card.delivered = timezone.now()
|
|
|
|
obj.card.save()
|
2020-02-01 15:05:10 +00:00
|
|
|
sendStatus()
|
2019-10-27 15:20:28 +00:00
|
|
|
return obj
|
2020-10-24 14:50:04 +00:00
|
|
|
|
|
|
|
class UnavailableView(LoginRequiredMixin, TemplateView):
|
2021-05-24 12:02:50 +00:00
|
|
|
template_name = "frontend/unavailable.html"
|
2021-05-24 16:13:26 +00:00
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
sendMessage("Failed to sell %s. None in stock." % getValue("ledger.description", "Paysafecard"))
|
|
|
|
return super().get(*args, **kwargs)
|