IdHub/promotion/forms.py

69 lines
1.9 KiB
Python
Raw Normal View History

2023-12-04 16:12:39 +00:00
import json
import requests
from django import forms
from django.conf import settings
from django.template.loader import get_template
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from utils.idhub_ssikit import create_verifiable_presentation
2023-12-07 17:10:04 +00:00
from oidc4vp.models import Organization, Authorization
2023-12-11 12:41:19 +00:00
from promotion.models import Promotion
2023-12-04 16:12:39 +00:00
class WalletForm(forms.Form):
2024-02-02 08:18:15 +00:00
organization = forms.ChoiceField(
choices=[],
widget=forms.Select(attrs={'class': 'form-select'})
)
2023-12-04 16:12:39 +00:00
def __init__(self, *args, **kwargs):
self.presentation_definition = kwargs.pop('presentation_definition', [])
super().__init__(*args, **kwargs)
2023-12-07 17:10:04 +00:00
self.fields['organization'].choices = [
(x.id, x.name) for x in Organization.objects.filter()
if x.response_uri != settings.RESPONSE_URI
]
2023-12-04 16:12:39 +00:00
def save(self, commit=True):
2023-12-07 17:10:04 +00:00
self.org = Organization.objects.filter(
id=self.data['organization']
)
if not self.org.exists():
2023-12-04 16:12:39 +00:00
return
2023-12-07 17:10:04 +00:00
self.org = self.org[0]
2023-12-04 16:12:39 +00:00
2023-12-07 17:10:04 +00:00
self.authorization = Authorization(
organization=self.org,
presentation_definition=self.presentation_definition,
)
self.promotion = Promotion(
discount = Promotion.Types.VULNERABLE.value,
authorize = self.authorization
)
2023-12-04 16:12:39 +00:00
2023-12-07 17:10:04 +00:00
if commit:
self.authorization.save()
self.promotion.save()
return self.authorization.authorize()
return
class ContractForm(forms.Form):
nif = forms.CharField()
name = forms.CharField()
first_last_name = forms.CharField()
second_last_name = forms.CharField()
email = forms.CharField()
email_repeat = forms.CharField()
telephone = forms.CharField()
birthday = forms.CharField()
gen = forms.CharField()
lang = forms.CharField()
2023-12-11 11:19:57 +00:00