Skip to content

Commit c3b6618

Browse files
committed
PER data-fetcher functionality to backend side, v1.3
1 parent 4f6a14a commit c3b6618

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

per/drf_views.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ def _contains_affirmative(text: str) -> bool:
125125
return any(word in normalized for word in AFFIRMATIVE_WORDS)
126126

127127

128+
def _phase_display_from_int(phase: int | None, existing_display: str | None = None) -> str | None:
129+
"""Return normalized phase display using Overview.Phase IntegerChoices.
130+
131+
Uses the IntegerChoices label, then normalizes:
132+
- "WorkPlan" -> "Workplan"
133+
- "Action And Accountability" -> "Action & accountability"
134+
"""
135+
label = None
136+
try:
137+
if isinstance(phase, int):
138+
label = Overview.Phase(phase).label # from IntegerChoices
139+
except Exception:
140+
label = None
141+
disp = label or existing_display
142+
if disp == "Action And Accountability":
143+
return "Action & accountability"
144+
if disp == "WorkPlan":
145+
return "Workplan"
146+
return disp
147+
148+
128149
class PERDocsFilter(filters.FilterSet):
129150
id = filters.NumberFilter(field_name="id", lookup_expr="exact")
130151

@@ -659,11 +680,7 @@ def list(self, request, *args, **kwargs):
659680
results = data.get("results") if isinstance(data, dict) else data
660681
if results:
661682
for row in results:
662-
pd = row.get("phase_display")
663-
if pd == "Action And Accountability":
664-
row["phase_display"] = "Action & accountability"
665-
elif pd == "WorkPlan":
666-
row["phase_display"] = "Workplan"
683+
row["phase_display"] = _phase_display_from_int(row.get("phase"), row.get("phase_display"))
667684
return Response(data)
668685

669686

@@ -734,13 +751,8 @@ def get(self, request):
734751
)
735752
items = []
736753
for ov in latest_overviews:
737-
# Normalize phase display and keep raw phase if available
738-
phase_display = getattr(ov, "phase_display", None)
739-
normalized_phase_display = phase_display
740-
if phase_display == "Action And Accountability":
741-
normalized_phase_display = "Action & accountability"
742-
elif phase_display == "WorkPlan":
743-
normalized_phase_display = "Workplan"
754+
# Compute normalized phase display from int value or existing string
755+
normalized_phase_display = _phase_display_from_int(getattr(ov, "phase", None), getattr(ov, "phase_display", None))
744756

745757
# Attach components from latest assessment tied to the overview
746758
components = []

0 commit comments

Comments
 (0)