enhancement: label sort order (#2763)

* chore: label sort ordering

* dev: ordering

* fix: sort order

* fix: save of labels

* dev: remove ordering by name

---------

Co-authored-by: NarayanBavisetti <narayan3119@gmail.com>
This commit is contained in:
Nikhil 2023-11-15 12:25:44 +05:30 committed by sriram veeraghanta
parent d158fe8193
commit ad3e511328
3 changed files with 47 additions and 1 deletions

View File

@ -753,8 +753,8 @@ class LabelViewSet(BaseViewSet):
.select_related("project") .select_related("project")
.select_related("workspace") .select_related("workspace")
.select_related("parent") .select_related("parent")
.order_by("name")
.distinct() .distinct()
.order_by("sort_order")
) )

View File

@ -0,0 +1,33 @@
# Generated by Django 4.2.5 on 2023-11-13 15:07
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid
import random
def random_sort_ordering(apps, schema_editor):
Label = apps.get_model("db", "Label")
bulk_labels = []
for label in Label.objects.all():
label.sort_order = random.randint(0,65535)
bulk_labels.append(label)
Label.objects.bulk_update(bulk_labels, ["sort_order"], batch_size=1000)
class Migration(migrations.Migration):
dependencies = [
("db", "0046_alter_analyticview_created_by_and_more"),
]
operations = [
migrations.AddField(
model_name="label",
name="sort_order",
field=models.FloatField(default=65535),
),
migrations.RunPython(random_sort_ordering)
]

View File

@ -431,6 +431,7 @@ class Label(ProjectBaseModel):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
description = models.TextField(blank=True) description = models.TextField(blank=True)
color = models.CharField(max_length=255, blank=True) color = models.CharField(max_length=255, blank=True)
sort_order = models.FloatField(default=65535)
class Meta: class Meta:
unique_together = ["name", "project"] unique_together = ["name", "project"]
@ -439,6 +440,18 @@ class Label(ProjectBaseModel):
db_table = "labels" db_table = "labels"
ordering = ("-created_at",) ordering = ("-created_at",)
def save(self, *args, **kwargs):
if self._state.adding:
# Get the maximum sequence value from the database
last_id = Label.objects.filter(project=self.project).aggregate(
largest=models.Max("sort_order")
)["largest"]
# if last_id is not None
if last_id is not None:
self.sort_order = last_id + 10000
super(Label, self).save(*args, **kwargs)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)