From 6db0090bfedb4a211a6c618e651b2d015f44afb4 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 26 Oct 2023 13:33:13 +0200 Subject: [PATCH] add view list of credentials --- examples/import_credentials.csv | 2 + idhub/admin/views.py | 20 +++++++- idhub/migrations/0001_initial.py | 16 +++++- idhub/models.py | 44 +++++++++++++++- idhub/templates/idhub/admin/credentials.html | 30 +++++++++++ idhub/templates/idhub/admin/schemas.html | 4 ++ .../idhub/admin/wallet_identities.html | 51 +++++++++++++++++++ idhub/templates/idhub/base_admin.html | 10 ---- idhub_auth/migrations/0001_initial.py | 2 +- initial_datas.py | 32 ++++++++++++ schemas/member-schema.json | 21 ++++++++ 11 files changed, 218 insertions(+), 14 deletions(-) create mode 100644 examples/import_credentials.csv create mode 100644 initial_datas.py create mode 100644 schemas/member-schema.json diff --git a/examples/import_credentials.csv b/examples/import_credentials.csv new file mode 100644 index 0000000..4a06714 --- /dev/null +++ b/examples/import_credentials.csv @@ -0,0 +1,2 @@ +name email membershipType +Pepe user1@example.org individual diff --git a/idhub/admin/views.py b/idhub/admin/views.py index b78c3f8..480895d 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -1,6 +1,7 @@ import os import csv import json +import copy import logging import pandas as pd from pathlib import Path @@ -19,6 +20,7 @@ from idhub_auth.models import User from idhub.mixins import AdminView from idhub.email.views import NotifyActivateUserByEmail from idhub.models import ( + DID, File_datas, Membership, Rol, @@ -419,6 +421,13 @@ class AdminCredentialsView(Credentials): subtitle = _('Credentials list') icon = '' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'credentials': VerifiableCredential.objects, + }) + return context + class AdminIssueCredentialsView(Credentials): template_name = "idhub/admin/issue_credentials.html" @@ -438,6 +447,13 @@ class AdminWalletIdentitiesView(Credentials): icon = 'bi bi-patch-check-fill' wallet = True + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'dids': DID.objects, + }) + return context + class AdminWalletCredentialsView(Credentials): template_name = "idhub/admin/wallet_credentials.html" @@ -693,9 +709,11 @@ class AdminImportStep3View(ImportExport): return user.first() def create_credential(self, user, row): + d = copy.copy(self.json_schema) + d['instance'] = row return VerifiableCredential.objects.create( verified=False, user=user, - data=json.dumps(row) + data=json.dumps(d) ) diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index ffe4a09..bb9874b 100644 --- a/idhub/migrations/0001_initial.py +++ b/idhub/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-10-25 15:47 +# Generated by Django 4.2.5 on 2023-10-26 11:29 from django.conf import settings from django.db import migrations, models @@ -110,9 +110,22 @@ class Migration(migrations.Migration): ('id_string', models.CharField(max_length=250)), ('verified', models.BooleanField()), ('created_on', models.DateTimeField(auto_now=True)), + ('issuer_on', models.DateTimeField(null=True)), ('did_issuer', models.CharField(max_length=250)), ('did_subject', models.CharField(max_length=250)), ('data', models.TextField()), + ( + 'status', + models.PositiveSmallIntegerField( + choices=[ + (1, 'Enable'), + (2, 'Issued'), + (3, 'Revoked'), + (4, 'Expired'), + ], + default=1, + ), + ), ( 'user', models.ForeignKey( @@ -217,6 +230,7 @@ class Migration(migrations.Migration): ( 'user', models.ForeignKey( + null=True, on_delete=django.db.models.deletion.CASCADE, related_name='dids', to=settings.AUTH_USER_MODEL, diff --git a/idhub/models.py b/idhub/models.py index f64a126..774426b 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -1,3 +1,4 @@ +import json from django.db import models from django.utils.translation import gettext_lazy as _ from idhub_auth.models import User @@ -26,6 +27,7 @@ class DID(models.Model): User, on_delete=models.CASCADE, related_name='dids', + null=True, ) # kind = "KEY|WEB" @@ -35,20 +37,60 @@ class Schemas(models.Model): data = models.TextField() created_at = models.DateTimeField(auto_now=True) + @property + def get_schema(self): + if not self.data: + return {} + return json.loads(self.data) + + def name(self): + return self.get_schema.get('name', '') + + def description(self): + return self.get_schema.get('description', '') + class VerifiableCredential(models.Model): + """ + Definition of Verificable Credentials + """ + class Status(models.IntegerChoices): + ENABLE = 1, _("Enable") + ISSUED = 2, _("Issued") + REVOKED = 3, _("Revoked") + EXPIRED = 4, _("Expired") + id_string = models.CharField(max_length=250) verified = models.BooleanField() created_on = models.DateTimeField(auto_now=True) + issuer_on = models.DateTimeField(null=True) did_issuer = models.CharField(max_length=250) did_subject = models.CharField(max_length=250) + data = models.TextField() + status = models.PositiveSmallIntegerField( + choices=Status.choices, + default=Status.ENABLE + ) user = models.ForeignKey( User, on_delete=models.CASCADE, related_name='vcredentials', ) - data = models.TextField() + @property + def get_schema(self): + if not self.data: + return {} + return json.loads(self.data) + + def type(self): + return self.get_schema.get('name', '') + + def description(self): + return self.get_schema.get('description', '') + + def get_status(self): + return self.Status(self.status).label class VCTemplate(models.Model): wkit_template_id = models.CharField(max_length=250) diff --git a/idhub/templates/idhub/admin/credentials.html b/idhub/templates/idhub/admin/credentials.html index f5849fd..81a1abb 100644 --- a/idhub/templates/idhub/admin/credentials.html +++ b/idhub/templates/idhub/admin/credentials.html @@ -6,4 +6,34 @@ {{ subtitle }} +
+
+
+ + + + + + + + + + + + + {% for f in credentials.all %} + + + + + + + + + {% endfor %} + +
{{ f.type }}{{ f.description }}{{ f.issue_on }}{{ f.get_status }}{{ f.user.email }}
+
+
+
{% endblock %} diff --git a/idhub/templates/idhub/admin/schemas.html b/idhub/templates/idhub/admin/schemas.html index 8b799fe..2aa2e88 100644 --- a/idhub/templates/idhub/admin/schemas.html +++ b/idhub/templates/idhub/admin/schemas.html @@ -14,6 +14,8 @@ + + @@ -23,6 +25,8 @@ {{ schema.created_at }} {{ schema.file_schema }} + {{ schema.name }} + {{ schema.description }} {% trans 'View' %} diff --git a/idhub/templates/idhub/admin/wallet_identities.html b/idhub/templates/idhub/admin/wallet_identities.html index f5849fd..0a1295b 100644 --- a/idhub/templates/idhub/admin/wallet_identities.html +++ b/idhub/templates/idhub/admin/wallet_identities.html @@ -6,4 +6,55 @@ {{ subtitle }} +
+
+
+ + + + + + + + + + + + {% for d in dids.all %} + + + + + + + + {% endfor %} + +
{{ d.created_at }}{{ d.label }}{{ d.id }}
+ +
+
+
+ +{% for d in dids.all %} + +{% endfor %} {% endblock %} diff --git a/idhub/templates/idhub/base_admin.html b/idhub/templates/idhub/base_admin.html index e496825..bece139 100644 --- a/idhub/templates/idhub/base_admin.html +++ b/idhub/templates/idhub/base_admin.html @@ -117,16 +117,6 @@ Credentials list - -