Skip to content

Commit 1527338

Browse files
authored
Merge pull request #244 from American-Institutes-for-Research/HEA-787/season_purpose
Hea 787/season purpose
2 parents eceecae + f3f6796 commit 1527338

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

apps/common/lookups.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def get(self, value: str, **parent_values) -> str | None:
348348
"""
349349
df = pd.DataFrame({"value": [value]})
350350
for parent_field in self.parent_fields:
351-
df[parent_field] = [parent_values[parent_field]]
351+
if parent_field in parent_values:
352+
df[parent_field] = [parent_values[parent_field]]
352353
try:
353354
df = self.do_lookup(df, "value", "result")
354355
result = df.iloc[0, -1]
@@ -372,7 +373,8 @@ def get_instance(self, value: str, **parent_values) -> Model | None:
372373
"""
373374
df = pd.DataFrame({"value": [value]})
374375
for parent_field in self.parent_fields:
375-
df[parent_field] = [parent_values[parent_field]]
376+
if parent_field in parent_values:
377+
df[parent_field] = [parent_values[parent_field]]
376378
try:
377379
df = self.do_lookup(df, "value", "result")
378380
df = self.get_instances(df, "result")

apps/metadata/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class SeasonAdmin(admin.ModelAdmin):
151151
"season_type",
152152
"purpose",
153153
)
154-
ordering = ("order",)
154+
ordering = ("country", "order")
155155

156156

157157
class MarketAdmin(ReferenceDataAdmin):

apps/metadata/migrations/0014_season_purpose.py renamed to apps/metadata/migrations/0015_season_purpose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class Migration(migrations.Migration):
77

88
dependencies = [
9-
("metadata", "0013_wealthcharacteristic_characteristic_group"),
9+
("metadata", "0014_characteristicgroup_and_more"),
1010
]
1111

1212
operations = [

apps/metadata/viewsets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,4 @@ class SeasonViewSet(BaseModelViewSet):
238238
"season_type",
239239
"purpose",
240240
)
241+
ordering = ("country", "order")

pipelines/assets/livelihood_activity.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ def get_all_label_attributes(labels: pd.Series, activity_type: str, country_code
414414
# Add the country_id because it is required for the Season lookup
415415
if country_code:
416416
all_label_attributes["country_id"] = country_code
417+
# The all_label_attributes dataframe should also contain a 'strategy_type' column, which will be used by the
418+
# lookup to restrict the possible matches to Seasons with a matching `purpose` (or those with a null purpose).
417419
all_label_attributes = seasonnamelookup.do_lookup(all_label_attributes, "season", "season")
418420
all_label_attributes["season"] = all_label_attributes["season"].replace(pd.NA, None)
419421

@@ -659,8 +661,16 @@ def get_instances_from_dataframe(
659661
livelihood_zone_baseline.reference_year_end_date.isoformat(),
660662
]
661663

662-
# Save the identifier for Season 2 because we need it when creating MilkProduction instances
663-
season2_name = SeasonNameLookup().get("Season 2", country_id=livelihood_zone_baseline.livelihood_zone.country_id)
664+
# Save the identifier for Season 2 because we need it when creating MilkProduction and ButterProduction instances
665+
seasonnamelookup = SeasonNameLookup()
666+
dairy_season2_names = [
667+
seasonnamelookup.get(
668+
"Season 2", country_id=livelihood_zone_baseline.livelihood_zone.country_id, purpose="MilkProduction"
669+
),
670+
seasonnamelookup.get(
671+
"Season 2", country_id=livelihood_zone_baseline.livelihood_zone.country_id, purpose="ButterProduction"
672+
),
673+
]
664674

665675
# Prepare a lookup for ClassifiedProduct, so it caches and reuses the results of .get() lookups
666676
classifiedproductlookup = ClassifiedProductLookup()
@@ -834,7 +844,7 @@ def get_instances_from_dataframe(
834844
if (
835845
livelihood_strategy["strategy_type"] in ["MilkProduction", "ButterProduction"]
836846
and ("product_id" not in livelihood_strategy or not livelihood_strategy["product_id"])
837-
and livelihood_strategy["season"] == season2_name
847+
and livelihood_strategy["season"] in dairy_season2_names
838848
and previous_livelihood_strategy
839849
and "product_id" in previous_livelihood_strategy
840850
and previous_livelihood_strategy["product_id"]
@@ -846,7 +856,7 @@ def get_instances_from_dataframe(
846856
# if necessary.
847857
if (
848858
livelihood_strategy["strategy_type"] == "MilkProduction"
849-
and livelihood_strategy["season"] == season2_name
859+
and livelihood_strategy["season"] in dairy_season2_names
850860
and previous_livelihood_activities_for_strategy
851861
):
852862
# Assertion to prevent linting from complaining about possible None values
@@ -947,7 +957,14 @@ def get_instances_from_dataframe(
947957
strategy["strategy_type"] == "MilkProduction"
948958
# Season for the current LivelihoodStrategy hasn't been converted to a natural key yet,
949959
# so coerce it to a list for comparison
950-
and strategy["season"] == [livelihood_strategy["season"]]
960+
and strategy["season"]
961+
== [
962+
seasonnamelookup.get(
963+
livelihood_strategy["season_original"],
964+
country_id=livelihood_zone_baseline.livelihood_zone.country_id,
965+
purpose="MilkProduction",
966+
)
967+
]
951968
and strategy["additional_identifier"]
952969
== livelihood_strategy["additional_identifier"]
953970
):

0 commit comments

Comments
 (0)