fix descriptions, names and jsons

This commit is contained in:
Cayo Puigdefabregas 2024-01-22 18:13:06 +01:00
parent 76fe838c92
commit 19fa84e45e
3 changed files with 30 additions and 18 deletions

View File

@ -928,10 +928,17 @@ 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
_name = json.dumps(ldata.get('name', ''))
_description = json.dumps(ldata.get('description', ''))
schema = Schemas.objects.create( schema = Schemas.objects.create(
file_schema=file_name, file_schema=file_name,
data=data, data=data,
type=name, type=title,
_name=_name,
_description=_description,
# template_description=_description
template_description=self.get_description() template_description=self.get_description()
) )
schema.save() schema.save()
@ -946,7 +953,7 @@ class SchemasImportAddView(SchemasMix):
return data return data
def get_template_description(self): def get_template_description(self):
context = self.get_context() context = {}
template_name = 'credentials/{}'.format( template_name = 'credentials/{}'.format(
self.schema.file_schema self.schema.file_schema
) )
@ -954,7 +961,7 @@ class SchemasImportAddView(SchemasMix):
return tmpl.render(context) return tmpl.render(context)
def get_description(self): def get_description(self):
for des in json.loads(self.render()).get('description', []): for des in json.loads(self.get_template_description()).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 ''

View File

@ -95,16 +95,18 @@ class Command(BaseCommand):
assert title assert title
except Exception: except Exception:
title = '' title = ''
return _name = ''
name = ''
try:
for x in dname:
if settings.LANGUAGE_CODE in x['lang']:
name = x.get('value', '')
except Exception:
return
Schemas.objects.create(file_schema=file_name, data=data, type=title) _name = json.dumps(ldata.get('name', ''))
_description = json.dumps(ldata.get('description', ''))
Schemas.objects.create(
file_schema=file_name,
data=data,
type=title,
_name=_name,
_description=_description
)
def open_file(self, file_name): def open_file(self, file_name):
data = '' data = ''

View File

@ -473,7 +473,7 @@ class Schemas(models.Model):
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)
_name = models.CharField(max_length=250, null=True, db_column='name') _name = models.TextField(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(null=True) template_description = models.TextField(null=True)
@ -488,7 +488,12 @@ class Schemas(models.Model):
if not field_value: if not field_value:
field_value = self.get_schema.get(schema_key, [] if is_json else '') field_value = self.get_schema.get(schema_key, [] if is_json else '')
self._update_model_field(field_attr, field_value) self._update_model_field(field_attr, field_value)
return json.loads(field_value) if is_json else field_value try:
if is_json:
return json.loads(field_value)
except json.decoder.JSONDecodeError:
return field_value
return field_value
def _update_model_field(self, field_attr, field_value): def _update_model_field(self, field_attr, field_value):
if field_value: if field_value:
@ -597,16 +602,14 @@ class VerificableCredential(models.Model):
def type(self): def type(self):
return self.schema.type return self.schema.type
#<<<<<<< HEAD
def get_description(self): def get_description(self):
return self.schema.template_description return self.schema._description or ''
#=======
def description(self): def description(self):
for des in json.loads(self.render("")).get('description', []): for des in json.loads(self.render("")).get('description', []):
if settings.LANGUAGE_CODE in des.get('lang'): if settings.LANGUAGE_CODE in des.get('lang'):
return des.get('value', '') return des.get('value', '')
return '' return ''
#>>>>>>> main
def get_type(self, lang=None): def get_type(self, lang=None):
schema = json.loads(self.schema.data) schema = json.loads(self.schema.data)