Skip to content

Commit

Permalink
add entity ID and name to table and export
Browse files Browse the repository at this point in the history
  • Loading branch information
snopoke committed Sep 29, 2023
1 parent bbc958e commit f5f3321
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 7 additions & 3 deletions commcare_connect/opportunity/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions commcare_connect/opportunity/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions commcare_connect/opportunity/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}},
),
]
Expand All @@ -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"
)


Expand All @@ -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],
]
Expand Down

0 comments on commit f5f3321

Please sign in to comment.