diff --git a/commcare_connect/opportunity/export.py b/commcare_connect/opportunity/export.py index 31e88315..f48ee9bf 100644 --- a/commcare_connect/opportunity/export.py +++ b/commcare_connect/opportunity/export.py @@ -18,13 +18,17 @@ def export_user_visit_data( user_visits = user_visits.filter(status__in=status) table = UserVisitTable(user_visits) - exclude_columns = ("visit_date",) + exclude_columns = ("visit_date", "form_json") columns = [ column for column in table.columns.iterall() if not (column.column.exclude_from_export or column.name in exclude_columns) ] - base_data = [[row.get_cell_value(column.name) for column in columns] for row in table.rows] + base_data = [ + # form_json must be the last column in the row + [row.get_cell_value(column.name) for column in columns] + [row.get_cell_value("form_json")] + for row in table.rows + ] base_headers = [force_str(column.header, strings_only=True) for column in columns] return get_flattened_dataset(base_headers, base_data) @@ -45,7 +49,7 @@ def get_flattened_dataset(headers: list[str], data: list[list]) -> Dataset: schema.update(flat_json.keys()) schema = sorted(schema, key=_schema_sort) - headers = headers[:-1] + schema + headers = headers + schema dataset = Dataset(title="Export", headers=headers) for row, flat_json in zip(data, flat_data): diff --git a/commcare_connect/opportunity/tables.py b/commcare_connect/opportunity/tables.py index 2442c63c..f20d5531 100644 --- a/commcare_connect/opportunity/tables.py +++ b/commcare_connect/opportunity/tables.py @@ -30,6 +30,8 @@ class UserVisitTable(tables.Table): ) deliver_unit = columns.Column("Unit Name", accessor="deliver_unit.name") + entity_id = columns.Column("Entity ID", accessor="entity_id", visible=False) + entity_name = columns.Column("Entity Name", accessor="entity_name") class Meta: model = UserVisit diff --git a/commcare_connect/opportunity/tests/test_export.py b/commcare_connect/opportunity/tests/test_export.py index 5c27ab62..3c7b6aa2 100644 --- a/commcare_connect/opportunity/tests/test_export.py +++ b/commcare_connect/opportunity/tests/test_export.py @@ -26,6 +26,8 @@ def test_export_user_visit_data(mobile_user_with_connect_link): user=mobile_user_with_connect_link, visit_date=date2, deliver_unit=deliver_unit, + entity_id="abc", + entity_name="A B C", form_json={"form": {"name": "test_form2", "group": {"q": "b"}}}, ), ] @@ -35,9 +37,9 @@ def test_export_user_visit_data(mobile_user_with_connect_link): name = mobile_user_with_connect_link.name assert exporter.export("csv") == ( - "Visit ID,Visit date,Status,Username,Name of User,Unit Name,form.name,form.group.q\r\n" - f",{date1.isoformat()},Pending,{username},{name},{deliver_unit.name},test_form1,\r\n" - f",{date2.isoformat()},Pending,{username},{name},{deliver_unit.name},test_form2,b\r\n" + "Visit ID,Visit date,Status,Username,Name of User,Unit Name,Entity ID,Entity Name,form.name,form.group.q\r\n" + f",{date1.isoformat()},Pending,{username},{name},{deliver_unit.name},,,test_form1,\r\n" + f",{date2.isoformat()},Pending,{username},{name},{deliver_unit.name},abc,A B C,test_form2,b\r\n" ) @@ -57,7 +59,7 @@ def test_export_user_visit_data(mobile_user_with_connect_link): ], ) def test_get_flattened_dataset(data, expected): - headers = ["header1", "header2", "header3"] + headers = ["header1", "header2"] data = [ ["value1", "value2", data], ]