Skip to content

Commit

Permalink
Merge pull request #147 from modoboa/fix/import_backend_detection
Browse files Browse the repository at this point in the history
Added option to force import backend
  • Loading branch information
tonioo committed Dec 18, 2023
2 parents db81802 + 3cebabb commit 60e942f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
11 changes: 8 additions & 3 deletions modoboa_contacts/importer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
]


def detect_import_backend(fp, delimiter: str = ";"):
def get_import_backend(fp, delimiter: str = ";", name: str = "auto"):
reader = csv.DictReader(
fp,
delimiter=delimiter,
Expand All @@ -18,16 +18,21 @@ def detect_import_backend(fp, delimiter: str = ";"):
rows = reader

for backend in BACKENDS:
if backend.detect_from_columns(columns):
if name == "auto":
if backend.detect_from_columns(columns):
return backend, rows
elif name == backend.name:
return backend, rows

raise RuntimeError("Failed to detect backend to use")


def import_csv_file(addressbook,
backend_name: str,
csv_filename: str,
delimiter: str,
carddav_password: str = None):
with open(csv_filename) as fp:
backend, rows = detect_import_backend(fp, delimiter)
backend, rows = get_import_backend(
fp, delimiter, backend_name)
backend(addressbook).proceed(rows, carddav_password)
2 changes: 1 addition & 1 deletion modoboa_contacts/importer/backends/outlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class OutlookBackend(ImporterBackend):

@classmethod
def detect_from_columns(cls, columns):
return columns == OUTLOOK_COLUMNS
return set(OUTLOOK_COLUMNS).issubset(columns)

def get_first_name(self, values: dict) -> str:
result = values["First Name"]
Expand Down
8 changes: 8 additions & 0 deletions modoboa_contacts/management/commands/import_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def add_arguments(self, parser):
"contacts will be synced to CardDAV servert too"
)
)
parser.add_argument(
"--backend", type=str, default="auto",
help=(
"Specify import backend to use. Defaults to 'auto', "
"meaning the script will try to guess which one to use"
)
)
parser.add_argument(
"email", type=str,
help="Email address to import contacts for"
Expand All @@ -44,6 +51,7 @@ def handle(self, *args, **options):
try:
import_csv_file(
addressbook,
options["backend"],
options["file"],
options["delimiter"],
options.get("carddav_password")
Expand Down
20 changes: 19 additions & 1 deletion modoboa_contacts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_import_unknown_backend(self):
self.assertEqual(str(ctx.exception),
"Failed to detect backend to use")

def test_import_from_outlook(self):
def test_import_from_outlook_auto(self):
management.call_command(
"import_contacts", "[email protected]", self.path)
address = models.EmailAddress.objects.get(
Expand All @@ -389,6 +389,24 @@ def test_import_from_outlook(self):
"Street 1 Street 2"
)

def test_import_from_outlook(self):
management.call_command(
"import_contacts",
"[email protected]",
self.path,
backend="outlook",
)
address = models.EmailAddress.objects.get(
address="[email protected]")
phone = models.PhoneNumber.objects.get(
number="12345678")
self.assertEqual(address.contact.first_name, "Toto Tata")
self.assertEqual(address.contact.addressbook.user.email, "[email protected]")
self.assertEqual(
address.contact.address,
"Street 1 Street 2"
)

def test_import_and_carddav_sync(self):
with httmock.HTTMock(mocks.options_mock, mocks.put_mock):
management.call_command(
Expand Down

0 comments on commit 60e942f

Please sign in to comment.