Add epics info in user_story_extra_info in task serializer
parent
e68079b769
commit
94ea299c39
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue