chore: add assignee avatar and minor refactor on cycles list and retrieve endpoint (#1320)

This commit is contained in:
pablohashescobar 2023-06-20 10:25:47 +05:30 committed by GitHub
parent c9ebc20a8e
commit 464c0f2308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,5 @@
# Python imports # Python imports
import json import json
from datetime import datetime, timedelta
# Django imports # Django imports
from django.db import IntegrityError from django.db import IntegrityError
@ -15,7 +14,6 @@ from django.db.models import (
Prefetch, Prefetch,
Sum, Sum,
) )
from django.db.models.functions import TruncDate
from django.core import serializers from django.core import serializers
from django.utils import timezone from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -163,14 +161,9 @@ class CycleViewSet(BaseViewSet):
) )
def list(self, request, slug, project_id): def list(self, request, slug, project_id):
# try: try:
queryset = self.get_queryset() queryset = self.get_queryset()
cycle_view = request.GET.get("cycle_view", False) cycle_view = request.GET.get("cycle_view", "all")
if not cycle_view:
return Response(
{"error": "Cycle View parameter is required"},
status=status.HTTP_400_BAD_REQUEST,
)
# All Cycles # All Cycles
if cycle_view == "all": if cycle_view == "all":
@ -197,7 +190,8 @@ class CycleViewSet(BaseViewSet):
.annotate(first_name=F("assignees__first_name")) .annotate(first_name=F("assignees__first_name"))
.annotate(last_name=F("assignees__last_name")) .annotate(last_name=F("assignees__last_name"))
.annotate(assignee_id=F("assignees__id")) .annotate(assignee_id=F("assignees__id"))
.values("first_name", "last_name", "assignee_id") .annotate(avatar=F("assignees__avatar"))
.values("first_name", "last_name", "assignee_id", "avatar")
.annotate(total_issues=Count("assignee_id")) .annotate(total_issues=Count("assignee_id"))
.annotate( .annotate(
completed_issues=Count( completed_issues=Count(
@ -252,9 +246,7 @@ class CycleViewSet(BaseViewSet):
cycle_id=data[0]["id"], cycle_id=data[0]["id"],
) )
return Response( return Response(data, status=status.HTTP_200_OK)
data, status=status.HTTP_200_OK
)
# Upcoming Cycles # Upcoming Cycles
if cycle_view == "upcoming": if cycle_view == "upcoming":
@ -293,12 +285,13 @@ class CycleViewSet(BaseViewSet):
return Response( return Response(
{"error": "No matching view found"}, status=status.HTTP_400_BAD_REQUEST {"error": "No matching view found"}, status=status.HTTP_400_BAD_REQUEST
) )
# except Exception as e:
# print(e) except Exception as e:
# return Response( capture_exception(e)
# {"error": "Something went wrong please try again later"}, return Response(
# status=status.HTTP_400_BAD_REQUEST, {"error": "Something went wrong please try again later"},
# ) status=status.HTTP_400_BAD_REQUEST,
)
def create(self, request, slug, project_id): def create(self, request, slug, project_id):
try: try:
@ -365,6 +358,7 @@ class CycleViewSet(BaseViewSet):
try: try:
queryset = self.get_queryset().get(pk=pk) queryset = self.get_queryset().get(pk=pk)
# Assignee Distribution
assignee_distribution = ( assignee_distribution = (
Issue.objects.filter( Issue.objects.filter(
issue_cycle__cycle_id=pk, issue_cycle__cycle_id=pk,
@ -374,7 +368,8 @@ class CycleViewSet(BaseViewSet):
.annotate(first_name=F("assignees__first_name")) .annotate(first_name=F("assignees__first_name"))
.annotate(last_name=F("assignees__last_name")) .annotate(last_name=F("assignees__last_name"))
.annotate(assignee_id=F("assignees__id")) .annotate(assignee_id=F("assignees__id"))
.values("first_name", "last_name", "assignee_id") .annotate(avatar=F("assignees__avatar"))
.values("first_name", "last_name", "assignee_id", "avatar")
.annotate(total_issues=Count("assignee_id")) .annotate(total_issues=Count("assignee_id"))
.annotate( .annotate(
completed_issues=Count( completed_issues=Count(
@ -391,6 +386,7 @@ class CycleViewSet(BaseViewSet):
.order_by("first_name", "last_name") .order_by("first_name", "last_name")
) )
# Label Distribution
label_distribution = ( label_distribution = (
Issue.objects.filter( Issue.objects.filter(
issue_cycle__cycle_id=pk, issue_cycle__cycle_id=pk,
@ -433,7 +429,10 @@ class CycleViewSet(BaseViewSet):
data, data,
status=status.HTTP_200_OK, status=status.HTTP_200_OK,
) )
except Cycle.DoesNotExist:
return Response(
{"error": "Cycle Does not exists"}, status=status.HTTP_400_BAD_REQUEST
)
except Exception as e: except Exception as e:
capture_exception(e) capture_exception(e)
return Response( return Response(