From 19fa84e45e87430fd1f04327bad0cfe1dd124098 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 22 Jan 2024 18:13:06 +0100 Subject: [PATCH] fix descriptions, names and jsons --- idhub/admin/views.py | 13 ++++++++++--- idhub/management/commands/initial_datas.py | 20 +++++++++++--------- idhub/models.py | 15 +++++++++------ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 2af35dc..3773cab 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -928,10 +928,17 @@ class SchemasImportAddView(SchemasMix): except Exception: messages.error(self.request, _('This is not a valid schema!')) return + + _name = json.dumps(ldata.get('name', '')) + _description = json.dumps(ldata.get('description', '')) + schema = Schemas.objects.create( file_schema=file_name, data=data, - type=name, + type=title, + _name=_name, + _description=_description, + # template_description=_description template_description=self.get_description() ) schema.save() @@ -946,7 +953,7 @@ class SchemasImportAddView(SchemasMix): return data def get_template_description(self): - context = self.get_context() + context = {} template_name = 'credentials/{}'.format( self.schema.file_schema ) @@ -954,7 +961,7 @@ class SchemasImportAddView(SchemasMix): return tmpl.render(context) 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'): return des.get('value', '') return '' diff --git a/idhub/management/commands/initial_datas.py b/idhub/management/commands/initial_datas.py index 04c2df7..e889cb7 100644 --- a/idhub/management/commands/initial_datas.py +++ b/idhub/management/commands/initial_datas.py @@ -95,16 +95,18 @@ class Command(BaseCommand): assert title except Exception: title = '' - return - name = '' - try: - for x in dname: - if settings.LANGUAGE_CODE in x['lang']: - name = x.get('value', '') - except Exception: - return + _name = '' - 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): data = '' diff --git a/idhub/models.py b/idhub/models.py index 14229e5..96d8559 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -473,7 +473,7 @@ class Schemas(models.Model): file_schema = models.CharField(max_length=250) data = models.TextField() 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') template_description = models.TextField(null=True) @@ -488,7 +488,12 @@ class Schemas(models.Model): if not field_value: field_value = self.get_schema.get(schema_key, [] if is_json else '') 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): if field_value: @@ -597,16 +602,14 @@ class VerificableCredential(models.Model): def type(self): return self.schema.type -#<<<<<<< HEAD def get_description(self): - return self.schema.template_description -#======= + return self.schema._description or '' + def description(self): for des in json.loads(self.render("")).get('description', []): if settings.LANGUAGE_CODE in des.get('lang'): return des.get('value', '') return '' -#>>>>>>> main def get_type(self, lang=None): schema = json.loads(self.schema.data)