add formular for required credential

This commit is contained in:
Cayo Puigdefabregas 2023-11-02 14:17:07 +01:00
parent f3a971a656
commit 60efbf4a25
6 changed files with 96 additions and 71 deletions

View File

@ -54,10 +54,9 @@ class VerificableCredential(models.Model):
""" """
class Status(models.IntegerChoices): class Status(models.IntegerChoices):
ENABLED = 1, _("Enabled") ENABLED = 1, _("Enabled")
REQUESTED = 2, _("Requested") ISSUED = 2, _("Issued")
ISSUED = 3, _("Issued") REVOKED = 3, _("Revoked")
REVOKED = 4, _("Revoked") EXPIRED = 4, _("Expired")
EXPIRED = 6, _("Expired")
id_string = models.CharField(max_length=250) id_string = models.CharField(max_length=250)
verified = models.BooleanField() verified = models.BooleanField()
@ -94,7 +93,10 @@ class VerificableCredential(models.Model):
def get_datas(self): def get_datas(self):
data = json.loads(self.data).get('instance').items() data = json.loads(self.data).get('instance').items()
return data 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): class VCTemplate(models.Model):
wkit_template_id = models.CharField(max_length=250) wkit_template_id = models.CharField(max_length=250)

View File

@ -6,34 +6,29 @@
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </h3>
<div class="row mt-5"> {% load django_bootstrap5 %}
<div class="col"> <form role="form" method="post">
<div class="table-responsive"> {% csrf_token %}
<table class="table table-striped table-sm"> {% if form.errors %}
<thead> <div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<tr> <div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Type' %}</button></th> <div class="message">
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Details' %}</button></th> {% for field, error in form.errors.items %}
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Enabled' %}</button></th> {{ error }}<br />
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for f in credentials.all %}
<tr style="font-size:15px;">
<td>{{ f.type }}</td>
<td>{{ f.description }}</td>
<td>{{ f.created_on }}</td>
<td>
<a href="{% url 'idhub:user_credential_request' f.id %}" class="btn btn-green-user">
{% trans 'Request' %}
</a>
</td>
</tr>
{% endfor %} {% endfor %}
</tbody> <button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</table>
</div>
</div> </div>
</div> </div>
{% endif %}
<div class="row">
<div class="col-sm-4">
{% bootstrap_form form %}
</div>
</div>
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'idhub:user_credentials' %}">{% trans "Cancel" %}</a>
<input class="btn btn-green-user" type="submit" name="submit" value="{% trans 'Request' %}" />
</div>
</form>
{% endblock %} {% endblock %}

View File

@ -26,8 +26,8 @@
</div> </div>
</div> </div>
<div class="form-actions-no-box"> <div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'idhub:user_dids' %}">{% translate "Cancel" %}</a> <a class="btn btn-grey" href="{% url 'idhub:user_dids' %}">{% trans "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" /> <input class="btn btn-green-admin" type="submit" name="submit" value="{% trans 'Save' %}" />
</div> </div>
</form> </form>

View File

@ -84,9 +84,6 @@ urlpatterns = [
path('user/credentials/request/', path('user/credentials/request/',
views_user.UserCredentialsRequestView.as_view(), views_user.UserCredentialsRequestView.as_view(),
name='user_credentials_request'), name='user_credentials_request'),
path('user/credentials/<int:pk>/request/',
views_user.UserCredentialRequestView.as_view(),
name='user_credential_request'),
path('user/credentials_presentation/', path('user/credentials_presentation/',
views_user.UserCredentialsPresentationView.as_view(), views_user.UserCredentialsPresentationView.as_view(),
name='user_credentials_presentation'), name='user_credentials_presentation'),

View File

@ -1,5 +1,6 @@
from django import forms from django import forms
from idhub_auth.models import User from idhub_auth.models import User
from idhub.models import DID, VerificableCredential
class ProfileForm(forms.ModelForm): class ProfileForm(forms.ModelForm):
@ -8,3 +9,44 @@ class ProfileForm(forms.ModelForm):
class Meta: class Meta:
model = User model = User
fields = ('first_name', 'last_name', 'email') 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

View File

@ -1,14 +1,19 @@
import logging import logging
from django.utils.translation import gettext_lazy as _ 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.views.generic.base import TemplateView
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib import messages from django.contrib import messages
from apiregiter import iota from apiregiter import iota
from idhub.user.forms import ProfileForm from idhub.user.forms import ProfileForm, RequestCredentialForm
from idhub.mixins import UserView from idhub.mixins import UserView
from idhub.models import DID, VerificableCredential from idhub.models import DID, VerificableCredential
@ -104,41 +109,25 @@ class UserCredentialJsonView(MyWallet, TemplateView):
return response return response
class UserCredentialsRequestView(MyWallet, TemplateView): class UserCredentialsRequestView(MyWallet, FormView):
template_name = "idhub/user/credentials_request.html" template_name = "idhub/user/credentials_request.html"
subtitle = _('Credentials request') subtitle = _('Credentials request')
icon = 'bi bi-patch-check-fill' icon = 'bi bi-patch-check-fill'
form_class = RequestCredentialForm
success_url = reverse_lazy('idhub:user_credentials')
def get_context_data(self, **kwargs): def get_form_kwargs(self):
context = super().get_context_data(**kwargs) kwargs = super().get_form_kwargs()
creds = VerificableCredential.objects.filter( kwargs['user'] = self.request.user
user=self.request.user, return kwargs
status=VerificableCredential.Status.ENABLE
)
context.update({
'credentials': creds,
})
return context
def form_valid(self, form):
class UserCredentialRequestView(MyWallet, TemplateView): cred = form.save()
success_url = reverse_lazy('idhub:user_credentials_request') if cred:
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()
messages.success(self.request, _("The credential was required successfully!")) 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): class UserCredentialsPresentationView(MyWallet, TemplateView):