Skip to content

Commit 36808c4

Browse files
Merge pull request #91 from unicef/bugfix/248006-import-data-rdi-detail-column-label-not-working
AB#248006: Import Data RDI Detail column label not working
2 parents a865d54 + 604ba2d commit 36808c4

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/country_workspace/datasources/rdi.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ def process_households(sheet: Sheet, job: AsyncJob, batch: Batch, config: Config
6868
mapping = {}
6969

7070
for i, row in enumerate(sheet, 1):
71-
name = get_value(row, config["master_column_label"])
7271
household_key = get_value(row, config["household_pk_col"])
72+
label = get_value(row, config["detail_column_label"])
7373

7474
try:
7575
mapping[household_key] = cast(
7676
Household,
7777
job.program.households.create(
7878
batch=batch,
79-
name=name,
79+
name=label,
8080
flex_fields=clean_field_names(row),
8181
),
8282
)
@@ -86,14 +86,22 @@ def process_households(sheet: Sheet, job: AsyncJob, batch: Batch, config: Config
8686
return mapping
8787

8888

89+
def full_name_column(row: Record) -> str | None:
90+
for key in row:
91+
if key.startswith("full") and "name" in key:
92+
return key
93+
return None
94+
95+
8996
def process_individuals(
9097
sheet: Sheet, household_mapping: Mapping[int, Household], job: AsyncJob, batch: Batch, config: Config
9198
) -> int:
9299
processed = 0
93100

94101
for i, row in enumerate(sheet, 1):
95-
name = get_value(row, config["detail_column_label"])
96-
household_key = get_value(row, config["household_pk_col"])
102+
name_column = full_name_column(row)
103+
name = get_value(row, name_column) if name_column else None
104+
household_key = get_value(row, config["master_column_label"])
97105
household = household_mapping.get(household_key)
98106

99107
try:

src/country_workspace/workspaces/admin/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ImportFileForm(forms.Form):
5050

5151
detail_column_label = forms.CharField(
5252
required=False,
53-
initial="full_name_i_c",
53+
initial="household_id",
5454
help_text="Which column should be used as label for the household. It can use interpolation",
5555
)
5656

tests/datasources/test_rdi.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
extract_images,
2323
merge_images,
2424
read_sheets,
25+
full_name_column,
2526
# validate_households,
2627
)
2728
from country_workspace.models import Household
@@ -33,6 +34,7 @@
3334
HOUSEHOLD_2_PK = 2
3435
HOUSEHOLD_1_NAME = "Household 1"
3536
HOUSEHOLD_2_NAME = "Household 2"
37+
FULL_NAME_COLUMN = "full_name"
3638

3739

3840
@pytest.fixture
@@ -50,21 +52,21 @@ def config() -> Config:
5052
@pytest.fixture
5153
def household_sheet(config: Config) -> Sheet:
5254
return [
53-
{config["master_column_label"]: HOUSEHOLD_1_NAME, config["household_pk_col"]: HOUSEHOLD_1_PK},
54-
{config["master_column_label"]: HOUSEHOLD_1_NAME, config["household_pk_col"]: HOUSEHOLD_2_PK},
55+
{config["detail_column_label"]: HOUSEHOLD_1_NAME, config["household_pk_col"]: HOUSEHOLD_1_PK},
56+
{config["detail_column_label"]: HOUSEHOLD_1_NAME, config["household_pk_col"]: HOUSEHOLD_2_PK},
5557
]
5658

5759

5860
@pytest.fixture
5961
def individual_sheet(config: Config) -> Sheet:
6062
return [
6163
{
62-
config["detail_column_label"]: "John Doe",
63-
config["household_pk_col"]: HOUSEHOLD_1_PK,
64+
FULL_NAME_COLUMN: "John Doe",
65+
config["master_column_label"]: HOUSEHOLD_1_PK,
6466
},
6567
{
66-
config["detail_column_label"]: "Doe John",
67-
config["household_pk_col"]: HOUSEHOLD_2_PK,
68+
FULL_NAME_COLUMN: "Doe John",
69+
config["master_column_label"]: HOUSEHOLD_2_PK,
6870
},
6971
]
7072

@@ -135,7 +137,7 @@ def test_process_households(mocker: MockerFixture, config: Config, household_she
135137
}
136138
job.program.households.create.assert_has_calls(
137139
[
138-
call(batch=batch, name=row[config["master_column_label"]], flex_fields=clean_field_names_mock.return_value)
140+
call(batch=batch, name=row[config["detail_column_label"]], flex_fields=clean_field_names_mock.return_value)
139141
for row in household_sheet
140142
]
141143
)
@@ -166,8 +168,8 @@ def test_process_individuals(
166168
[
167169
call(
168170
batch=batch_mock,
169-
name=row[config["detail_column_label"]],
170-
household_id=household_mapping[row[config["household_pk_col"]]].pk,
171+
name=row[FULL_NAME_COLUMN],
172+
household_id=household_mapping[row[config["master_column_label"]]].pk,
171173
flex_fields=clean_field_names_mock.return_value,
172174
)
173175
for row in individual_sheet
@@ -305,3 +307,15 @@ def test_read_sheets(mocker: MockerFixture) -> None:
305307
extract_images_mock.assert_called_once_with(filepath, sheet_index)
306308
merge_images_mock.assert_called_once_with(sheet, images)
307309
filter_rows_with_household_pk_mock.assert_called_once_with(config_mock, merge_images_mock.return_value)
310+
311+
312+
@pytest.mark.parametrize(
313+
("record", "expected"),
314+
[
315+
({"full_name": "John Smith"}, "full_name"),
316+
({}, None),
317+
({"name_full": "John Smith"}, None),
318+
],
319+
)
320+
def test_full_name_column(record: Record, expected: str | None) -> None:
321+
assert full_name_column(record) == expected

tests/workspace/test_ws_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_import_data_rdi(force_migrated_records, app, program):
7171

7272
res.forms["import-file"]["_selected_tab"] = "rdi"
7373
res.forms["import-file"]["rdi-file"] = Upload("rdi_one.xlsx", data)
74-
res.forms["import-file"]["rdi-detail_column_label"] = "full_name_i_c"
74+
res.forms["import-file"]["rdi-detail_column_label"] = "household_id"
7575
res = res.forms["import-file"].submit()
7676
assert res.status_code == 302
7777
assert program.households.count() == 1

0 commit comments

Comments
 (0)