Refactored Schemas and VerificableCredentials models to get the Schema template description on object creation

This commit is contained in:
Elijah 2023-12-15 19:28:55 +01:00
parent 5a2e656536
commit d0e31ab99b
3 changed files with 31 additions and 11 deletions

View file

@ -8,6 +8,7 @@ from smtplib import SMTPException
from django_tables2 import SingleTableView
from django.conf import settings
from django.template.loader import get_template
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
from django.views.generic.edit import (
@ -833,13 +834,13 @@ class SchemasImportView(SchemasMix):
if not Schemas.objects.filter(file_schema=x).exists()]
return schemas
class SchemasImportAddView(SchemasMix):
def get(self, request, *args, **kwargs):
file_name = kwargs['file_schema']
schemas_files = os.listdir(settings.SCHEMAS_DIR)
if not file_name in schemas_files:
if file_name not in schemas_files:
messages.error(self.request, f"The schema {file_name} not exist!")
return redirect('idhub:admin_schemas_import')
@ -858,7 +859,10 @@ class SchemasImportAddView(SchemasMix):
except Exception:
messages.error(self.request, _('This is not a valid schema!'))
return
schema = Schemas.objects.create(file_schema=file_name, data=data, type=name)
schema = Schemas.objects.create(file_schema=file_name,
data=data, type=name,
template_description=self.get_description()
)
schema.save()
return schema
@ -870,6 +874,20 @@ class SchemasImportAddView(SchemasMix):
return data
def get_template_description(self):
context = self.get_context()
template_name = 'credentials/{}'.format(
self.schema.file_schema
)
tmpl = get_template(template_name)
return tmpl.render(context)
def get_description(self):
for des in json.loads(self.render()).get('description', []):
if settings.LANGUAGE_CODE == des.get('lang'):
return des.get('value', '')
return ''
class ImportView(ImportExport, SingleTableView):
template_name = "idhub/admin/import.html"

View file

@ -438,6 +438,7 @@ class Schemas(models.Model):
created_at = models.DateTimeField(auto_now=True)
_name = models.CharField(max_length=250, null=True, db_column='name')
_description = models.CharField(max_length=250, null=True, db_column='description')
template_description = models.TextField()
@property
def get_schema(self):
@ -489,7 +490,6 @@ class VerificableCredential(models.Model):
issued_on = models.DateTimeField(null=True)
data = models.TextField()
csv_data = models.TextField()
description = models.TextField(null=True)
status = models.PositiveSmallIntegerField(
choices=Status.choices,
default=Status.ENABLED
@ -519,12 +519,8 @@ class VerificableCredential(models.Model):
def type(self):
return self.schema.type
@property
def get_description(self):
for des in json.loads(self.render()).get('description', []):
if settings.LANGUAGE_CODE == des.get('lang'):
return des.get('value', '')
return ''
return self.schema.template_description
def get_status(self):
return self.Status(self.status).label
@ -571,7 +567,6 @@ class VerificableCredential(models.Model):
tmpl = get_template(template_name)
return tmpl.render(context)
def get_issued_on(self):
if self.issued_on:
return self.issued_on.strftime("%m/%d/%Y")

View file

@ -112,7 +112,7 @@ class CredentialsTable(tables.Table):
description = tables.Column(verbose_name="Details", empty_values=())
def render_description(self, record):
return record.get_description
return record.get_description()
def render_status(self, record):
return record.get_status()
@ -124,6 +124,13 @@ class CredentialsTable(tables.Table):
return (queryset, True)
def order_description(self, queryset, is_descending):
queryset = queryset.order_by(
("-" if is_descending else "") + "schema__template_description"
)
return (queryset, True)
class Meta:
model = VerificableCredential
template_name = "idhub/custom_table.html"