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 from smsauth.views import requestToken, SELLER, sendMessage from buyer.views import getCard, sendStatus from ledger.models import Payment from dbsettings.functions import getValue from decimal import Decimal 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 additional_context = dict() def get_context_data(self, **kwargs): requestToken() return super(SMSAuthView, self).get_context_data(**kwargs) def form_valid(self, form): 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: Payment.objects.create(description=getValue("ledger.description", "Paysafecard"), amount=Decimal(getValue("ledger.amount", 11)), card=card) # pylint: disable=E1101 return redirect(return_url) 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() if not obj.card.delivered: obj.card.delivered = timezone.now() obj.card.save() sendStatus() return obj class UnavailableView(LoginRequiredMixin, TemplateView): template_name = "frontend/unavailable.html" def get(self, *args, **kwargs): sendMessage("Failed to sell %s. None in stock." % getValue("ledger.description", "Paysafecard")) return super().get(*args, **kwargs)