From ad3e511328300b831a4363317a7f3ddfe7eb42d7 Mon Sep 17 00:00:00 2001 From: Nikhil <118773738+pablohashescobar@users.noreply.github.com> Date: Wed, 15 Nov 2023 12:25:44 +0530 Subject: [PATCH] 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 --- apiserver/plane/api/views/issue.py | 2 +- ..._issuemention_label_sort_order_and_more.py | 33 +++++++++++++++++++ apiserver/plane/db/models/issue.py | 13 ++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 apiserver/plane/db/migrations/0047_issuemention_label_sort_order_and_more.py diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index d1cd93e73..258aee80d 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -753,8 +753,8 @@ class LabelViewSet(BaseViewSet): .select_related("project") .select_related("workspace") .select_related("parent") - .order_by("name") .distinct() + .order_by("sort_order") ) diff --git a/apiserver/plane/db/migrations/0047_issuemention_label_sort_order_and_more.py b/apiserver/plane/db/migrations/0047_issuemention_label_sort_order_and_more.py new file mode 100644 index 000000000..de7d6ed0f --- /dev/null +++ b/apiserver/plane/db/migrations/0047_issuemention_label_sort_order_and_more.py @@ -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) + ] diff --git a/apiserver/plane/db/models/issue.py b/apiserver/plane/db/models/issue.py index 0c227a158..a951e5c11 100644 --- a/apiserver/plane/db/models/issue.py +++ b/apiserver/plane/db/models/issue.py @@ -431,6 +431,7 @@ class Label(ProjectBaseModel): name = models.CharField(max_length=255) description = models.TextField(blank=True) color = models.CharField(max_length=255, blank=True) + sort_order = models.FloatField(default=65535) class Meta: unique_together = ["name", "project"] @@ -439,6 +440,18 @@ class Label(ProjectBaseModel): db_table = "labels" 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): return str(self.name)