diff --git a/backend/leads/csv_import.py b/backend/leads/csv_import.py index 24c70821..cfe61135 100644 --- a/backend/leads/csv_import.py +++ b/backend/leads/csv_import.py @@ -201,8 +201,14 @@ def parse_and_validate(file_bytes: bytes, request, legacy=False) -> ImportResult for idx, raw_row in enumerate(data_rows, start=1): if not any((cell or "").strip() for cell in raw_row): continue - if any((cell or "").strip() for cell in raw_row[len(headers) :]): - errors.append(RowError(idx, "", "Row has more cells than the header")) + if not legacy and any((cell or "").strip() for cell in raw_row[len(headers) :]): + errors.append( + RowError( + idx, + "", + "Row has more cells than the header; quote values that contain commas", + ) + ) continue parsed.append( ( @@ -231,6 +237,7 @@ def parse_and_validate(file_bytes: bytes, request, legacy=False) -> ImportResult if email: seen_emails[email.lower()] = idx + errors.sort(key=lambda e: e.row) return ImportResult(valid=valid, errors=errors) diff --git a/backend/leads/tests/test_lead_import.py b/backend/leads/tests/test_lead_import.py index 139a7d82..dfb618d7 100644 --- a/backend/leads/tests/test_lead_import.py +++ b/backend/leads/tests/test_lead_import.py @@ -116,6 +116,21 @@ class TestPreview: assert [e["row"] for e in body["errors"]] == [1] assert "more cells than the header" in body["errors"][0]["message"] + def test_blank_cells_past_the_header_are_accepted(self, admin_client): + upload = _raw(b"first_name,last_name\nAnn,Lee,, \n") + body = _post(admin_client, PREVIEW, upload).json() + assert body["summary"] == {"total": 1, "valid": 1, "invalid": 0} + + def test_row_errors_stay_in_row_order(self, admin_client): + upload = _raw( + b"first_name,last_name,email\n" + b"Ann,Lee,not-an-email\n" + b"Bob,Ray,b@example.com,extra\n" + b"Cy,Fox,also-bad\n" + ) + body = _post(admin_client, PREVIEW, upload).json() + assert [e["row"] for e in body["errors"]] == [1, 2, 3] + def test_a_converted_lead_cannot_be_imported(self, admin_client): upload = _csv( BASE + ["status"], [["Ada", "Byte", "a@example.com", "Converted"]] @@ -400,6 +415,12 @@ class TestDeprecatedUploadAlias: assert body["errors"][0]["row"] == 2 assert not Lead.objects.exists() + def test_a_row_wider_than_the_header_still_imports(self, admin_client): + upload = _raw(b"first_name,last_name\nAnn,Lee,note\n") + response = self._post(admin_client, upload) + assert response.status_code == 200, response.json() + assert Lead.objects.filter(first_name="Ann").exists() + def test_a_bad_file_is_a_400(self, admin_client): response = self._post(admin_client, _raw(b"")) assert response.status_code == 400