From 74d3e92fcd3f4acdef00c4a551a5c7e94793e243 Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 4 Jan 2024 14:38:59 +0100 Subject: [PATCH] Fixed error on sorting template names and descriptions --- idhub/admin/forms.py | 2 +- idhub/admin/tables.py | 13 +++---------- idhub/models.py | 33 +++++++++++++++++++++++++++------ idhub/tests/test_tables.py | 25 +++++++------------------ 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 2649f4b..478d671 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -34,7 +34,7 @@ class ImportForm(forms.Form): (x.did, x.label) for x in DID.objects.filter(user=self.user) ] self.fields['schema'].choices = [ - (x.id, x.name()) for x in Schemas.objects.filter() + (x.id, x.name) for x in Schemas.objects.filter() ] def clean_did(self): diff --git a/idhub/admin/tables.py b/idhub/admin/tables.py index 2c6e89e..e4d4ea2 100644 --- a/idhub/admin/tables.py +++ b/idhub/admin/tables.py @@ -245,18 +245,11 @@ class TemplateTable(tables.Table): orderable=False, verbose_name="Delete schema") - # empty_values makes django_tables call render_attr - name = tables.Column(empty_values=()) - description = tables.Column(empty_values=()) - - def render_name(self, record): - return record.get_name() - - def render_description(self, record): - return record.get_description() + _name = tables.Column(verbose_name="Name") + _description = tables.Column(verbose_name="Description") class Meta: model = Schemas template_name = "idhub/custom_table.html" - fields = ("created_at", "file_schema", "name", "description", + fields = ("created_at", "file_schema", "_name", "_description", "view_schema", "delete_schema") diff --git a/idhub/models.py b/idhub/models.py index 3ef0269..83dd90a 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -436,8 +436,8 @@ 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) - description = models.CharField(max_length=250, null=True) + _name = models.CharField(max_length=250, null=True, db_column='name') + _description = models.CharField(max_length=250, null=True, db_column='description') @property def get_schema(self): @@ -445,12 +445,33 @@ class Schemas(models.Model): return {} return json.loads(self.data) - def get_name(self): - return self.get_schema.get('name', '') + def _update_and_get_field(self, field_attr, schema_key): + field_value = getattr(self, field_attr) + if not field_value: + field_value = self.get_schema.get(schema_key, '') + self._update_model_field(field_attr, field_value) + return field_value - def get_description(self): - return self.get_schema.get('description', '') + def _update_model_field(self, field_attr, field_value): + if field_value: + setattr(self, field_attr, field_value) + self.save(update_fields=[field_attr]) + @property + def name(self): + return self._update_and_get_field('_name', 'name') + + @name.setter + def name(self, value): + self._name = value + + @property + def description(self): + return self._update_and_get_field('_description', 'description') + + @description.setter + def description(self, value): + self._description = value class VerificableCredential(models.Model): """ diff --git a/idhub/tests/test_tables.py b/idhub/tests/test_tables.py index 81d2886..7c3ad89 100644 --- a/idhub/tests/test_tables.py +++ b/idhub/tests/test_tables.py @@ -128,30 +128,19 @@ class TemplateTableTest(TestCase): self.fail("Ordering template table by name raised FieldError") def test_order_table_by_name_correctly_orders(self): - self.table.order_by = 'name' + table = TemplateTable(Schemas.objects.all(), order_by="name") # Fetch the sorted records - sorted_records = list(self.table.rows) + sorted_records = list(table.rows) # Verify the order is as expected - self.assertTrue(sorted_records[0].record.name - < sorted_records[1].record.name) - self.assertTrue(sorted_records[1].record.name - < sorted_records[2].record.name) - - def test_order_table_by_name_ascending_correctly_orders(self): - self.table.order_by = '-name' - # Fetch the sorted records - sorted_records = list(self.table.rows) - - # Verify the order is as expected - self.assertTrue(sorted_records[0].record.name - > sorted_records[1].record.name) - self.assertTrue(sorted_records[1].record.name - > sorted_records[2].record.name) + self.assertLess(sorted_records[0].record.name, + sorted_records[1].record.name) + self.assertLess(sorted_records[1].record.name, + sorted_records[2].record.name) def test_order_table_by_description_works(self): try: # Apply sorting self.table.order_by = 'description' except FieldError: - self.fail("Ordering template table by descripition raised FieldError") + self.fail("Ordering template table by description raised FieldError")