Skip to content

Commit 2ba577b

Browse files
committed
Address PR feedback by adding _missing_ class method to fall back for french aliases see HEA-809
1 parent 7319b5b commit 2ba577b

2 files changed

Lines changed: 52 additions & 54 deletions

File tree

apps/baseline/models.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,44 +1155,14 @@ class HouseholdLaborProvider(models.TextChoices):
11551155
ALL = "all", _("All Together")
11561156

11571157
@classmethod
1158-
def get_aliases(cls):
1158+
def _missing_(cls, value):
11591159
"""
1160-
Return a dict mapping alias labels to their canonical values.
1160+
Called when the value is missing from the enum
11611161
"""
1162-
return {
1163-
# French singular/plural for men
1164-
"hommes": cls.MEN,
1165-
"homme": cls.MEN,
1166-
# French singular/plural for women
1167-
"femmes": cls.WOMEN,
1168-
"femme": cls.WOMEN,
1169-
# French singular/plural for boys
1170-
"garçons": cls.BOYS,
1171-
"garçon": cls.BOYS,
1172-
"garcons": cls.BOYS, # without accent
1173-
"garcon": cls.BOYS, # without accent
1174-
# French singular/plural for girls
1175-
"filles": cls.GIRLS,
1176-
"fille": cls.GIRLS,
1177-
# French for adults
1178-
"adultes": cls.ADULTS,
1179-
# Children combinations (boys/girls in any order)
1180-
"boys/girls": cls.CHILDREN,
1181-
"girls/boys": cls.CHILDREN,
1182-
"garçons/filles": cls.CHILDREN,
1183-
"filles/garçons": cls.CHILDREN,
1184-
"garcons/filles": cls.CHILDREN, # without accent
1185-
"filles/garcons": cls.CHILDREN, # without accent
1186-
# Adults combinations (men/women in any order)
1187-
"men/women": cls.ADULTS,
1188-
"women/men": cls.ADULTS,
1189-
"men & women": cls.ADULTS,
1190-
"women & men": cls.ADULTS,
1191-
"hommes/femmes": cls.ADULTS,
1192-
"femmes/hommes": cls.ADULTS,
1193-
"hommes & femmes": cls.ADULTS,
1194-
"femmes & hommes": cls.ADULTS,
1195-
}
1162+
value_lower = str(value).lower()
1163+
if hasattr(cls, "_aliases") and value_lower in cls._aliases:
1164+
return cls(cls._aliases[value_lower])
1165+
return None
11961166

11971167
@classmethod
11981168
def get_all_labels(cls):
@@ -1201,10 +1171,45 @@ def get_all_labels(cls):
12011171
"""
12021172
canonical_values = [value for value, _label in cls.choices]
12031173
display_labels = [str(label) for _value, label in cls.choices]
1204-
alias_labels = list(cls.get_aliases().keys())
1174+
alias_labels = list(cls._aliases.keys()) if hasattr(cls, "_aliases") else []
12051175
all_labels = canonical_values + display_labels + alias_labels
12061176
return sorted(all_labels, key=len, reverse=True)
12071177

1178+
HouseholdLaborProvider._aliases = {
1179+
# French singular/plural for men
1180+
"hommes": "men",
1181+
"homme": "men",
1182+
# French singular/plural for women
1183+
"femmes": "women",
1184+
"femme": "women",
1185+
# French singular/plural for boys
1186+
"garçons": "boys",
1187+
"garçon": "boys",
1188+
"garcons": "boys", # without accent
1189+
"garcon": "boys", # without accent
1190+
# French singular/plural for girls
1191+
"filles": "girls",
1192+
"fille": "girls",
1193+
# French for adults
1194+
"adultes": "adults",
1195+
# Children combinations (boys/girls in any order)
1196+
"boys/girls": "children",
1197+
"girls/boys": "children",
1198+
"garçons/filles": "children",
1199+
"filles/garçons": "children",
1200+
"garcons/filles": "children", # without accent
1201+
"filles/garcons": "children", # without accent
1202+
# Adults combinations (men/women in any order)
1203+
"men/women": "adults",
1204+
"women/men": "adults",
1205+
"men & women": "adults",
1206+
"women & men": "adults",
1207+
"hommes/femmes": "adults",
1208+
"femmes/hommes": "adults",
1209+
"hommes & femmes": "adults",
1210+
"femmes & hommes": "adults",
1211+
}
1212+
12081213
household_labor_provider = models.CharField(
12091214
max_length=10, choices=HouseholdLaborProvider.choices, blank=True, verbose_name=_("Activity done by")
12101215
)

pipelines/assets/livelihood_activity.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -289,23 +289,16 @@ def get_livelihood_activity_regular_expression_attributes(label: str) -> dict:
289289

290290
# Map household_labor_provider to canonical values using TextChoices
291291
if "household_labor_provider" in attributes and attributes["household_labor_provider"]:
292-
hlp_label = attributes["household_labor_provider"].lower()
293-
# First check if it's already a canonical value
294-
canonical_values = [value for value, _ in LivelihoodActivity.HouseholdLaborProvider.choices]
295-
if hlp_label in canonical_values:
296-
# Already a canonical value, use as-is
297-
attributes["household_labor_provider"] = hlp_label
298-
else:
299-
# Check if it's an alias
300-
aliases = LivelihoodActivity.HouseholdLaborProvider.get_aliases()
301-
if hlp_label in aliases:
302-
attributes["household_labor_provider"] = aliases[hlp_label]
303-
else:
304-
# Check if it's a display label
305-
for choice_value, choice_label in LivelihoodActivity.HouseholdLaborProvider.choices:
306-
if str(choice_label).lower() == hlp_label:
307-
attributes["household_labor_provider"] = choice_value
308-
break
292+
try:
293+
hlp = LivelihoodActivity.HouseholdLaborProvider(attributes["household_labor_provider"].lower())
294+
attributes["household_labor_provider"] = hlp.value
295+
except ValueError:
296+
# Check if it's a display label
297+
hlp_label = attributes["household_labor_provider"].lower()
298+
for choice_value, choice_label in LivelihoodActivity.HouseholdLaborProvider.choices:
299+
if str(choice_label).lower() == hlp_label:
300+
attributes["household_labor_provider"] = choice_value
301+
break
309302

310303
attributes["activity_label"] = label
311304
attributes["strategy_type"] = strategy_type

0 commit comments

Comments
 (0)