add type in schema model and fix description in credential

This commit is contained in:
Cayo Puigdefabregas 2023-12-01 17:50:30 +01:00
parent 5eb606c4a4
commit 98aecad77b
4 changed files with 54 additions and 52 deletions

View File

@ -772,11 +772,14 @@ class SchemasNewView(SchemasMix):
return return
try: try:
data = f.read().decode('utf-8') 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: except Exception:
messages.error(self.request, _('This is not a valid schema!')) messages.error(self.request, _('This is not a valid schema!'))
return 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() schema.save()
return schema return schema
@ -817,11 +820,15 @@ class SchemasImportAddView(SchemasMix):
def create_schema(self, file_name): def create_schema(self, file_name):
data = self.open_file(file_name) data = self.open_file(file_name)
try: try:
json.loads(data) ldata = json.loads(data)
assert credtools.validate_schema(ldata)
name = ldata.get('name')
assert name
except Exception: except Exception:
messages.error(self.request, _('This is not a valid schema!')) messages.error(self.request, _('This is not a valid schema!'))
return 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() schema.save()
return schema return schema

View File

@ -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.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -111,6 +111,7 @@ class Migration(migrations.Migration):
verbose_name='ID', verbose_name='ID',
), ),
), ),
('type', models.CharField(max_length=250)),
('file_schema', models.CharField(max_length=250)), ('file_schema', models.CharField(max_length=250)),
('data', models.TextField()), ('data', models.TextField()),
('created_at', models.DateTimeField(auto_now=True)), ('created_at', models.DateTimeField(auto_now=True)),
@ -274,36 +275,39 @@ class Migration(migrations.Migration):
'type', 'type',
models.PositiveSmallIntegerField( models.PositiveSmallIntegerField(
choices=[ choices=[
(1, 'EV_USR_REGISTERED'), (1, 'User registered'),
(2, 'EV_USR_WELCOME'), (2, 'User welcomed'),
(3, 'EV_DATA_UPDATE_REQUESTED_BY_USER'), (3, 'Data update requested by user'),
(4, 'EV_DATA_UPDATE_REQUESTED'), (
(5, 'EV_USR_UPDATED_BY_ADMIN'), 4,
(6, 'EV_USR_UPDATED'), 'Data update requested. Pending approval by administrator',
(7, 'EV_USR_DELETED_BY_ADMIN'), ),
(8, 'EV_DID_CREATED_BY_USER'), (5, "User's data updated by admin"),
(9, 'EV_DID_CREATED'), (6, 'Your data updated by admin'),
(10, 'EV_DID_DELETED'), (7, 'User deactivated by admin'),
(11, 'EV_CREDENTIAL_DELETED_BY_ADMIN'), (8, 'DID created by user'),
(12, 'EV_CREDENTIAL_DELETED'), (9, 'DID created'),
(13, 'EV_CREDENTIAL_ISSUED_FOR_USER'), (10, 'DID deleted'),
(14, 'EV_CREDENTIAL_ISSUED'), (11, 'Credential deleted by user'),
(15, 'EV_CREDENTIAL_PRESENTED_BY_USER'), (12, 'Credential deleted'),
(16, 'EV_CREDENTIAL_PRESENTED'), (13, 'Credential issued for user'),
(17, 'EV_CREDENTIAL_ENABLED'), (14, 'Credential issued'),
(18, 'EV_CREDENTIAL_CAN_BE_REQUESTED'), (15, 'Credential presented by user'),
(19, 'EV_CREDENTIAL_REVOKED_BY_ADMIN'), (16, 'Credential presented'),
(20, 'EV_CREDENTIAL_REVOKED'), (17, 'Credential enabled'),
(21, 'EV_ROLE_CREATED_BY_ADMIN'), (18, 'Credential available'),
(22, 'EV_ROLE_MODIFIED_BY_ADMIN'), (19, 'Credential revoked by admin'),
(23, 'EV_ROLE_DELETED_BY_ADMIN'), (20, 'Credential revoked'),
(24, 'EV_SERVICE_CREATED_BY_ADMIN'), (21, 'Role created by admin'),
(25, 'EV_SERVICE_MODIFIED_BY_ADMIN'), (22, 'Role modified by admin'),
(26, 'EV_SERVICE_DELETED_BY_ADMIN'), (23, 'Role deleted by admin'),
(27, 'EV_ORG_DID_CREATED_BY_ADMIN'), (24, 'Service created by admin'),
(28, 'EV_ORG_DID_DELETED_BY_ADMIN'), (25, 'Service modified by admin'),
(29, 'EV_USR_DEACTIVATED_BY_ADMIN'), (26, 'Service deleted by admin'),
(30, 'EV_USR_ACTIVATED_BY_ADMIN'), (27, 'Organisational DID created by admin'),
(28, 'Organisational DID deleted by admin'),
(29, 'User deactivated'),
(30, 'User activated'),
] ]
), ),
), ),

View File

@ -432,6 +432,7 @@ class DID(models.Model):
class Schemas(models.Model): class Schemas(models.Model):
type = models.CharField(max_length=250)
file_schema = models.CharField(max_length=250) file_schema = models.CharField(max_length=250)
data = models.TextField() data = models.TextField()
created_at = models.DateTimeField(auto_now=True) created_at = models.DateTimeField(auto_now=True)
@ -486,23 +487,11 @@ class VerificableCredential(models.Model):
related_name='vcredentials', related_name='vcredentials',
) )
@property
def get_schema(self):
if not self.data:
return {}
return json.loads(self.data)
def type(self): def type(self):
if self.data: return self.schema.type
return self.get_schema.get('type')[-1]
return self.schema.name()
def description(self): def description(self):
if not self.data: for des in json.loads(self.render()).get('description', []):
return self.schema.description()
for des in self.get_schema.get('description', []):
if settings.LANGUAGE_CODE == des.get('lang'): if settings.LANGUAGE_CODE == des.get('lang'):
return des.get('value', '') return des.get('value', '')
return '' return ''
@ -528,8 +517,10 @@ class VerificableCredential(models.Model):
def get_context(self): def get_context(self):
d = json.loads(self.csv_data) d = json.loads(self.csv_data)
format = "%Y-%m-%dT%H:%M:%SZ" issuance_date = ''
issuance_date = self.issued_on.strftime(format) if self.issued_on:
format = "%Y-%m-%dT%H:%M:%SZ"
issuance_date = self.issued_on.strftime(format)
context = { context = {
'vc_id': self.id, 'vc_id': self.id,

View File

@ -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 from django.db import migrations, models