IdHub/promotion/views.py

120 lines
4 KiB
Python
Raw Normal View History

2023-12-07 17:10:04 +00:00
import json
2023-12-04 16:12:39 +00:00
from django.views.generic.edit import View, FormView
2023-12-07 17:10:04 +00:00
from django.shortcuts import redirect
2023-12-04 16:12:39 +00:00
from django.template.loader import get_template
from django.urls import reverse_lazy
from django.http import HttpResponse
2023-12-11 11:11:18 +00:00
from oidc4vp.models import Authorization
2023-12-07 17:10:04 +00:00
from promotion.forms import WalletForm, ContractForm
2023-12-04 16:12:39 +00:00
class PromotionView(View):
2023-12-11 11:19:57 +00:00
template_name = "somconnexio_tarifes_mobil.html"
2023-12-04 16:12:39 +00:00
def get(self, request, *args, **kwargs):
self.context = {}
template = get_template(
self.template_name,
).render()
return HttpResponse(template)
2023-12-11 11:11:18 +00:00
class ThanksView(View):
template_name = "somconnexio_thanks.html"
def get(self, request, *args, **kwargs):
self.context = {}
template = get_template(
self.template_name,
).render()
return HttpResponse(template)
2023-12-04 16:12:39 +00:00
2023-12-11 11:11:18 +00:00
class ContractView(FormView):
2023-12-07 17:10:04 +00:00
template_name = "somconnexio_contract.html"
promotion = None
2023-12-11 11:11:18 +00:00
vp_token = None
2023-12-07 17:10:04 +00:00
authorization = None
form_class = ContractForm
2023-12-11 11:11:18 +00:00
success_url = reverse_lazy('promotion:thanks')
def get_context_data(self, **kwargs):
# import pdb; pdb.set_trace()
self.context = super().get_context_data(**kwargs)
2023-12-07 17:10:04 +00:00
code = self.request.GET.get("code")
self.get_discount(code)
2023-12-11 11:11:18 +00:00
self.context.update({
2023-12-07 17:10:04 +00:00
"promotion": self.promotion,
2023-12-11 11:11:18 +00:00
"verificable_presentation": self.vp_token,
"sim": 10.0,
"mensual": 15.0,
"total": 25.0
})
if self.promotion:
self.context['sim'] = self.context.get_discount(self.context["sim"])
self.context['mensual'] = self.context.get_discount(self.context["mensual"])
self.context['total'] = self.context.get_discount(self.context["total"])
return self.context
2023-12-07 17:10:04 +00:00
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
2023-12-11 11:11:18 +00:00
if not self.vp_token:
return kwargs
2023-12-07 17:10:04 +00:00
self.vp_token.get_user_info()
kwargs['verificable_presentation'] = self.vp_token
kwargs["nif"] = self.vp_token.user_info.get("nif", '')
kwargs["name"] = self.vp_token.user_info.get("name", '')
kwargs["first_last_name"] = self.vp_token.user_info.get("first_last_name", '')
kwargs["second_last_name"] = self.vp_token.user_info.get("second_last_name", '')
kwargs["email"] = self.vp_token.user_info.get("email", '')
kwargs["email_repeat"] = self.vp_token.user_info.get("email", '')
kwargs["telephone"] = self.vp_token.user_info.get("telephone", '')
kwargs["birthday"] = self.vp_token.user_info.get("birthday", '')
kwargs["gen"] = self.vp_token.user_info.get("gen", '')
kwargs["lang"] = self.vp_token.user_info.get("lang", '')
return kwargs
def form_valid(self, form):
2023-12-11 11:19:57 +00:00
return super().form_valid(form)
2023-12-07 17:10:04 +00:00
def get_discount(self, code):
2023-12-11 11:11:18 +00:00
if not code:
return
2023-12-07 17:10:04 +00:00
self.authorization = Authorization.objects.filter(
code=code,
code_unused=False
).first()
if self.authorization:
if self.authorization.promotions:
self.promotion = self.authorization.promotionsp[-1]
if self.authorization.vp_tokens:
self.vp_tokens = self.authorization.vp_tokens[-1]
class SelectWalletView(FormView):
template_name = "select_wallet.html"
form_class = WalletForm
success_url = reverse_lazy('promotion:select_wallet')
# def get(self, request, *args, **kwargs):
# self.context = {'form': fo}
# template = get_template(
# self.template_name,
# # context
# ).render()
# return HttpResponse(template)
# def post(self, request, *args, **kwargs):
# super().post(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['presentation_definition'] = json.dumps(["MemberShipCard"])
return kwargs
def form_valid(self, form):
url = form.save()
return redirect(url)