Skip to content

fix: trim whitespace from custom csv headers#630

Open
priyan923 wants to merge 2 commits into
Kuldeeep18:mainfrom
priyan923:fix/558-trim-csv-headers
Open

fix: trim whitespace from custom csv headers#630
priyan923 wants to merge 2 commits into
Kuldeeep18:mainfrom
priyan923:fix/558-trim-csv-headers

Conversation

@priyan923

@priyan923 priyan923 commented Jul 8, 2026

Copy link
Copy Markdown

Pull Request

🔗 Related Issue

Closes #558


📝 Summary of Changes

This PR resolves Issue #558 by sanitizing CSV headers during the lead import process. It prevents automapping failures and validation errors caused by hidden leading or trailing whitespace in user-uploaded CSV column names.


🏷️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • ♻️ Refactor
  • 📝 Documentation update
  • 🎨 UI / Style change
  • 🔧 Chore

🧪 Testing

The fix ensures the csv.DictReader processes headers as clean strings before they are mapped to the database schema.

Steps to test:

1.Create a local CSV file with intentionally messy headers (e.g., " Email ", " First Name ").

2.Run the import_leads_from_csv task.

3.Verify that the system successfully maps these columns to the email and first_name fields without triggering validation errors.

4.Run python manage.py test to ensure no regressions in existing logic.

📸 Screenshots

Screenshot (1251)

✅ Checklist

  • No merge conflicts
  • Changes follow the project guidelines
  • Documentation updated (if applicable)
  • Related issue linked
  • Changes tested locally (if applicable)

Summary by CodeRabbit

  • Bug Fixes
    • Improved CSV lead imports to trim extra whitespace from header names, preventing mismatches during field mapping.
    • Lead details from CSV uploads (including names and company) are now more reliably imported when column headers are inconsistently padded.
  • Tests
    • Added coverage to verify whitespace-trimmed CSV header import behavior.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The lead CSV import now trims whitespace from CSV header fieldnames before mapping rows, and a test verifies imports still populate lead fields when headers contain surrounding spaces.

Changes

CSV header trimming

Layer / File(s) Summary
Normalize CSV header fieldnames
backend/leads/tasks.py
Strips whitespace from reader.fieldnames when present, right after creating the csv.DictReader, so downstream field mapping uses trimmed header names.
Test trimmed header import
backend/leads/tests.py
Adds a CSV import test with spaced header names and verifies first_name, last_name, and company are mapped correctly.

Estimated code review effort: 1 (Trivial) | ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title clearly states the main change: trimming whitespace from CSV headers during import.
Linked Issues check ✅ Passed The CSV import now strips header whitespace before mapping, and the new test verifies the behavior for issue #558.
Out of Scope Changes check ✅ Passed Changes stay focused on CSV header trimming and its test; no unrelated code paths were added.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
backend/leads/tasks.py (1)

86-87: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a test with whitespace-padded CSV headers.

The existing tests cover BOM and semicolon delimiters but don't directly exercise the whitespace-trimming fix. A test with headers like " Email , First Name " would validate this specific change.

🧪 Suggested test
def test_import_trims_whitespace_from_csv_headers(self):
    csv_data = (
        " Email , First Name , Last Name , Company Name \n"
        "alice@example.com,Alice,Smith,Acme\n"
    )

    import_leads_from_csv(csv_data, str(self.organization.id))

    lead = Lead.objects.get(organization=self.organization, email='alice@example.com')
    self.assertEqual(lead.first_name, 'Alice')
    self.assertEqual(lead.last_name, 'Smith')
    self.assertEqual(lead.company, 'Acme')
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/leads/tasks.py` around lines 86 - 87, Add a focused test for the
whitespace-trimming behavior in the CSV import path. In the test suite that
covers import_leads_from_csv and Lead creation, add a case with headers padded
like " Email , First Name , Last Name , Company Name " and verify the imported
Lead is populated correctly after reader.fieldnames trimming. Use the existing
import_leads_from_csv helper and assert the stored email, first_name, last_name,
and company values to directly exercise the change in backend/leads/tasks.py.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@backend/leads/tasks.py`:
- Around line 86-87: Add a focused test for the whitespace-trimming behavior in
the CSV import path. In the test suite that covers import_leads_from_csv and
Lead creation, add a case with headers padded like " Email , First Name , Last
Name , Company Name " and verify the imported Lead is populated correctly after
reader.fieldnames trimming. Use the existing import_leads_from_csv helper and
assert the stored email, first_name, last_name, and company values to directly
exercise the change in backend/leads/tasks.py.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1ddb5e3f-4468-4d05-9d82-a9ee653e968f

📥 Commits

Reviewing files that changed from the base of the PR and between 4a33158 and fcdda18.

📒 Files selected for processing (1)
  • backend/leads/tasks.py

@priyan923

priyan923 commented Jul 8, 2026

Copy link
Copy Markdown
Author

@Kuldeeep18 Please Merge my PR as all checks have been passed

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
backend/leads/tests.py (2)

92-104: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Test logic is correct and covers the PR objective well.

The trimmed headers (Email, First Name, Last Name, Company Name) correctly flow through _get_field aliases to populate first_name, last_name, and company. The email is implicitly verified via the Lead.objects.get query.

One nit: Line 93's comment # Everything here must have the same indentation reads like a leftover developer note rather than a useful code comment. Consider removing it.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/leads/tests.py` around lines 92 - 104, Remove the leftover developer
note comment in the `test_import_trims_whitespace_from_csv_headers` test and
keep the test focused on the CSV import behavior; the assertions already cover
the `import_leads_from_csv` flow and `Lead.objects.get`, so delete the
unnecessary `# Everything here must have the same indentation` line without
changing the test logic.

478-479: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Trailing whitespace at end of file.

Lines 478-479 contain blank lines with trailing whitespace. Consider cleaning up.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/leads/tests.py` around lines 478 - 479, The file has trailing
whitespace on the blank lines at the end, so clean up the end-of-file formatting
by removing the extra spaces and leaving only the needed final newline. Check
the closing portion of the tests module and ensure there are no whitespace-only
lines with trailing characters.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@backend/leads/tests.py`:
- Around line 92-104: Remove the leftover developer note comment in the
`test_import_trims_whitespace_from_csv_headers` test and keep the test focused
on the CSV import behavior; the assertions already cover the
`import_leads_from_csv` flow and `Lead.objects.get`, so delete the unnecessary
`# Everything here must have the same indentation` line without changing the
test logic.
- Around line 478-479: The file has trailing whitespace on the blank lines at
the end, so clean up the end-of-file formatting by removing the extra spaces and
leaving only the needed final newline. Check the closing portion of the tests
module and ensure there are no whitespace-only lines with trailing characters.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0ed4b08f-55fe-4712-a6e4-73fca1d63841

📥 Commits

Reviewing files that changed from the base of the PR and between fcdda18 and b0019ba.

📒 Files selected for processing (1)
  • backend/leads/tests.py

@priyan923 priyan923 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kuldeeep18
I have added the requested unit test to leads/tests.py.
Note: The test currently contains one unrelated AttributeError in the campaign test suite related to Django template rendering ; I want to clarify that this is not caused by my changes and exists in the base branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LO-121 [Easy]: Trim Whitespace from Custom CSV Headers during Import

1 participant