Skip to content

Commit

Permalink
refactor: pass task_count instead of task_id manually to xlsform
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Sep 17, 2024
1 parent 2d44d2d commit d5d7806
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
17 changes: 10 additions & 7 deletions osm_fieldwork/update_xlsform.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,21 @@ def append_select_one_from_file_row(df: pd.DataFrame, entity_name: str) -> pd.Da
return pd.concat([top_df, additional_row, bottom_df], ignore_index=True)


def append_task_ids_to_choices_sheet(df: pd.DataFrame, task_ids: list[int]) -> pd.DataFrame:
def append_task_ids_to_choices_sheet(df: pd.DataFrame, task_count: int) -> pd.DataFrame:
"""Add task id rows to choices sheet (for filtering Entity list)."""
task_ids = list(range(1, task_count + 1))

additional_rows = pd.DataFrame(
{
"list_name": ["task_filter"] * len(task_ids),
"list_name": ["task_filter"] * task_count,
"name": task_ids,
"label::English(en)": task_ids,
"label::Swahili(sw)": task_ids,
"label::French(fr)": task_ids,
"label::Spanish(es)": task_ids,
}
)

df = pd.concat([df, additional_rows], ignore_index=True)
return df

Expand All @@ -143,7 +146,7 @@ def append_mandatory_fields(
custom_form: BytesIO,
form_category: str,
additional_entities: list[str] = None,
task_ids: list[int] = None,
task_count: int = None,
existing_id: str = None,
) -> BytesIO:
"""Append mandatory fields to the XLSForm for use in FMTM.
Expand All @@ -155,8 +158,8 @@ def append_mandatory_fields(
reference an additional Entity list (set of geometries).
The values should be plural, so that 's' will be stripped in the
field name.
task_ids(list[int]): add task ids to choices sheet.
These are used to filter Entities by task id in ODK Collect.
task_count(int): number of tasks, used to generate task_id entries in choices
sheet. These are used to filter Entities by task id in ODK Collect.
existing_id(str): an existing UUID to use for the form_id, else random uuid4.
Returns:
Expand Down Expand Up @@ -201,8 +204,8 @@ def append_mandatory_fields(
custom_sheets["survey"] = append_select_one_from_file_row(custom_sheets["survey"], entity_name)

# Append task id rows to choices sheet
if task_ids:
custom_sheets["choices"] = append_task_ids_to_choices_sheet(custom_sheets["choices"], task_ids)
if task_count:
custom_sheets["choices"] = append_task_ids_to_choices_sheet(custom_sheets["choices"], task_count)

# Return spreadsheet wrapped as BytesIO memory object
output = BytesIO()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_update_xlsform.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ def test_add_task_ids_to_choices():
with open(test_form, "rb") as xlsform:
form_bytes = BytesIO(xlsform.read())

task_ids = [1, 2, 3, 4, 5, 6, 7]
updated_form = append_mandatory_fields(form_bytes, "buildings", task_ids=task_ids)
task_count = 7
updated_form = append_mandatory_fields(form_bytes, "buildings", task_count=task_count)
workbook = load_workbook(filename=BytesIO(updated_form.getvalue()))

survey_sheet = workbook["choices"]
# Assuming 'name' is in column B
name_column = [cell.value for cell in survey_sheet["B"]]

# Assert each task_id is in the name_column
task_ids = [1, 2, 3, 4, 5, 6, 7]
for task_id in task_ids:
assert task_id in name_column, f"Task ID {task_id} not found in the choices sheet."

0 comments on commit d5d7806

Please sign in to comment.