From 60efbf4a25efda783b8eba78a48c4f056da4fa34 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 2 Nov 2023 14:17:07 +0100 Subject: [PATCH] add formular for required credential --- idhub/models.py | 12 +++-- .../idhub/user/credentials_request.html | 51 ++++++++---------- idhub/templates/idhub/user/did_register.html | 4 +- idhub/urls.py | 3 -- idhub/user/forms.py | 44 ++++++++++++++- idhub/user/views.py | 53 ++++++++----------- 6 files changed, 96 insertions(+), 71 deletions(-) diff --git a/idhub/models.py b/idhub/models.py index 2d8f755..c558712 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -54,10 +54,9 @@ class VerificableCredential(models.Model): """ class Status(models.IntegerChoices): ENABLED = 1, _("Enabled") - REQUESTED = 2, _("Requested") - ISSUED = 3, _("Issued") - REVOKED = 4, _("Revoked") - EXPIRED = 6, _("Expired") + ISSUED = 2, _("Issued") + REVOKED = 3, _("Revoked") + EXPIRED = 4, _("Expired") id_string = models.CharField(max_length=250) verified = models.BooleanField() @@ -94,7 +93,10 @@ class VerificableCredential(models.Model): def get_datas(self): data = json.loads(self.data).get('instance').items() return data - # import pdb; pdb.set_trace() + + def get_issued(self, did): + self.status = self.Status.ISSUED + self.did_subject = did class VCTemplate(models.Model): wkit_template_id = models.CharField(max_length=250) diff --git a/idhub/templates/idhub/user/credentials_request.html b/idhub/templates/idhub/user/credentials_request.html index a2bf5bb..6dc720d 100644 --- a/idhub/templates/idhub/user/credentials_request.html +++ b/idhub/templates/idhub/user/credentials_request.html @@ -6,34 +6,29 @@ {{ subtitle }} -
-
-
- - - - - - - - - - - {% for f in credentials.all %} - - - - - - - {% endfor %} - -
{{ f.type }}{{ f.description }}{{ f.created_on }} - - {% trans 'Request' %} - -
-
+{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +
+
+ {% bootstrap_form form %} +
+
+ + +
{% endblock %} diff --git a/idhub/templates/idhub/user/did_register.html b/idhub/templates/idhub/user/did_register.html index 019b966..b2e1111 100644 --- a/idhub/templates/idhub/user/did_register.html +++ b/idhub/templates/idhub/user/did_register.html @@ -26,8 +26,8 @@
- {% translate "Cancel" %} - + {% trans "Cancel" %} +
diff --git a/idhub/urls.py b/idhub/urls.py index c861750..eeb6b09 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -84,9 +84,6 @@ urlpatterns = [ path('user/credentials/request/', views_user.UserCredentialsRequestView.as_view(), name='user_credentials_request'), - path('user/credentials//request/', - views_user.UserCredentialRequestView.as_view(), - name='user_credential_request'), path('user/credentials_presentation/', views_user.UserCredentialsPresentationView.as_view(), name='user_credentials_presentation'), diff --git a/idhub/user/forms.py b/idhub/user/forms.py index 1b2dd28..1138eee 100644 --- a/idhub/user/forms.py +++ b/idhub/user/forms.py @@ -1,5 +1,6 @@ from django import forms from idhub_auth.models import User +from idhub.models import DID, VerificableCredential class ProfileForm(forms.ModelForm): @@ -7,4 +8,45 @@ class ProfileForm(forms.ModelForm): class Meta: model = User - fields = ('first_name', 'last_name', 'email') \ No newline at end of file + fields = ('first_name', 'last_name', 'email') + + +class RequestCredentialForm(forms.Form): + did = forms.ChoiceField(choices=[]) + credential = forms.ChoiceField(choices=[]) + + def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user', None) + super().__init__(*args, **kwargs) + self.fields['did'].choices = [ + (x.did, x.label) for x in DID.objects.filter(user=self.user) + ] + self.fields['credential'].choices = [ + (x.id, x.type()) for x in VerificableCredential.objects.filter( + user=self.user, + status=VerificableCredential.Status.ENABLED + ) + ] + + def save(self, commit=True): + did = DID.objects.filter( + user=self.user, + did=self.data['did'] + ) + cred = VerificableCredential.objects.filter( + user=self.user, + id=self.data['credential'] + ) + if not all([cred.exists(), did.exists()]): + return + + did = did[0].did + cred = cred[0] + cred.get_issued(did) + + if commit: + cred.save() + return cred + + return + diff --git a/idhub/user/views.py b/idhub/user/views.py index 444256b..d35159b 100644 --- a/idhub/user/views.py +++ b/idhub/user/views.py @@ -1,14 +1,19 @@ import logging from django.utils.translation import gettext_lazy as _ -from django.views.generic.edit import UpdateView, CreateView, DeleteView +from django.views.generic.edit import ( + UpdateView, + CreateView, + DeleteView, + FormView +) from django.views.generic.base import TemplateView from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.http import HttpResponse from django.contrib import messages from apiregiter import iota -from idhub.user.forms import ProfileForm +from idhub.user.forms import ProfileForm, RequestCredentialForm from idhub.mixins import UserView from idhub.models import DID, VerificableCredential @@ -104,41 +109,25 @@ class UserCredentialJsonView(MyWallet, TemplateView): return response -class UserCredentialsRequestView(MyWallet, TemplateView): +class UserCredentialsRequestView(MyWallet, FormView): template_name = "idhub/user/credentials_request.html" subtitle = _('Credentials request') icon = 'bi bi-patch-check-fill' + form_class = RequestCredentialForm + success_url = reverse_lazy('idhub:user_credentials') - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - creds = VerificableCredential.objects.filter( - user=self.request.user, - status=VerificableCredential.Status.ENABLE - ) - context.update({ - 'credentials': creds, - }) - return context - - -class UserCredentialRequestView(MyWallet, TemplateView): - success_url = reverse_lazy('idhub:user_credentials_request') - - def get(self, request, *args, **kwargs): - pk = kwargs['pk'] - creds = VerificableCredential.objects.filter( - pk=pk, - user=self.request.user, - status=VerificableCredential.Status.ENABLE - ) - if not creds: - messages.error(self.request, _("Not exists the credential!")) - else: - cred = creds[0] - cred.status = VerificableCredential.Status.REQUIRED - cred.save() + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs['user'] = self.request.user + return kwargs + + def form_valid(self, form): + cred = form.save() + if cred: messages.success(self.request, _("The credential was required successfully!")) - return redirect(self.success_url) + else: + messages.error(self.request, _("Not exists the credential!")) + return super().form_valid(form) class UserCredentialsPresentationView(MyWallet, TemplateView):