From 88e987b902c19ed8121cd33c0edf07d779d8425e Mon Sep 17 00:00:00 2001 From: Nikhil <118773738+pablohashescobar@users.noreply.github.com> Date: Fri, 11 Aug 2023 16:03:15 +0530 Subject: [PATCH] feat: cycle and module sort order (#1841) * dev: cycle and module ordering * dev: sort order for smallest --- .../db/migrations/0042_auto_20230809_1745.py | 30 +++++++++++++++++++ apiserver/plane/db/models/cycle.py | 12 ++++++++ apiserver/plane/db/models/module.py | 12 ++++++++ 3 files changed, 54 insertions(+) create mode 100644 apiserver/plane/db/migrations/0042_auto_20230809_1745.py diff --git a/apiserver/plane/db/migrations/0042_auto_20230809_1745.py b/apiserver/plane/db/migrations/0042_auto_20230809_1745.py new file mode 100644 index 000000000..8bac2d954 --- /dev/null +++ b/apiserver/plane/db/migrations/0042_auto_20230809_1745.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.3 on 2023-08-09 12:15 +import random +from django.db import migrations + +def random_cycle_order(apps, schema_editor): + CycleModel = apps.get_model("db", "Cycle") + updated_cycles = [] + for obj in CycleModel.objects.all(): + obj.sort_order = random.randint(1, 65536) + updated_cycles.append(obj) + CycleModel.objects.bulk_update(updated_cycles, ["sort_order"], batch_size=100) + +def random_module_order(apps, schema_editor): + ModuleModel = apps.get_model("db", "Module") + updated_modules = [] + for obj in ModuleModel.objects.all(): + obj.sort_order = random.randint(1, 65536) + updated_modules.append(obj) + ModuleModel.objects.bulk_update(updated_modules, ["sort_order"], batch_size=100) + +class Migration(migrations.Migration): + + dependencies = [ + ('db', '0041_user_display_name_alter_analyticview_created_by_and_more'), + ] + + operations = [ + migrations.RunPython(random_cycle_order), + migrations.RunPython(random_module_order), + ] diff --git a/apiserver/plane/db/models/cycle.py b/apiserver/plane/db/models/cycle.py index c8c43cef4..56301e3d3 100644 --- a/apiserver/plane/db/models/cycle.py +++ b/apiserver/plane/db/models/cycle.py @@ -17,6 +17,7 @@ class Cycle(ProjectBaseModel): related_name="owned_by_cycle", ) view_props = models.JSONField(default=dict) + sort_order = models.FloatField(default=65535) class Meta: verbose_name = "Cycle" @@ -24,6 +25,17 @@ class Cycle(ProjectBaseModel): db_table = "cycles" ordering = ("-created_at",) + def save(self, *args, **kwargs): + if self._state.adding: + smallest_sort_order = Cycle.objects.filter( + project=self.project + ).aggregate(smallest=models.Min("sort_order"))["smallest"] + + if smallest_sort_order is not None: + self.sort_order = smallest_sort_order - 10000 + + super(Cycle, self).save(*args, **kwargs) + def __str__(self): """Return name of the cycle""" return f"{self.name} <{self.project.name}>" diff --git a/apiserver/plane/db/models/module.py b/apiserver/plane/db/models/module.py index 8ad0ec838..ad1e16080 100644 --- a/apiserver/plane/db/models/module.py +++ b/apiserver/plane/db/models/module.py @@ -40,6 +40,7 @@ class Module(ProjectBaseModel): through_fields=("module", "member"), ) view_props = models.JSONField(default=dict) + sort_order = models.FloatField(default=65535) class Meta: unique_together = ["name", "project"] @@ -48,6 +49,17 @@ class Module(ProjectBaseModel): db_table = "modules" ordering = ("-created_at",) + def save(self, *args, **kwargs): + if self._state.adding: + smallest_sort_order = Module.objects.filter( + project=self.project + ).aggregate(smallest=models.Min("sort_order"))["smallest"] + + if smallest_sort_order is not None: + self.sort_order = smallest_sort_order - 10000 + + super(Module, self).save(*args, **kwargs) + def __str__(self): return f"{self.name} {self.start_date} {self.target_date}"