diff --git a/apiserver/plane/api/serializers/cycle.py b/apiserver/plane/api/serializers/cycle.py index 4e125bfae..09f35b669 100644 --- a/apiserver/plane/api/serializers/cycle.py +++ b/apiserver/plane/api/serializers/cycle.py @@ -1,3 +1,6 @@ +# Third party imports +from rest_framework import serializers + # Module imports from .base import BaseSerializer from .user import UserLiteSerializer @@ -22,6 +25,7 @@ class CycleSerializer(BaseSerializer): class CycleIssueSerializer(BaseSerializer): issue_detail = IssueStateSerializer(read_only=True, source="issue") + sub_issues_count = serializers.IntegerField(read_only=True) class Meta: model = CycleIssue diff --git a/apiserver/plane/api/serializers/issue.py b/apiserver/plane/api/serializers/issue.py index 227d3b96d..a148cbfb5 100644 --- a/apiserver/plane/api/serializers/issue.py +++ b/apiserver/plane/api/serializers/issue.py @@ -432,6 +432,7 @@ class IssueSerializer(BaseSerializer): blocker_issues = BlockerIssueSerializer(read_only=True, many=True) issue_cycle = IssueCycleDetailSerializer(read_only=True) issue_module = IssueModuleDetailSerializer(read_only=True) + sub_issues_count = serializers.IntegerField(read_only=True) class Meta: model = Issue @@ -443,4 +444,4 @@ class IssueSerializer(BaseSerializer): "updated_by", "created_at", "updated_at", - ] \ No newline at end of file + ] diff --git a/apiserver/plane/api/serializers/module.py b/apiserver/plane/api/serializers/module.py index 1d3748f8d..9f165dd28 100644 --- a/apiserver/plane/api/serializers/module.py +++ b/apiserver/plane/api/serializers/module.py @@ -150,6 +150,7 @@ class ModuleIssueSerializer(BaseSerializer): module_detail = ModuleFlatSerializer(read_only=True, source="module") issue_detail = IssueStateSerializer(read_only=True, source="issue") + sub_issues_count = serializers.IntegerField(read_only=True) class Meta: model = ModuleIssue @@ -200,4 +201,4 @@ class ModuleSerializer(BaseSerializer): "updated_by", "created_at", "updated_at", - ] \ No newline at end of file + ] diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index 28125512f..d1b291d9a 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -1,3 +1,6 @@ +# Django imports +from django.db.models import OuterRef, Func, F + # Third party imports from rest_framework.response import Response from rest_framework import status @@ -56,6 +59,12 @@ class CycleIssueViewSet(BaseViewSet): return self.filter_queryset( super() .get_queryset() + .annotate( + sub_issues_count=Issue.objects.filter(parent=OuterRef("issue_id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) .filter(workspace__slug=self.kwargs.get("slug")) .filter(project_id=self.kwargs.get("project_id")) .filter(project__project_projectmember__member=self.request.user) diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index dd012c129..4212934bf 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -3,7 +3,7 @@ import json from itertools import groupby, chain # Django imports -from django.db.models import Prefetch +from django.db.models import Prefetch, OuterRef, Func, F from django.core.serializers.json import DjangoJSONEncoder # Third Party imports @@ -93,9 +93,16 @@ class IssueViewSet(BaseViewSet): return super().perform_update(serializer) def get_queryset(self): + return ( super() .get_queryset() + .annotate( + sub_issues_count=Issue.objects.filter(parent=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) .filter(project_id=self.kwargs.get("project_id")) .filter(workspace__slug=self.kwargs.get("slug")) .select_related("project") @@ -178,6 +185,7 @@ class IssueViewSet(BaseViewSet): ) except Exception as e: + print(e) capture_exception(e) return Response( {"error": "Something went wrong please try again later"}, @@ -218,6 +226,12 @@ class UserWorkSpaceIssues(BaseAPIView): try: issues = ( Issue.objects.filter(assignees__in=[request.user], workspace__slug=slug) + .annotate( + sub_issues_count=Issue.objects.filter(parent=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) .select_related("project") .select_related("workspace") .select_related("state") diff --git a/apiserver/plane/api/views/module.py b/apiserver/plane/api/views/module.py index 4c121f23f..9955ded76 100644 --- a/apiserver/plane/api/views/module.py +++ b/apiserver/plane/api/views/module.py @@ -1,6 +1,6 @@ # Django Imports from django.db import IntegrityError -from django.db.models import Prefetch +from django.db.models import Prefetch, F, OuterRef, Func # Third party imports from rest_framework.response import Response @@ -118,6 +118,12 @@ class ModuleIssueViewSet(BaseViewSet): return self.filter_queryset( super() .get_queryset() + .annotate( + sub_issues_count=Issue.objects.filter(parent=OuterRef("issue")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) .filter(workspace__slug=self.kwargs.get("slug")) .filter(project_id=self.kwargs.get("project_id")) .filter(module_id=self.kwargs.get("module_id")) diff --git a/apiserver/plane/api/views/workspace.py b/apiserver/plane/api/views/workspace.py index e03a80e82..e8695c818 100644 --- a/apiserver/plane/api/views/workspace.py +++ b/apiserver/plane/api/views/workspace.py @@ -10,7 +10,7 @@ from django.utils import timezone from django.core.exceptions import ValidationError from django.core.validators import validate_email from django.contrib.sites.shortcuts import get_current_site -from django.db.models import CharField, Count +from django.db.models import CharField, Count, OuterRef, Func, F from django.db.models.functions import Cast # Third party modules @@ -111,6 +111,14 @@ class UserWorkSpacesEndpoint(BaseAPIView): def get(self, request): try: + + member_count = ( + WorkspaceMember.objects.filter(workspace=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) + workspace = ( Workspace.objects.prefetch_related( Prefetch("workspace_member", queryset=WorkspaceMember.objects.all()) @@ -119,7 +127,7 @@ class UserWorkSpacesEndpoint(BaseAPIView): workspace_member__member=request.user, ) .select_related("owner") - ).annotate(total_members=Count("workspace_member")) + ).annotate(total_members=member_count) serializer = WorkSpaceSerializer(self.filter_queryset(workspace), many=True) return Response(serializer.data, status=status.HTTP_200_OK)