taiga-back/taiga/projects/milestones/serializers.py

70 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (C) 2014-2016 Andrey Antukh <niwi@niwi.nz>
# Copyright (C) 2014-2016 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014-2016 David Barragán <bameda@dbarragan.com>
# Copyright (C) 2014-2016 Alejandro Alonso <alejandro.alonso@kaleidos.net>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.utils.translation import ugettext as _
from taiga.base.api import serializers
from taiga.base.utils import json
from taiga.projects.notifications.mixins import WatchedResourceModelSerializer
from taiga.projects.notifications.mixins import ListWatchedResourceModelSerializer
from taiga.projects.notifications.validators import WatchersValidator
from taiga.projects.mixins.serializers import ValidateDuplicatedNameInProjectMixin
from taiga.projects.userstories.serializers import UserStoryListSerializer
from . import models
import serpy
class MilestoneSerializer(WatchersValidator, WatchedResourceModelSerializer,
ValidateDuplicatedNameInProjectMixin):
total_points = serializers.SerializerMethodField("get_total_points")
closed_points = serializers.SerializerMethodField("get_closed_points")
class Meta:
model = models.Milestone
read_only_fields = ("id", "created_date", "modified_date")
def get_total_points(self, obj):
return sum(obj.total_points.values())
def get_closed_points(self, obj):
return sum(obj.closed_points.values())
class MilestoneListSerializer(ListWatchedResourceModelSerializer, serializers.LightSerializer):
id = serpy.Field()
name = serpy.Field()
slug = serpy.Field()
owner = serpy.Field(attr="owner_id")
project = serpy.Field(attr="project_id")
estimated_start = serpy.Field()
estimated_finish = serpy.Field()
created_date = serpy.Field()
modified_date = serpy.Field()
closed = serpy.Field()
disponibility = serpy.Field()
order = serpy.Field()
watchers = serpy.Field()
user_stories = serpy.MethodField("get_user_stories")
total_points = serializers.Field(source="total_points_attr")
closed_points = serializers.Field(source="closed_points_attr")
def get_user_stories(self, obj):
return UserStoryListSerializer(obj.user_stories.all(), many=True).data