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):
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)

View file

@ -6,34 +6,29 @@
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
<div class="row mt-5">
<div class="col">
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Type' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Details' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Enabled' %}</button></th>
<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 %}
</tbody>
</table>
</div>
{% load django_bootstrap5 %}
<form role="form" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</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 %}

View file

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

View file

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

View file

@ -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')
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
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):