From 45a6f6ab6ac48de161e0279d17c0e10e8de9a0bf Mon Sep 17 00:00:00 2001 From: Sushil Tiwari Date: Thu, 3 Oct 2024 13:11:15 +0545 Subject: [PATCH] Fetching the latest fdrs data only --- databank/management/commands/sources/FDRS.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/databank/management/commands/sources/FDRS.py b/databank/management/commands/sources/FDRS.py index 9e7512603e..de8419ffa0 100644 --- a/databank/management/commands/sources/FDRS.py +++ b/databank/management/commands/sources/FDRS.py @@ -25,7 +25,7 @@ ("KPI_expenditureLC_CHF", CO.expenditures), ("KPI_PeopleVol_Tot", CO.volunteers), ("KPI_TrainFA_Tot", CO.trained_in_first_aid), - ("KPI_noLocalUnits", CO.branches), + ("KPI_noBranches", CO.branches), # volunteers ("KPI_PeopleVol_M_age_13_17", CO.male_volunteer_age_13_17), ("KPI_PeopleVol_M_age_18_29", CO.male_volunteer_age_18_29), @@ -107,8 +107,9 @@ def prefetch(): return ( { # KEY -: {year: '', value: ''} + # NOTE: We are fetching the latest data for each indicators f"{ns_iso_map[ns_data['id']].upper()}-{indicator_data['id']}": ( - ns_data["data"][-1] if (ns_data["data"] and len(ns_data["data"]) > 0) else None + max(ns_data["data"], key=lambda x: x["year"]) if (ns_data["data"] and len(ns_data["data"]) > 0) else None ) for indicator_data in requests.get(FDRS_DATA_API_ENDPOINT).json()["data"] for ns_data in indicator_data["data"] @@ -127,12 +128,12 @@ def load(country, overview, fdrs_data): int(item["year"]) for item in fdrs_data.values() if item is not None and item.get("year") is not None ) + # NOTE: We are getting the only latest year specific data for fdrs_indicator, field in FDRS_INDICATORS_FIELD_MAP: - value = fdrs_data.get(f"{country.iso.upper()}-{fdrs_indicator}") - setattr( - overview, - field.field.name, - value and value.get("value"), - ) + data = fdrs_data.get(f"{country.iso.upper()}-{fdrs_indicator}") + value = None + if data and data.get("year") == fdrs_data_fetched_year: + value = data.get("value") + setattr(overview, field.field.name, value) overview.fdrs_data_fetched_year = str(fdrs_data_fetched_year) overview.save()