Skip to content

Commit

Permalink
Merge pull request #474 from rcpch/mbarton/save-cleaned-fields-for-in…
Browse files Browse the repository at this point in the history
…valid-form-during-csv-upload

Save cleaned fields during CSV upload even if other fields are invalid
  • Loading branch information
mbarton authored Jan 11, 2025
2 parents 9f338a5 + e6dd849 commit 1a9ecda
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
13 changes: 9 additions & 4 deletions project/npda/general_functions/csv/csv_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,15 @@ async def validate_rows(rows, async_client):
def create_instance(model, form):
# We want to retain fields even if they're invalid so that we can return them to the user
# Use the field value from cleaned_data, falling back to data if it's not there
if form.is_valid():
data = form.cleaned_data
else:
data = form.data
data = {}

for key, value in form.cleaned_data.items():
data[key] = value

for key, value in form.data.items():
if key not in data:
data[key] = value

instance = model(**data)
instance.is_valid = form.is_valid()
instance.errors = (
Expand Down
35 changes: 35 additions & 0 deletions project/npda/tests/test_csv_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,3 +832,38 @@ def test_height_is_rounded_to_one_decimal(test_user, single_row_valid_df):
assert visit.weight == round(
Decimal("7.89"), 1
) # Values are stored as Decimals (4 digits with 1 decimal place)


@pytest.mark.django_db
@patch(
"project.npda.general_functions.csv.csv_upload.validate_patient_async",
mock_patient_external_validation_result(
postcode=ValidationError("Invalid postcode")
),
)
def test_cleaned_fields_are_stored_when_other_fields_are_invalid(test_user, single_row_valid_df):
# PATIENT
# - Valid, cleaning should remove the spaces
single_row_valid_df["NHS Number"] = "719 573 0220"

# Postcode marked as invalid by the mock patched above
single_row_valid_df["Postcode of usual address"] = "not a real postcode"

# VISIT
# - Valid, cleaning should retain only one decimal place
single_row_valid_df["Patient Weight (kg)"] = 7.89

# - Invalid - cannot be less than 40
single_row_valid_df["Patient Height (cm)"] = 38

csv_upload_sync(test_user, single_row_valid_df, None, ALDER_HEY_PZ_CODE)

patient = Patient.objects.first()
visit = Visit.objects.first()

assert(patient.nhs_number == "7195730220") # cleaned version saved
assert(patient.postcode == "not a real postcode") # saved but invalid

assert(visit.weight == round(Decimal("7.89"), 1)) # cleaned version saved
assert(visit.height == 38) # saved but invalid

0 comments on commit 1a9ecda

Please sign in to comment.