promotion page

This commit is contained in:
Cayo Puigdefabregas 2023-12-04 17:12:39 +01:00
parent 39b89bebe0
commit 753e1f6d1a
13 changed files with 2439 additions and 1 deletions

0
promotion/__init__.py Normal file
View File

3
promotion/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
promotion/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class PromotionConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'promotion'

59
promotion/forms.py Normal file
View File

@ -0,0 +1,59 @@
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
from oidc4vp.models import Organization
class WalletForm(forms.Form):
def __init__(self, *args, **kwargs):
self.presentation_definition = kwargs.pop('presentation_definition', [])
reg = r'({})'.format('|'.join(self.presentation_definition))
self.credentials = self.user.vcredentials.filter(
schema__type__iregex=reg
)
super().__init__(*args, **kwargs)
for vp in self.presentation_definition:
vp = vp.lower()
choices = [
(str(x.id), x.schema.type.lower()) for x in self.credentials.filter(
schema__type__iexact=vp)
]
self.fields[vp.lower()] = forms.ChoiceField(
widget=forms.RadioSelect,
choices=choices
)
def clean(self):
data = super().clean()
self.list_credentials = []
for c in self.credentials:
if str(c.id) == data.get(c.schema.type.lower()):
if c.status is not c.Status.ISSUED.value or not c.data:
txt = _('There are some problems with this credentials')
raise ValidationError(txt)
self.list_credentials.append(c)
return data
def save(self, commit=True):
if not self.list_credentials:
return
self.get_verificable_presentation()
if commit:
return self.org.send(self.vp)
return

View File

3
promotion/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

3
promotion/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

14
promotion/urls.py Normal file
View File

@ -0,0 +1,14 @@
from django.urls import path, reverse_lazy
from promotion import views
app_name = 'promotion'
urlpatterns = [
path('', views.PromotionView.as_view(),
name="show_promotion"),
path('select_wallet', views.SelectWalletView.as_view(),
name="select_wallet"),
]

30
promotion/views.py Normal file
View File

@ -0,0 +1,30 @@
from django.views.generic.edit import View, FormView
from django.template.loader import get_template
from django.urls import reverse_lazy
from django.http import HttpResponse
from promotion.forms import WalletForm
class PromotionView(View):
template_name = "somconnexio.tarifes-mobil.html"
def get(self, request, *args, **kwargs):
self.context = {}
template = get_template(
self.template_name,
).render()
return HttpResponse(template)
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 = {}
template = get_template(
self.template_name,
# context
).render()
return HttpResponse(template)

View File

@ -73,7 +73,8 @@ INSTALLED_APPS = [
'django_tables2', 'django_tables2',
'idhub_auth', 'idhub_auth',
'oidc4vp', 'oidc4vp',
'idhub' 'idhub',
'promotion'
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

@ -25,4 +25,5 @@ urlpatterns = [
# path('django-admin/', admin.site.urls), # path('django-admin/', admin.site.urls),
path('', include('idhub.urls')), path('', include('idhub.urls')),
path('oidc4vp/', include('oidc4vp.urls')), path('oidc4vp/', include('oidc4vp.urls')),
path('promotion/', include('promotion.urls')),
] ]