From 94ea299c393c404662559fe6fb62df0cd2600fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Wed, 17 Aug 2016 14:21:07 +0200 Subject: [PATCH] Add epics info in user_story_extra_info in task serializer --- taiga/projects/tasks/api.py | 1 - taiga/projects/tasks/serializers.py | 14 +---------- taiga/projects/tasks/utils.py | 39 ++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/taiga/projects/tasks/api.py b/taiga/projects/tasks/api.py index 5f9a06ae..9584a17b 100644 --- a/taiga/projects/tasks/api.py +++ b/taiga/projects/tasks/api.py @@ -79,7 +79,6 @@ class TaskViewSet(OCCResourceMixin, VotedResourceMixin, HistoryResourceMixin, def get_queryset(self): qs = super().get_queryset() qs = qs.select_related("milestone", - "user_story", "project", "status", "owner", diff --git a/taiga/projects/tasks/serializers.py b/taiga/projects/tasks/serializers.py index 358b88ee..f0621581 100644 --- a/taiga/projects/tasks/serializers.py +++ b/taiga/projects/tasks/serializers.py @@ -29,18 +29,6 @@ from taiga.projects.notifications.mixins import WatchedResourceSerializer from taiga.projects.tagging.serializers import TaggedInProjectResourceSerializer from taiga.projects.votes.mixins.serializers import VoteResourceSerializerMixin -class UserStoryExtraInfoSerializer(serializers.LightSerializer): - id = Field() - ref = Field() - subject = Field() - - def to_value(self, instance): - if instance is None: - return None - - return super().to_value(instance) - - class TaskListSerializer(VoteResourceSerializerMixin, WatchedResourceSerializer, OwnerExtraInfoSerializerMixin, AssignedToExtraInfoSerializerMixin, StatusExtraInfoSerializerMixin, BasicAttachmentsInfoSerializerMixin, @@ -65,7 +53,7 @@ class TaskListSerializer(VoteResourceSerializerMixin, WatchedResourceSerializer, is_blocked = Field() blocked_note = Field() is_closed = MethodField() - user_story_extra_info = UserStoryExtraInfoSerializer(attr="user_story") + user_story_extra_info = Field() def get_milestone_slug(self, obj): return obj.milestone.slug if obj.milestone else None diff --git a/taiga/projects/tasks/utils.py b/taiga/projects/tasks/utils.py index d10dddab..d5775d19 100644 --- a/taiga/projects/tasks/utils.py +++ b/taiga/projects/tasks/utils.py @@ -25,8 +25,44 @@ from taiga.projects.votes.utils import attach_total_voters_to_queryset from taiga.projects.votes.utils import attach_is_voter_to_queryset -def attach_extra_info(queryset, user=None, include_attachments=False): +def attach_user_story_extra_info(queryset, as_field="user_story_extra_info"): + """Attach userstory extra info as json column to each object of the queryset. + :param queryset: A Django user stories queryset object. + :param as_field: Attach the userstory extra info as an attribute with this name. + + :return: Queryset object with the additional `as_field` field. + """ + + model = queryset.model + sql = """SELECT row_to_json(u) + FROM (SELECT "userstories_userstory"."id" AS "id", + "userstories_userstory"."ref" AS "ref", + "userstories_userstory"."subject" AS "subject", + (SELECT json_agg(row_to_json(t)) + FROM (SELECT "epics_epic"."id" AS "id", + "epics_epic"."ref" AS "ref", + "epics_epic"."subject" AS "subject", + "epics_epic"."color" AS "color", + json_build_object('id', "projects_project"."id", + 'name', "projects_project"."name", + 'slug', "projects_project"."slug") AS "project" + FROM "epics_relateduserstory" + INNER JOIN "epics_epic" + ON "epics_epic"."id" = "epics_relateduserstory"."epic_id" + INNER JOIN "projects_project" + ON "projects_project"."id" = "epics_epic"."project_id" + WHERE "epics_relateduserstory"."user_story_id" = "{tbl}"."user_story_id" + ORDER BY "projects_project"."name", "epics_epic"."ref") t) AS "epics" + FROM "userstories_userstory" + WHERE "userstories_userstory"."id" = "{tbl}"."user_story_id") u""" + + sql = sql.format(tbl=model._meta.db_table) + queryset = queryset.extra(select={as_field: sql}) + return queryset + + +def attach_extra_info(queryset, user=None, include_attachments=False): if include_attachments: queryset = attach_basic_attachments(queryset) queryset = queryset.extra(select={"include_attachments": "True"}) @@ -36,4 +72,5 @@ def attach_extra_info(queryset, user=None, include_attachments=False): queryset = attach_total_watchers_to_queryset(queryset) queryset = attach_is_voter_to_queryset(queryset, user) queryset = attach_is_watcher_to_queryset(queryset, user) + queryset = attach_user_story_extra_info(queryset) return queryset