From 98aecad77bfe0e351e457e34692f9deada187e90 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 1 Dec 2023 17:50:30 +0100 Subject: [PATCH] add type in schema model and fix description in credential --- idhub/admin/views.py | 15 ++++-- idhub/migrations/0001_initial.py | 66 ++++++++++++++------------- idhub/models.py | 23 +++------- idhub_auth/migrations/0001_initial.py | 2 +- 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/idhub/admin/views.py b/idhub/admin/views.py index f8fd6d0..0693fb2 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -772,11 +772,14 @@ class SchemasNewView(SchemasMix): return try: data = f.read().decode('utf-8') - assert credtools.validate_schema(json.loads(data)) + ldata = json.loads(data) + assert credtools.validate_schema(ldata) + name = ldata.get('name') + assert name except Exception: messages.error(self.request, _('This is not a valid schema!')) return - schema = Schemas.objects.create(file_schema=file_name, data=data) + schema = Schemas.objects.create(file_schema=file_name, data=data, type=name) schema.save() return schema @@ -817,11 +820,15 @@ class SchemasImportAddView(SchemasMix): def create_schema(self, file_name): data = self.open_file(file_name) try: - json.loads(data) + ldata = json.loads(data) + assert credtools.validate_schema(ldata) + name = ldata.get('name') + assert name + except Exception: messages.error(self.request, _('This is not a valid schema!')) return - schema = Schemas.objects.create(file_schema=file_name, data=data) + schema = Schemas.objects.create(file_schema=file_name, data=data, type=name) schema.save() return schema diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index b4d6ac7..a39a956 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-11-15 09:58 +# Generated by Django 4.2.5 on 2023-12-01 16:40 from django.conf import settings from django.db import migrations, models @@ -111,6 +111,7 @@ class Migration(migrations.Migration): verbose_name='ID', ), ), + ('type', models.CharField(max_length=250)), ('file_schema', models.CharField(max_length=250)), ('data', models.TextField()), ('created_at', models.DateTimeField(auto_now=True)), @@ -274,36 +275,39 @@ class Migration(migrations.Migration): 'type', models.PositiveSmallIntegerField( choices=[ - (1, 'EV_USR_REGISTERED'), - (2, 'EV_USR_WELCOME'), - (3, 'EV_DATA_UPDATE_REQUESTED_BY_USER'), - (4, 'EV_DATA_UPDATE_REQUESTED'), - (5, 'EV_USR_UPDATED_BY_ADMIN'), - (6, 'EV_USR_UPDATED'), - (7, 'EV_USR_DELETED_BY_ADMIN'), - (8, 'EV_DID_CREATED_BY_USER'), - (9, 'EV_DID_CREATED'), - (10, 'EV_DID_DELETED'), - (11, 'EV_CREDENTIAL_DELETED_BY_ADMIN'), - (12, 'EV_CREDENTIAL_DELETED'), - (13, 'EV_CREDENTIAL_ISSUED_FOR_USER'), - (14, 'EV_CREDENTIAL_ISSUED'), - (15, 'EV_CREDENTIAL_PRESENTED_BY_USER'), - (16, 'EV_CREDENTIAL_PRESENTED'), - (17, 'EV_CREDENTIAL_ENABLED'), - (18, 'EV_CREDENTIAL_CAN_BE_REQUESTED'), - (19, 'EV_CREDENTIAL_REVOKED_BY_ADMIN'), - (20, 'EV_CREDENTIAL_REVOKED'), - (21, 'EV_ROLE_CREATED_BY_ADMIN'), - (22, 'EV_ROLE_MODIFIED_BY_ADMIN'), - (23, 'EV_ROLE_DELETED_BY_ADMIN'), - (24, 'EV_SERVICE_CREATED_BY_ADMIN'), - (25, 'EV_SERVICE_MODIFIED_BY_ADMIN'), - (26, 'EV_SERVICE_DELETED_BY_ADMIN'), - (27, 'EV_ORG_DID_CREATED_BY_ADMIN'), - (28, 'EV_ORG_DID_DELETED_BY_ADMIN'), - (29, 'EV_USR_DEACTIVATED_BY_ADMIN'), - (30, 'EV_USR_ACTIVATED_BY_ADMIN'), + (1, 'User registered'), + (2, 'User welcomed'), + (3, 'Data update requested by user'), + ( + 4, + 'Data update requested. Pending approval by administrator', + ), + (5, "User's data updated by admin"), + (6, 'Your data updated by admin'), + (7, 'User deactivated by admin'), + (8, 'DID created by user'), + (9, 'DID created'), + (10, 'DID deleted'), + (11, 'Credential deleted by user'), + (12, 'Credential deleted'), + (13, 'Credential issued for user'), + (14, 'Credential issued'), + (15, 'Credential presented by user'), + (16, 'Credential presented'), + (17, 'Credential enabled'), + (18, 'Credential available'), + (19, 'Credential revoked by admin'), + (20, 'Credential revoked'), + (21, 'Role created by admin'), + (22, 'Role modified by admin'), + (23, 'Role deleted by admin'), + (24, 'Service created by admin'), + (25, 'Service modified by admin'), + (26, 'Service deleted by admin'), + (27, 'Organisational DID created by admin'), + (28, 'Organisational DID deleted by admin'), + (29, 'User deactivated'), + (30, 'User activated'), ] ), ), diff --git a/idhub/models.py b/idhub/models.py index 53f8186..50deb54 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -432,6 +432,7 @@ class DID(models.Model): class Schemas(models.Model): + type = models.CharField(max_length=250) file_schema = models.CharField(max_length=250) data = models.TextField() created_at = models.DateTimeField(auto_now=True) @@ -486,23 +487,11 @@ class VerificableCredential(models.Model): related_name='vcredentials', ) - @property - def get_schema(self): - if not self.data: - return {} - return json.loads(self.data) - def type(self): - if self.data: - return self.get_schema.get('type')[-1] - - return self.schema.name() + return self.schema.type def description(self): - if not self.data: - return self.schema.description() - - for des in self.get_schema.get('description', []): + for des in json.loads(self.render()).get('description', []): if settings.LANGUAGE_CODE == des.get('lang'): return des.get('value', '') return '' @@ -528,8 +517,10 @@ class VerificableCredential(models.Model): def get_context(self): d = json.loads(self.csv_data) - format = "%Y-%m-%dT%H:%M:%SZ" - issuance_date = self.issued_on.strftime(format) + issuance_date = '' + if self.issued_on: + format = "%Y-%m-%dT%H:%M:%SZ" + issuance_date = self.issued_on.strftime(format) context = { 'vc_id': self.id, diff --git a/idhub_auth/migrations/0001_initial.py b/idhub_auth/migrations/0001_initial.py index d40f0a4..26dbc6c 100644 --- a/idhub_auth/migrations/0001_initial.py +++ b/idhub_auth/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-11-15 09:58 +# Generated by Django 4.2.5 on 2023-12-01 16:40 from django.db import migrations, models