@@ -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