From ffeb77ec86c7121063f92b03abda307f658df62e Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Thu, 29 Feb 2024 16:38:53 +0530 Subject: [PATCH] dev: fix paginating true list --- apiserver/plane/app/views/view.py | 77 +++++++++++++++--------------- apiserver/plane/utils/grouper.py | 70 ++++++++++++++++++++------- apiserver/plane/utils/paginator.py | 3 +- 3 files changed, 93 insertions(+), 57 deletions(-) diff --git a/apiserver/plane/app/views/view.py b/apiserver/plane/app/views/view.py index 8827fe28c..769aa1adb 100644 --- a/apiserver/plane/app/views/view.py +++ b/apiserver/plane/app/views/view.py @@ -44,8 +44,14 @@ from plane.db.models import ( IssueLink, IssueAttachment, ) +from plane.utils.grouper import ( + issue_queryset_grouper, + issue_on_results, + issue_group_values, +) from plane.utils.issue_filters import issue_filters from plane.utils.order_queryset import order_issue_queryset +from plane.utils.paginator import GroupedOffsetPaginator class GlobalViewViewSet(BaseViewSet): @@ -158,49 +164,44 @@ class GlobalViewIssuesViewSet(BaseViewSet): issue_queryset=issue_queryset, order_by_param=order_by_param ) - def on_results(issues): - if self.expand or self.fields: - return IssueSerializer( - issues, many=True, expand=self.expand, fields=self.fields - ).data - return issues.values( - "id", - "name", - "state_id", - "sort_order", - "completed_at", - "estimate_point", - "priority", - "start_date", - "target_date", - "sequence_id", - "project_id", - "parent_id", - "cycle_id", - "module_ids", - "label_ids", - "assignee_ids", - "sub_issues_count", - "created_at", - "updated_at", - "created_by", - "updated_by", - "attachment_count", - "link_count", - "is_draft", - "archived_at", - ) + # Group by + group_by = request.GET.get("group_by", False) + issue_queryset = issue_queryset_grouper( + queryset=issue_queryset, field=group_by + ) - if request.GET.get("layout", "spreadsheet") in [ - "layout", - "spreadsheet", - ]: + # List Paginate + if not group_by: return self.paginate( request=request, queryset=issue_queryset, - on_results=on_results + on_results=lambda issues: issue_on_results( + group_by=group_by, issues=issues + ), ) - return on_results(issues=issue_queryset) + + # Group paginate + return self.paginate( + request=request, + queryset=issue_queryset, + on_results=lambda issues: issue_on_results( + group_by=group_by, issues=issues + ), + group_by_fields=issue_group_values( + field=group_by, + slug=slug, + ), + paginator_cls=GroupedOffsetPaginator, + group_by_field_name=group_by, + count_filter=Q( + Q(issue_inbox__status=1) + | Q(issue_inbox__status=-1) + | Q(issue_inbox__status=2) + | Q(issue_inbox__isnull=True), + archived_at__isnull=False, + is_draft=True, + ), + ) class IssueViewViewSet(BaseViewSet): diff --git a/apiserver/plane/utils/grouper.py b/apiserver/plane/utils/grouper.py index 012bd7090..e652557a7 100644 --- a/apiserver/plane/utils/grouper.py +++ b/apiserver/plane/utils/grouper.py @@ -1,12 +1,19 @@ # Django imports -from django.db.models import Q, F, QuerySet +from django.db.models import Q, F from django.contrib.postgres.aggregates import ArrayAgg from django.contrib.postgres.fields import ArrayField from django.db.models import Value, UUIDField from django.db.models.functions import Coalesce # Module imports -from plane.db.models import State, Label, ProjectMember, Cycle, Module +from plane.db.models import ( + State, + Label, + ProjectMember, + Cycle, + Module, + WorkspaceMember, +) def issue_queryset_grouper(field, queryset): @@ -136,26 +143,53 @@ def issue_on_results(issues, group_by): return issues.values(*required_fields) -def issue_group_values(field, slug, project_id): +def issue_group_values(field, slug, project_id=None): if field == "state_id": - return list(State.objects.filter( workspace__slug=slug, project_id=project_id - ).values_list("id", flat=True)) + queryset = State.objects.filter( + ~Q(name="Triage"), + workspace__slug=slug, + ).values_list("id", flat=True) + if project_id: + return list(queryset.filter(project_id=project_id)) + else: + return list(queryset) if field == "labels__id": - return list(Label.objects.filter( - workspace__slug=slug, project_id=project_id - ).values_list("id", flat=True)) + ["None"] + queryset = Label.objects.filter(workspace__slug=slug).values_list( + "id", flat=True + ) + if project_id: + return list(queryset.filter(project_id=project_id)) + ["None"] + else: + return list(queryset) + ["None"] if field == "assignees__id": - return list(ProjectMember.objects.filter( - workspace__slug=slug, project_id=project_id, is_active=True, - ).values_list("member_id", flat=True)) + ["None"] + if project_id: + return ProjectMember.objects.filter( + workspace__slug=slug, + project_id=project_id, + is_active=True, + ).values_list("member_id", flat=True) + else: + return list( + WorkspaceMember.objects.filter( + workspace__slug=slug, is_active=True + ).values_list("member_id", flat=True) + ) if field == "modules__id": - return list(Module.objects.filter( - workspace__slug=slug, project_id=project_id - ).values_list("id", flat=True)) + ["None"] + queryset = Module.objects.filter( + workspace__slug=slug, + ).values_list("id", flat=True) + if project_id: + return list(queryset.filter(project_id=project_id)) + ["None"] + else: + return list(queryset) + ["None"] if field == "cycle_id": - return list(Cycle.objects.filter( - workspace__slug=slug, project_id=project_id - ).values_list("id", flat=True)) + ["None"] + queryset = Cycle.objects.filter( + workspace__slug=slug, + ).values_list("id", flat=True) + if project_id: + return list(queryset.filter(project_id=project_id)) + ["None"] + else: + return list(queryset) + ["None"] if field == "priority": - return ["low", "medium", "high", "urgent", "none"] + return ["low", "medium", "high", "urgent", "none"] return [] diff --git a/apiserver/plane/utils/paginator.py b/apiserver/plane/utils/paginator.py index a10110725..2fe39c56d 100644 --- a/apiserver/plane/utils/paginator.py +++ b/apiserver/plane/utils/paginator.py @@ -327,7 +327,8 @@ class GroupedOffsetPaginator(OffsetPaginator): processed_results = self.__get_field_dict() for result in results: group_value = str(result.get(self.group_by_field_name)) - processed_results[str(group_value)]["results"].append(result) + if group_value in processed_results: + processed_results[str(group_value)]["results"].append(result) return processed_results def process_results(self, results):