Skip to content

Commit

Permalink
Add 'Other' label text for all languages defined by form
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Jan 17, 2024
1 parent f8d9330 commit f8a4ec1
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 102 deletions.
40 changes: 35 additions & 5 deletions pyxform/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,32 @@ def _add_other_option_to_multiple_choice_question(d: Dict[str, Any]) -> None:
choice_list = d.get(const.CHOICES, d.get(const.CHILDREN, []))
if len(choice_list) <= 0:
raise PyXFormError("There should be choices for this question.")
if OR_OTHER_CHOICE not in choice_list:
choice_list.append(OR_OTHER_CHOICE)
if not any(
OR_OTHER_CHOICE[const.NAME] in choice[const.NAME] for choice in choice_list
):
choice_list.append(
SurveyElementBuilder._get_translated_other_label(choice_list)
)

@staticmethod
def _get_translated_other_label(choice_list) -> Dict[str, Any]:
translated_other_choice = OR_OTHER_CHOICE.copy()

if isinstance(choice_list[0][const.LABEL], Dict):
# Get all langs defined across all existing choices in this list and give them all a label of "Other"
langs = set().union(
*map(
set,
[
itemset_choice[const.LABEL].keys()
for itemset_choice in choice_list
],
)
)
translated_other_choice[const.LABEL] = dict(
map(lambda l: (l, OR_OTHER_CHOICE[const.LABEL]), langs)
)
return translated_other_choice

@staticmethod
def _add_none_option_to_select_all_that_apply(d_copy):
Expand Down Expand Up @@ -265,13 +289,19 @@ def _create_section_from_dict(self, d):
if child[const.TYPE].endswith(const.SELECT_OR_OTHER_SUFFIX):
select_question = survey_element[0]
itemset_choices = self._choices.get(select_question[const.ITEMSET], None)
translated_other_choice = OR_OTHER_CHOICE.copy()
if (
itemset_choices is not None
and isinstance(itemset_choices, list)
and OR_OTHER_CHOICE not in itemset_choices
and not any(
OR_OTHER_CHOICE[const.NAME] in choice[const.NAME]
for choice in itemset_choices
)
):
itemset_choices.append(OR_OTHER_CHOICE)
# This is required for builder_tests.BuilderTests.test_loop to pass.
itemset_choices.append(
self._get_translated_other_label(itemset_choices)
)
# Select choices need to be both in choices and children. See the loop test in builder_tests
self._add_other_option_to_multiple_choice_question(d=child)
if survey_element:
result.add_children(survey_element)
Expand Down
6 changes: 0 additions & 6 deletions pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,12 +1213,6 @@ def workbook_to_json(
if parse_dict.get("specify_other") is not None:
sheet_translations.or_other_seen = True
select_type += constants.SELECT_OR_OTHER_SUFFIX
if row.get(constants.CHOICE_FILTER):
msg = (
ROW_FORMAT_STRING % row_number
+ " Choice filter not supported with or_other."
)
raise PyXFormError(msg)

new_json_dict = row.copy()
new_json_dict[constants.TYPE] = select_type
Expand Down
10 changes: 5 additions & 5 deletions tests/builder_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_specify_other(self):
"sexes": [
{"label": {"English": "Male"}, "name": "male"},
{"label": {"English": "Female"}, "name": "female"},
{"label": "Other", "name": "other"},
{"label": {"English": "Other"}, "name": "other"},
]
},
"children": [
Expand All @@ -144,7 +144,7 @@ def test_specify_other(self):
# json2xform half that will need to change)
{"name": "male", "label": {"English": "Male"}},
{"name": "female", "label": {"English": "Female"}},
{"name": "other", "label": "Other"},
{"name": "other", "label": {"English": "Other"}},
],
},
{
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_loop(self):
"name": "open_pit_latrine",
},
{"label": {"english": "Bucket system"}, "name": "bucket_system"},
{"label": "Other", "name": "other"},
{"label": {"english": "Other"}, "name": "other"},
]
},
"children": [
Expand Down Expand Up @@ -261,7 +261,7 @@ def test_loop(self):
# u'name': u'none',
# u'label': u'None',
# },
{"name": "other", "label": "Other"},
{"name": "other", "label": {"english": "Other"}},
],
},
{
Expand Down Expand Up @@ -329,7 +329,7 @@ def test_loop(self):
"type": "integer",
}
],
"label": "Other",
"label": {"english": "Other"},
"name": "other",
"type": "group",
},
Expand Down
Binary file added tests/example_xls/~$attribute_columns_test.xlsx
Binary file not shown.
1 change: 0 additions & 1 deletion tests/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,6 @@ def test_repeat_count_item_with_same_suffix_as_repeat_is_ok(self):
"""
self.assertPyxformXform(
md=md,
debug=True,
xml__xpath_match=[
# repeat references existing count element directly.
"""
Expand Down
Loading

0 comments on commit f8a4ec1

Please sign in to comment.