From 993cf3faba468bf1220b07474283596713840ca0 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Fri, 5 May 2023 15:13:03 +0530 Subject: [PATCH] chore: add assignee avatar in cycle endpoint (#996) * chore: add assignee avatar in cycle endpoint * dev: update the structure to return avatar and firstname * dev: return distinct users * dev: update the structure to return id * dev: update the prefetch queryset to distinct by id * dev: remove id from distinct * dev: add unique condition --- apiserver/plane/api/serializers/cycle.py | 19 +++++++++++++++++ apiserver/plane/api/views/cycle.py | 27 +++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/apiserver/plane/api/serializers/cycle.py b/apiserver/plane/api/serializers/cycle.py index 5c06a28e7..d6d281357 100644 --- a/apiserver/plane/api/serializers/cycle.py +++ b/apiserver/plane/api/serializers/cycle.py @@ -19,10 +19,29 @@ class CycleSerializer(BaseSerializer): started_issues = serializers.IntegerField(read_only=True) unstarted_issues = serializers.IntegerField(read_only=True) backlog_issues = serializers.IntegerField(read_only=True) + assignees = serializers.SerializerMethodField() workspace_detail = WorkspaceLiteSerializer(read_only=True, source="workspace") project_detail = ProjectLiteSerializer(read_only=True, source="project") + def get_assignees(self, obj): + members = [ + { + "avatar": assignee.avatar, + "first_name": assignee.first_name, + "id": assignee.id, + } + for issue_cycle in obj.issue_cycle.all() + for assignee in issue_cycle.issue.assignees.all() + ] + # Use a set comprehension to return only the unique objects + unique_objects = {frozenset(item.items()) for item in members} + + # Convert the set back to a list of dictionaries + unique_list = [dict(item) for item in unique_objects] + + return unique_list + class Meta: model = Cycle fields = "__all__" diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index 9265aca00..36c54c54c 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -30,6 +30,7 @@ from plane.db.models import ( CycleFavorite, IssueLink, IssueAttachment, + User, ) from plane.bgtasks.issue_activites_task import issue_activity from plane.utils.grouper import group_results @@ -501,6 +502,12 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .prefetch_related( + Prefetch( + "issue_cycle__issue__assignees", + queryset=User.objects.only("avatar", "first_name", "id").distinct(), + ) + ) .order_by("name", "-is_favorite") ) @@ -545,6 +552,12 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .prefetch_related( + Prefetch( + "issue_cycle__issue__assignees", + queryset=User.objects.only("avatar", "first_name", "id").distinct(), + ) + ) .order_by("name", "-is_favorite") ) @@ -557,7 +570,7 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView): ) except Exception as e: - capture_exception(e) + print(e) return Response( {"error": "Something went wrong please try again later"}, status=status.HTTP_400_BAD_REQUEST, @@ -618,6 +631,12 @@ class CompletedCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .prefetch_related( + Prefetch( + "issue_cycle__issue__assignees", + queryset=User.objects.only("avatar", "first_name", "id").distinct(), + ) + ) .order_by("name", "-is_favorite") ) @@ -693,6 +712,12 @@ class DraftCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .prefetch_related( + Prefetch( + "issue_cycle__issue__assignees", + queryset=User.objects.only("avatar", "first_name", "id").distinct(), + ) + ) .order_by("name", "-is_favorite") )