diff --git a/taiga/projects/milestones/serializers.py b/taiga/projects/milestones/serializers.py index e189b5fd..a622ea0f 100644 --- a/taiga/projects/milestones/serializers.py +++ b/taiga/projects/milestones/serializers.py @@ -20,13 +20,15 @@ from taiga.base.api import serializers from taiga.base.utils import json from taiga.projects.notifications.mixins import WatchedResourceModelSerializer from taiga.projects.notifications.validators import WatchersValidator +from taiga.projects.votes.utils import attach_total_voters_to_queryset, attach_is_voter_to_queryset +from taiga.projects.notifications.utils import attach_watchers_to_queryset, attach_is_watcher_to_queryset -from ..userstories.serializers import UserStorySerializer +from ..userstories.serializers import UserStoryListSerializer from . import models class MilestoneSerializer(WatchersValidator, WatchedResourceModelSerializer, serializers.ModelSerializer): - user_stories = UserStorySerializer(many=True, required=False, read_only=True) + user_stories = serializers.SerializerMethodField("get_user_stories") total_points = serializers.SerializerMethodField("get_total_points") closed_points = serializers.SerializerMethodField("get_closed_points") @@ -34,6 +36,29 @@ class MilestoneSerializer(WatchersValidator, WatchedResourceModelSerializer, ser model = models.Milestone read_only_fields = ("id", "created_date", "modified_date") + def get_user_stories(self, obj): + qs = obj.user_stories.prefetch_related("role_points", + "role_points__points", + "role_points__role") + + qs = qs.select_related("milestone", + "project", + "status", + "owner", + "assigned_to", + "generated_from_issue") + + request = self.context.get("request", None) + requesting_user = request and request.user or None + if requesting_user and requesting_user.is_authenticated(): + qs = attach_is_voter_to_queryset(requesting_user, qs) + qs = attach_is_watcher_to_queryset(requesting_user, qs) + + qs = attach_total_voters_to_queryset(qs) + qs = attach_watchers_to_queryset(qs) + + return UserStoryListSerializer(qs, many=True).data + def get_total_points(self, obj): return sum(obj.total_points.values()) diff --git a/taiga/projects/notifications/mixins.py b/taiga/projects/notifications/mixins.py index ede48093..26726703 100644 --- a/taiga/projects/notifications/mixins.py +++ b/taiga/projects/notifications/mixins.py @@ -55,7 +55,7 @@ class WatchedResourceMixin: qs = attach_watchers_to_queryset(queryset) qs = attach_total_watchers_to_queryset(qs) if self.request.user.is_authenticated(): - qs = attach_is_watcher_to_queryset(qs, self.request.user) + qs = attach_is_watcher_to_queryset(self.request.user, qs) return qs diff --git a/taiga/projects/notifications/utils.py b/taiga/projects/notifications/utils.py index 49aea78d..b8138e0a 100644 --- a/taiga/projects/notifications/utils.py +++ b/taiga/projects/notifications/utils.py @@ -40,7 +40,7 @@ def attach_watchers_to_queryset(queryset, as_field="watchers"): return qs -def attach_is_watcher_to_queryset(queryset, user, as_field="is_watcher"): +def attach_is_watcher_to_queryset(user, queryset, as_field="is_watcher"): """Attach is_watcher boolean to each object of the queryset. :param user: A users.User object model