refactor: add annotations in queryset to return sub_issues_count and total members in workspace (#185)

* refactor: add anotations in queryset to return sub_issues_count

* refactor: add sub issue count in modules cycles and my issues endpoint
This commit is contained in:
pablohashescobar 2023-01-26 11:41:11 +05:30 committed by GitHub
parent 6e99c007a5
commit 3036014ea2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 6 deletions

View File

@ -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

View File

@ -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",
]
]

View File

@ -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",
]
]

View File

@ -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)

View File

@ -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")

View File

@ -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"))

View File

@ -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)