Skip to content

Commit 94c4090

Browse files
Use form fields correctly
1 parent 700856d commit 94c4090

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
HOUSEHOLD_2_PK = 2
3434
HOUSEHOLD_1_NAME = "Household 1"
3535
HOUSEHOLD_2_NAME = "Household 2"
36+
FULL_NAME_COLUMN = "full_name"
3637

3738

3839
@pytest.fixture
@@ -50,21 +51,21 @@ def config() -> Config:
5051
@pytest.fixture
5152
def household_sheet(config: Config) -> Sheet:
5253
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},
54+
{config["detail_column_label"]: HOUSEHOLD_1_NAME, config["household_pk_col"]: HOUSEHOLD_1_PK},
55+
{config["detail_column_label"]: HOUSEHOLD_1_NAME, config["household_pk_col"]: HOUSEHOLD_2_PK},
5556
]
5657

5758

5859
@pytest.fixture
5960
def individual_sheet(config: Config) -> Sheet:
6061
return [
6162
{
62-
config["detail_column_label"]: "John Doe",
63-
config["household_pk_col"]: HOUSEHOLD_1_PK,
63+
FULL_NAME_COLUMN: "John Doe",
64+
config["master_column_label"]: HOUSEHOLD_1_PK,
6465
},
6566
{
66-
config["detail_column_label"]: "Doe John",
67-
config["household_pk_col"]: HOUSEHOLD_2_PK,
67+
FULL_NAME_COLUMN: "Doe John",
68+
config["master_column_label"]: HOUSEHOLD_2_PK,
6869
},
6970
]
7071

@@ -135,7 +136,7 @@ def test_process_households(mocker: MockerFixture, config: Config, household_she
135136
}
136137
job.program.households.create.assert_has_calls(
137138
[
138-
call(batch=batch, name=row[config["master_column_label"]], flex_fields=clean_field_names_mock.return_value)
139+
call(batch=batch, name=row[config["detail_column_label"]], flex_fields=clean_field_names_mock.return_value)
139140
for row in household_sheet
140141
]
141142
)
@@ -166,8 +167,8 @@ def test_process_individuals(
166167
[
167168
call(
168169
batch=batch_mock,
169-
name=row[config["detail_column_label"]],
170-
household_id=household_mapping[row[config["household_pk_col"]]].pk,
170+
name=row[FULL_NAME_COLUMN],
171+
household_id=household_mapping[row[config["master_column_label"]]].pk,
171172
flex_fields=clean_field_names_mock.return_value,
172173
)
173174
for row in individual_sheet

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)