fixed consumer and ethnicity filter combinations

This commit is contained in:
2026-02-03 14:43:03 +01:00
parent 8dd41dfc96
commit e44251c3d6
2 changed files with 34 additions and 8 deletions

View File

@@ -60,11 +60,24 @@ def get_filter_combinations(survey: QualtricsSurvey) -> list[dict]:
'filters': {'gender': [gender]}
})
# Ethnicity - one at a time
for ethnicity in survey.options_ethnicity:
# Ethnicity - grouped by individual values
# Ethnicity options are comma-separated (e.g., "White or Caucasian, Hispanic or Latino")
# Create filters that include ALL options containing each individual ethnicity value
ethnicity_values = set()
for ethnicity_option in survey.options_ethnicity:
# Split by comma and strip whitespace
values = [v.strip() for v in ethnicity_option.split(',')]
ethnicity_values.update(values)
for ethnicity_value in sorted(ethnicity_values):
# Find all options that contain this value
matching_options = [
opt for opt in survey.options_ethnicity
if ethnicity_value in [v.strip() for v in opt.split(',')]
]
combinations.append({
'name': f'Ethnicity-{ethnicity}',
'filters': {'ethnicity': [ethnicity]}
'name': f'Ethnicity-{ethnicity_value}',
'filters': {'ethnicity': matching_options}
})
# Income - one at a time
@@ -74,11 +87,24 @@ def get_filter_combinations(survey: QualtricsSurvey) -> list[dict]:
'filters': {'income': [income]}
})
# Consumer segments - one at a time
# Consumer segments - combine _A and _B options
# Group options by base name (removing _A/_B suffix)
consumer_groups = {}
for consumer in survey.options_consumer:
# Check if ends with _A or _B
if consumer.endswith('_A') or consumer.endswith('_B'):
base_name = consumer[:-2] # Remove last 2 chars (_A or _B)
if base_name not in consumer_groups:
consumer_groups[base_name] = []
consumer_groups[base_name].append(consumer)
else:
# Not an _A/_B option, keep as-is
consumer_groups[consumer] = [consumer]
for base_name, options in consumer_groups.items():
combinations.append({
'name': f'Consumer-{consumer}',
'filters': {'consumer': [consumer]}
'name': f'Consumer-{base_name}',
'filters': {'consumer': options}
})
return combinations