IdHub/promotion/views.py

117 lines
4.0 KiB
Python
Raw Normal View History

2023-12-07 17:10:04 +00:00
import json
2024-01-24 16:12:46 +00:00
from django.conf import settings
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):
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:
2023-12-11 17:40:37 +00:00
self.context['sim'] = self.promotion.get_discount(self.context["sim"])
self.context['mensual'] = self.promotion.get_discount(self.context["mensual"])
self.context['total'] = self.promotion.get_discount(self.context["total"])
if self.vp_token:
self.context['verificable_presentation'] = self.vp_token
2023-12-11 11:11:18 +00:00
return self.context
2023-12-07 17:10:04 +00:00
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
2023-12-11 17:40:37 +00:00
code = self.request.GET.get("code")
self.get_discount(code)
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()
2024-02-07 10:46:08 +00:00
kwargs['initial']["nif"] = self.vp_token.user_info.get("identityNumber", '')
kwargs['initial']["name"] = self.vp_token.user_info.get("firstName", '')
kwargs['initial']["first_last_name"] = self.vp_token.user_info.get("lastName", '')
2023-12-11 17:40:37 +00:00
kwargs['initial']["second_last_name"] = self.vp_token.user_info.get("second_last_name", '')
kwargs['initial']["email"] = self.vp_token.user_info.get("email", '')
kwargs['initial']["email_repeat"] = self.vp_token.user_info.get("email", '')
kwargs['initial']["telephone"] = self.vp_token.user_info.get("telephone", '')
kwargs['initial']["birthday"] = self.vp_token.user_info.get("birthday", '')
kwargs['initial']["gen"] = self.vp_token.user_info.get("gen", '')
kwargs['initial']["lang"] = self.vp_token.user_info.get("lang", '')
2023-12-07 17:10:04 +00:00
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-11 17:40:37 +00:00
if self.authorization:
return
2023-12-11 11:11:18 +00:00
2023-12-07 17:10:04 +00:00
self.authorization = Authorization.objects.filter(
code=code,
2023-12-11 17:40:37 +00:00
code_used=False
2023-12-07 17:10:04 +00:00
).first()
if self.authorization:
2023-12-11 17:40:37 +00:00
if self.authorization.promotions.exists():
self.promotion = self.authorization.promotions.all()[0]
if self.authorization.vp_tokens.exists():
self.vp_token = self.authorization.vp_tokens.all()[0]
2023-12-07 17:10:04 +00:00
class SelectWalletView(FormView):
template_name = "select_wallet.html"
form_class = WalletForm
success_url = reverse_lazy('promotion:select_wallet')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
2024-01-24 16:12:46 +00:00
kwargs['presentation_definition'] = json.dumps(settings.SUPPORTED_CREDENTIALS)
2023-12-07 17:10:04 +00:00
return kwargs
def form_valid(self, form):
url = form.save()
return redirect(url)