Skip to content

Commit

Permalink
Add deadline distances to section serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryVisuri committed Aug 25, 2024
1 parent 32b9a54 commit 2a4135a
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions projects/serializers/projectschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Project,
ProjectSubtype,
ProjectCardSectionAttribute,
DeadlineDistance,
)
from projects.models.attribute import AttributeLock, AttributeCategorization
from projects.models.project import (
Expand Down Expand Up @@ -259,12 +260,13 @@ def get_field_subroles(self, attribute):

def get_categorization(self, attribute):
try:
project = self.context["project"]
return attribute.categorizations.get(
common_project_phase=project.phase.common_project_phase,
includes_principles=project.create_principles,
includes_draft=project.create_draft
).value
project = self.context.get("project", None)
if project:
return attribute.categorizations.get(
common_project_phase=project.phase.common_project_phase,
includes_principles=project.create_principles,
includes_draft=project.create_draft
).value
except (KeyError, AttributeCategorization.DoesNotExist):
pass
return ""
Expand Down Expand Up @@ -353,6 +355,27 @@ def _get_attribute_choices(attribute):
return choices


class DeadlineAttributeSchemaSerializer(AttributeSchemaSerializer):
previous_deadline = serializers.SerializerMethodField()
distance_from_previous = serializers.SerializerMethodField()

def get_previous_deadline(self, attribute):
try:
subtype = self.context['project'].subtype
deadline_distance = DeadlineDistance.objects.filter(deadline__subtype=subtype, deadline__attribute=attribute).first()
return deadline_distance.previous_deadline.attribute.identifier if deadline_distance and deadline_distance.previous_deadline and deadline_distance.previous_deadline.attribute else None
except (KeyError, DeadlineDistance.DoesNotExist):
return None

def get_distance_from_previous(self, attribute):
try:
subtype = self.context['project'].subtype
deadline_distance = DeadlineDistance.objects.filter(deadline__subtype=subtype, deadline__attribute=attribute).first()
return deadline_distance.distance_from_previous if deadline_distance and deadline_distance.previous_deadline else None
except (KeyError, DeadlineDistance.DoesNotExist):
return None


class ProjectSectionAttributeSchemaSerializer(serializers.Serializer):
relies_on = serializers.CharField(
source="relies_on.attribute.identifier", allow_null=True
Expand Down Expand Up @@ -607,20 +630,21 @@ class ProjectPhaseDeadlineSectionSerializer(serializers.Serializer):
attributes = serializers.SerializerMethodField("_get_attributes")

@staticmethod
def _get_serialized_attributes(sect_attrs, owner, privilege):
def _get_serialized_attributes(sect_attrs, owner, privilege, project):
serialized = []

for sect_attr in sect_attrs:
serialized.append(AttributeSchemaSerializer(
serialized.append(DeadlineAttributeSchemaSerializer(
sect_attr.attribute,
context={"owner": owner, "privilege": privilege}
context={"owner": owner, "privilege": privilege, "project": project}
).data)

return serialized

def _get_attributes(self, deadline_section):
owner = self.context.get("owner", False)
privilege = self.context.get("privilege", False)
project = self.context.get("project", None)

if privilege == "admin":
sect_attrs = deadline_section.projectphasedeadlinesectionattribute_set \
Expand All @@ -631,7 +655,7 @@ def _get_attributes(self, deadline_section):
sect_attrs = deadline_section.projectphasedeadlinesectionattribute_set \
.filter(owner_field=True).select_related("attribute")

return self._get_serialized_attributes(sect_attrs, owner, privilege)
return self._get_serialized_attributes(sect_attrs, owner, privilege, project)


class ProjectPhaseDeadlineSectionsSerializer(serializers.Serializer):
Expand All @@ -648,7 +672,7 @@ def _get_sections(privilege, owner, phase, project=None):
deadline_sections = [
ProjectPhaseDeadlineSectionSerializer(
section,
context={"privilege": privilege, "owner": owner},
context={"privilege": privilege, "owner": owner, "project": project},
).data
for section in phase.deadline_sections.all()
]
Expand Down Expand Up @@ -690,7 +714,7 @@ def _get_grouped_sections(privilege, owner, phase, project=None):
grouped_sections = [
ProjectPhaseDeadlineSectionSerializer(
section,
context={"privilege": privilege, "owner": owner},
context={"privilege": privilege, "owner": owner, "project": project},
).data
for section in phase.deadline_sections.all()
]
Expand Down

0 comments on commit 2a4135a

Please sign in to comment.