PR Review Lab · Import Contacts From a CSV File

20:00left of 20:00

Import Contacts From a CSV File

Adds an Import button to the Contacts page. Upload a CSV with name, email, and phone columns, and the importer creates a contact for each row. Rows with an email you already have are skipped, so importing the same file twice doesn't duplicate anyone. Tested with a sample export.

Acontacts/importer.py1 of 3

Files Changed

contacts/

contacts/tests/

contacts/importer.py

Python · Click a line number to comment · Tap a line to comment
@@ -0,0 +1,19 @@
Added: import csv
Added: import io
Added:
Added: from contacts.models import Contact
Added:
Added:
Added: def import_contacts(upload, owner):
Added: rows = list(csv.reader(io.TextIOWrapper(upload)))
Added: created = 0
Added: for i in range(1, len(rows) - 1):
Added: try:
Added: name, email, phone = rows[i]
Added: if Contact.objects.filter(owner=owner, email=email).exists():
Added: continue
Added: Contact.objects.create(owner=owner, name=name, email=email, phone=phone)
Added: created += 1
Added: except:
Added: pass
Added: return created