From ab9e0cf559d2c7717c01371d7c1045339ef23865 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Wed, 15 Mar 2023 23:21:37 +0530 Subject: [PATCH] refactor: update response structure for cycle issue and module issues (#432) --- apiserver/plane/api/serializers/issue.py | 2 ++ apiserver/plane/api/views/cycle.py | 40 ++++++++++++++++++++---- apiserver/plane/api/views/module.py | 30 +++++++++++++++--- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/apiserver/plane/api/serializers/issue.py b/apiserver/plane/api/serializers/issue.py index e934f5cbd..171bdf2d2 100644 --- a/apiserver/plane/api/serializers/issue.py +++ b/apiserver/plane/api/serializers/issue.py @@ -434,6 +434,8 @@ class IssueStateSerializer(BaseSerializer): project_detail = ProjectSerializer(read_only=True, source="project") label_details = LabelSerializer(read_only=True, source="labels", many=True) assignee_details = UserLiteSerializer(read_only=True, source="assignees", many=True) + sub_issues_count = serializers.IntegerField(read_only=True) + bridge_id = serializers.UUIDField(read_only=True) class Meta: model = Issue diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index 5f7b46771..b87e21f5a 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -3,7 +3,7 @@ import json # Django imports from django.db import IntegrityError -from django.db.models import OuterRef, Func, F, Q, Exists, OuterRef +from django.db.models import OuterRef, Func, F, Q, Exists, OuterRef, Prefetch from django.core import serializers from django.utils import timezone @@ -18,9 +18,18 @@ from plane.api.serializers import ( CycleSerializer, CycleIssueSerializer, CycleFavoriteSerializer, + IssueStateSerializer, ) from plane.api.permissions import ProjectEntityPermission -from plane.db.models import Cycle, CycleIssue, Issue, CycleFavorite +from plane.db.models import ( + Cycle, + CycleIssue, + Issue, + CycleFavorite, + IssueBlocker, + IssueLink, + ModuleIssue, +) from plane.bgtasks.issue_activites_task import issue_activity from plane.utils.grouper import group_results @@ -133,19 +142,38 @@ class CycleIssueViewSet(BaseViewSet): def list(self, request, slug, project_id, cycle_id): try: order_by = request.GET.get("order_by", "created_at") - queryset = self.get_queryset().order_by(f"issue__{order_by}") group_by = request.GET.get("group_by", False) - cycle_issues = CycleIssueSerializer(queryset, many=True).data + issues = ( + Issue.objects.filter(issue_cycle__cycle_id=cycle_id) + .annotate( + sub_issues_count=Issue.objects.filter(parent=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) + .annotate(bridge_id=F("issue_cycle__id")) + .filter(project_id=project_id) + .filter(workspace__slug=slug) + .select_related("project") + .select_related("workspace") + .select_related("state") + .select_related("parent") + .prefetch_related("assignees") + .prefetch_related("labels") + .order_by(order_by) + ) + + issues_data = IssueStateSerializer(issues, many=True).data if group_by: return Response( - group_results(cycle_issues, f"issue_detail.{group_by}"), + group_results(issues_data, group_by), status=status.HTTP_200_OK, ) return Response( - cycle_issues, + issues_data, status=status.HTTP_200_OK, ) except Exception as e: diff --git a/apiserver/plane/api/views/module.py b/apiserver/plane/api/views/module.py index da7593ea6..43e4ca236 100644 --- a/apiserver/plane/api/views/module.py +++ b/apiserver/plane/api/views/module.py @@ -19,6 +19,7 @@ from plane.api.serializers import ( ModuleIssueSerializer, ModuleLinkSerializer, ModuleFavoriteSerializer, + IssueStateSerializer, ) from plane.api.permissions import ProjectEntityPermission from plane.db.models import ( @@ -153,20 +154,39 @@ class ModuleIssueViewSet(BaseViewSet): def list(self, request, slug, project_id, module_id): try: - order_by = request.GET.get("order_by", "issue__created_at") - queryset = self.get_queryset().order_by(f"issue__{order_by}") + order_by = request.GET.get("order_by", "created_at") group_by = request.GET.get("group_by", False) - module_issues = ModuleIssueSerializer(queryset, many=True).data + issues = ( + Issue.objects.filter(issue_module__module_id=module_id) + .annotate( + sub_issues_count=Issue.objects.filter(parent=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) + .annotate(bridge_id=F("issue_module__id")) + .filter(project_id=project_id) + .filter(workspace__slug=slug) + .select_related("project") + .select_related("workspace") + .select_related("state") + .select_related("parent") + .prefetch_related("assignees") + .prefetch_related("labels") + .order_by(order_by) + ) + + issues_data = IssueStateSerializer(issues, many=True).data if group_by: return Response( - group_results(module_issues, f"issue_detail.{group_by}"), + group_results(issues_data, group_by), status=status.HTTP_200_OK, ) return Response( - module_issues, + issues_data, status=status.HTTP_200_OK, ) except Exception as e: