IdHub/promotion/forms.py

61 lines
1.7 KiB
Python
Raw Permalink Normal View History

2023-12-04 16:12:39 +00:00
from django import forms
from django.conf import settings
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 = [
2024-03-04 08:44:53 +00:00
(x.id, x.name) for x in Organization.objects.exclude(
domain=settings.DOMAIN
2024-06-06 11:47:37 +00:00
)
2023-12-07 17:10:04 +00:00
]
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()
2024-06-06 11:47:37 +00:00
return
2023-12-07 17:10:04 +00:00
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()