US#4445: Add milestones to project detail

remotes/origin/issue/4795/notification_even_they_are_disabled
Jesús Espino 2016-07-28 12:45:45 +02:00 committed by David Barragán Merino
parent 0064e6e82a
commit 0f15f671f5
2 changed files with 34 additions and 0 deletions

View File

@ -356,6 +356,14 @@ class ProjectDetailSerializer(ProjectSerializer):
tasks_csv_uuid = Field()
userstories_csv_uuid = Field()
transfer_token = Field()
milestones = MethodField()
def get_milestones(self, obj):
assert hasattr(obj, "milestones_attr"), "instance must have a milestones_attr attribute"
if obj.milestones_attr is None:
return []
return obj.milestones_attr
def to_value(self, instance):
# Name attributes must be translated

View File

@ -50,6 +50,31 @@ def attach_members(queryset, as_field="members_attr"):
return queryset
def attach_milestones(queryset, as_field="milestones_attr"):
"""Attach a json milestons representation to each object of the queryset.
:param queryset: A Django projects queryset object.
:param as_field: Attach the milestones as an attribute with this name.
:return: Queryset object with the additional `as_field` field.
"""
model = queryset.model
sql = """SELECT json_agg(row_to_json(t))
FROM(
SELECT
milestones_milestone.id,
milestones_milestone.slug,
milestones_milestone.name,
milestones_milestone.closed
FROM milestones_milestone
WHERE milestones_milestone.project_id = {tbl}.id
ORDER BY estimated_start) t"""
sql = sql.format(tbl=model._meta.db_table)
queryset = queryset.extra(select={as_field: sql})
return queryset
def attach_closed_milestones(queryset, as_field="closed_milestones_attr"):
"""Attach a closed milestones counter to each object of the queryset.
@ -432,5 +457,6 @@ def attach_extra_info(queryset, user=None):
queryset = attach_my_role_permissions(queryset, user)
queryset = attach_private_projects_same_owner(queryset, user)
queryset = attach_public_projects_same_owner(queryset, user)
queryset = attach_milestones(queryset)
return queryset