From cc017a86958c048f6d9b024a86d7b00255824fc7 Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Mon, 11 Mar 2019 23:59:07 -0700 Subject: [PATCH] First round of responses to PR feedback --- todo/management/commands/import_csv.py | 15 ++++++++------- todo/operations/csv_importer.py | 9 +++------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/todo/management/commands/import_csv.py b/todo/management/commands/import_csv.py index 304ccb2..8b399a1 100644 --- a/todo/management/commands/import_csv.py +++ b/todo/management/commands/import_csv.py @@ -10,7 +10,7 @@ from todo.operations.csv_importer import CSVImporter class Command(BaseCommand): help = """Import specifically formatted CSV file containing incoming tasks to be loaded. For specfic format of inbound CSV, see data/import_example.csv. - For documentation on field formats and required fields, see README.md. + For documentation on upsert logic and required fields, see README.md. """ def add_arguments(self, parser: CommandParser) -> None: @@ -22,13 +22,14 @@ class Command(BaseCommand): def handle(self, *args: Any, **options: Any) -> None: # Need a file to proceed if not options.get("file"): - print("Sorry, we need a file name to work from.") + print("Sorry, we need a filename to work from.") + sys.exit(1) + + filepath = str(options.get("file")) + + if not Path(filepath).exists(): + print(f"Sorry, couldn't find file: {filepath}") sys.exit(1) - else: - filepath = str(options.get("file")) - if not Path(filepath).exists(): - print(f"Sorry, couldn't find file: {filepath}") - sys.exit(1) with open(filepath, mode="r", encoding="utf-8-sig") as fileobj: # Pass in a file *object*, not a path diff --git a/todo/operations/csv_importer.py b/todo/operations/csv_importer.py index db08b7f..1a6d9c0 100644 --- a/todo/operations/csv_importer.py +++ b/todo/operations/csv_importer.py @@ -44,7 +44,7 @@ class CSVImporter: # DI check: Do we have expected header row? header = csv_reader.fieldnames expected = ['Title', 'Group', 'Task List', 'Created By', 'Created Date', 'Due Date', 'Completed', 'Assigned To', 'Note', 'Priority'] - if not header == expected: + if header != expected: self.results.get('summaries').append(f"Inbound data does not have expected columns.\nShould be: {expected}") return self.results @@ -93,11 +93,8 @@ class CSVImporter: msg = f"Missing required task creator." row_errors.append(msg) - created_by = get_user_model().objects.filter(username=row.get("Created By")) - if created_by.exists(): - creator = created_by.first() - else: - creator = None + creator = get_user_model().objects.filter(username=row.get("Created By")).first() + if not creator: msg = f"Invalid task creator {row.get('Created By')}" row_errors.append(msg)