Fixed error on sorting template names and descriptions

This commit is contained in:
Elijah 2024-01-04 14:38:59 +01:00
parent 644edab35a
commit 74d3e92fcd
4 changed files with 38 additions and 35 deletions

View File

@ -34,7 +34,7 @@ class ImportForm(forms.Form):
(x.did, x.label) for x in DID.objects.filter(user=self.user) (x.did, x.label) for x in DID.objects.filter(user=self.user)
] ]
self.fields['schema'].choices = [ 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): def clean_did(self):

View File

@ -245,18 +245,11 @@ class TemplateTable(tables.Table):
orderable=False, orderable=False,
verbose_name="Delete schema") verbose_name="Delete schema")
# empty_values makes django_tables call render_attr _name = tables.Column(verbose_name="Name")
name = tables.Column(empty_values=()) _description = tables.Column(verbose_name="Description")
description = tables.Column(empty_values=())
def render_name(self, record):
return record.get_name()
def render_description(self, record):
return record.get_description()
class Meta: class Meta:
model = Schemas model = Schemas
template_name = "idhub/custom_table.html" template_name = "idhub/custom_table.html"
fields = ("created_at", "file_schema", "name", "description", fields = ("created_at", "file_schema", "_name", "_description",
"view_schema", "delete_schema") "view_schema", "delete_schema")

View File

@ -436,8 +436,8 @@ 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) _name = models.CharField(max_length=250, null=True, db_column='name')
description = models.CharField(max_length=250, null=True) _description = models.CharField(max_length=250, null=True, db_column='description')
@property @property
def get_schema(self): def get_schema(self):
@ -445,12 +445,33 @@ class Schemas(models.Model):
return {} return {}
return json.loads(self.data) return json.loads(self.data)
def get_name(self): def _update_and_get_field(self, field_attr, schema_key):
return self.get_schema.get('name', '') 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): def _update_model_field(self, field_attr, field_value):
return self.get_schema.get('description', '') 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): class VerificableCredential(models.Model):
""" """

View File

@ -128,30 +128,19 @@ class TemplateTableTest(TestCase):
self.fail("Ordering template table by name raised FieldError") self.fail("Ordering template table by name raised FieldError")
def test_order_table_by_name_correctly_orders(self): 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 # Fetch the sorted records
sorted_records = list(self.table.rows) sorted_records = list(table.rows)
# Verify the order is as expected # Verify the order is as expected
self.assertTrue(sorted_records[0].record.name self.assertLess(sorted_records[0].record.name,
< sorted_records[1].record.name) sorted_records[1].record.name)
self.assertTrue(sorted_records[1].record.name self.assertLess(sorted_records[1].record.name,
< sorted_records[2].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)
def test_order_table_by_description_works(self): def test_order_table_by_description_works(self):
try: try:
# Apply sorting # Apply sorting
self.table.order_by = 'description' self.table.order_by = 'description'
except FieldError: except FieldError:
self.fail("Ordering template table by descripition raised FieldError") self.fail("Ordering template table by description raised FieldError")