Improving milestones API

remotes/origin/logger
Alejandro Alonso 2015-12-17 10:10:55 +01:00
parent afb5af1527
commit 8d241479fa
3 changed files with 29 additions and 4 deletions

View File

@ -20,13 +20,15 @@ from taiga.base.api import serializers
from taiga.base.utils import json from taiga.base.utils import json
from taiga.projects.notifications.mixins import WatchedResourceModelSerializer from taiga.projects.notifications.mixins import WatchedResourceModelSerializer
from taiga.projects.notifications.validators import WatchersValidator 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 from . import models
class MilestoneSerializer(WatchersValidator, WatchedResourceModelSerializer, serializers.ModelSerializer): 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") total_points = serializers.SerializerMethodField("get_total_points")
closed_points = serializers.SerializerMethodField("get_closed_points") closed_points = serializers.SerializerMethodField("get_closed_points")
@ -34,6 +36,29 @@ class MilestoneSerializer(WatchersValidator, WatchedResourceModelSerializer, ser
model = models.Milestone model = models.Milestone
read_only_fields = ("id", "created_date", "modified_date") 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): def get_total_points(self, obj):
return sum(obj.total_points.values()) return sum(obj.total_points.values())

View File

@ -55,7 +55,7 @@ class WatchedResourceMixin:
qs = attach_watchers_to_queryset(queryset) qs = attach_watchers_to_queryset(queryset)
qs = attach_total_watchers_to_queryset(qs) qs = attach_total_watchers_to_queryset(qs)
if self.request.user.is_authenticated(): 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 return qs

View File

@ -40,7 +40,7 @@ def attach_watchers_to_queryset(queryset, as_field="watchers"):
return qs 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. """Attach is_watcher boolean to each object of the queryset.
:param user: A users.User object model :param user: A users.User object model