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 }} +
+ | + | + | + | + | + |
---|---|---|---|---|---|
{{ f.type }} | +{{ f.description }} | +{{ f.issue_on }} | +{{ f.get_status }} | +{{ f.user.email }} | ++ |