Refactored Schemas and VerificableCredentials models to get the Schema template description on object creation
This commit is contained in:
parent
5a2e656536
commit
d0e31ab99b
|
@ -8,6 +8,7 @@ from smtplib import SMTPException
|
||||||
from django_tables2 import SingleTableView
|
from django_tables2 import SingleTableView
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.template.loader import get_template
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.views.generic.base import TemplateView
|
from django.views.generic.base import TemplateView
|
||||||
from django.views.generic.edit import (
|
from django.views.generic.edit import (
|
||||||
|
@ -833,13 +834,13 @@ class SchemasImportView(SchemasMix):
|
||||||
if not Schemas.objects.filter(file_schema=x).exists()]
|
if not Schemas.objects.filter(file_schema=x).exists()]
|
||||||
return schemas
|
return schemas
|
||||||
|
|
||||||
|
|
||||||
class SchemasImportAddView(SchemasMix):
|
class SchemasImportAddView(SchemasMix):
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
file_name = kwargs['file_schema']
|
file_name = kwargs['file_schema']
|
||||||
schemas_files = os.listdir(settings.SCHEMAS_DIR)
|
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!")
|
messages.error(self.request, f"The schema {file_name} not exist!")
|
||||||
return redirect('idhub:admin_schemas_import')
|
return redirect('idhub:admin_schemas_import')
|
||||||
|
|
||||||
|
@ -858,7 +859,10 @@ class SchemasImportAddView(SchemasMix):
|
||||||
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, type=name)
|
schema = Schemas.objects.create(file_schema=file_name,
|
||||||
|
data=data, type=name,
|
||||||
|
template_description=self.get_description()
|
||||||
|
)
|
||||||
schema.save()
|
schema.save()
|
||||||
return schema
|
return schema
|
||||||
|
|
||||||
|
@ -870,6 +874,20 @@ class SchemasImportAddView(SchemasMix):
|
||||||
|
|
||||||
return data
|
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):
|
class ImportView(ImportExport, SingleTableView):
|
||||||
template_name = "idhub/admin/import.html"
|
template_name = "idhub/admin/import.html"
|
||||||
|
|
|
@ -438,6 +438,7 @@ class Schemas(models.Model):
|
||||||
created_at = models.DateTimeField(auto_now=True)
|
created_at = models.DateTimeField(auto_now=True)
|
||||||
_name = models.CharField(max_length=250, null=True, db_column='name')
|
_name = models.CharField(max_length=250, null=True, db_column='name')
|
||||||
_description = models.CharField(max_length=250, null=True, db_column='description')
|
_description = models.CharField(max_length=250, null=True, db_column='description')
|
||||||
|
template_description = models.TextField()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def get_schema(self):
|
def get_schema(self):
|
||||||
|
@ -489,7 +490,6 @@ class VerificableCredential(models.Model):
|
||||||
issued_on = models.DateTimeField(null=True)
|
issued_on = models.DateTimeField(null=True)
|
||||||
data = models.TextField()
|
data = models.TextField()
|
||||||
csv_data = models.TextField()
|
csv_data = models.TextField()
|
||||||
description = models.TextField(null=True)
|
|
||||||
status = models.PositiveSmallIntegerField(
|
status = models.PositiveSmallIntegerField(
|
||||||
choices=Status.choices,
|
choices=Status.choices,
|
||||||
default=Status.ENABLED
|
default=Status.ENABLED
|
||||||
|
@ -519,12 +519,8 @@ class VerificableCredential(models.Model):
|
||||||
def type(self):
|
def type(self):
|
||||||
return self.schema.type
|
return self.schema.type
|
||||||
|
|
||||||
@property
|
|
||||||
def get_description(self):
|
def get_description(self):
|
||||||
for des in json.loads(self.render()).get('description', []):
|
return self.schema.template_description
|
||||||
if settings.LANGUAGE_CODE == des.get('lang'):
|
|
||||||
return des.get('value', '')
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def get_status(self):
|
def get_status(self):
|
||||||
return self.Status(self.status).label
|
return self.Status(self.status).label
|
||||||
|
@ -571,7 +567,6 @@ class VerificableCredential(models.Model):
|
||||||
tmpl = get_template(template_name)
|
tmpl = get_template(template_name)
|
||||||
return tmpl.render(context)
|
return tmpl.render(context)
|
||||||
|
|
||||||
|
|
||||||
def get_issued_on(self):
|
def get_issued_on(self):
|
||||||
if self.issued_on:
|
if self.issued_on:
|
||||||
return self.issued_on.strftime("%m/%d/%Y")
|
return self.issued_on.strftime("%m/%d/%Y")
|
||||||
|
|
|
@ -112,7 +112,7 @@ class CredentialsTable(tables.Table):
|
||||||
description = tables.Column(verbose_name="Details", empty_values=())
|
description = tables.Column(verbose_name="Details", empty_values=())
|
||||||
|
|
||||||
def render_description(self, record):
|
def render_description(self, record):
|
||||||
return record.get_description
|
return record.get_description()
|
||||||
|
|
||||||
def render_status(self, record):
|
def render_status(self, record):
|
||||||
return record.get_status()
|
return record.get_status()
|
||||||
|
@ -124,6 +124,13 @@ class CredentialsTable(tables.Table):
|
||||||
|
|
||||||
return (queryset, True)
|
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:
|
class Meta:
|
||||||
model = VerificableCredential
|
model = VerificableCredential
|
||||||
template_name = "idhub/custom_table.html"
|
template_name = "idhub/custom_table.html"
|
||||||
|
|
Loading…
Reference in a new issue