From 5639f4e09873fca8c339443b4210a40d14c027e6 Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Sat, 9 Mar 2019 00:10:53 -0800 Subject: [PATCH] Move core of CSV importer to operations --- todo/data/import example.csv | 4 --- todo/data/import_example.csv | 5 ++++ todo/management/commands/import_csv.py | 30 ++++---------------- todo/operations/__init__.py | 0 todo/operations/csv_importer.py | 39 ++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 28 deletions(-) delete mode 100644 todo/data/import example.csv create mode 100644 todo/data/import_example.csv create mode 100644 todo/operations/__init__.py create mode 100644 todo/operations/csv_importer.py diff --git a/todo/data/import example.csv b/todo/data/import example.csv deleted file mode 100644 index 73f8c99..0000000 --- a/todo/data/import example.csv +++ /dev/null @@ -1,4 +0,0 @@ -Title,Group,Task List,Created Date,Due Date,Completed,Created By,Assigned To,Note,Priority -Make dinner,Scuba Divers,Example Llist,2012-03-14,,No,shacker,user1,This is as good as it gets,3 -Bake bread,Scuba Divers,Example Llist,2012-03-14,2012-03-14,,shacker,,, -Eat food,Scuba Divers,Example Llist,,2015-06-24,Yes,,user2,Every generation throws a hero up the pop charts,77 \ No newline at end of file diff --git a/todo/data/import_example.csv b/todo/data/import_example.csv new file mode 100644 index 0000000..8762bb9 --- /dev/null +++ b/todo/data/import_example.csv @@ -0,0 +1,5 @@ +Title,Group,Task List,Created Date,Due Date,Completed,Created By,Assigned To,Note,Priority +Make dinner,Scuba Divers,Example List,2012-03-14,,No,shacker,user1,This is as good as it gets,3 +Bake bread,Scuba Divers,Example List,2012-03-14,2012-03-14,,shacker,,, +Eat food,Scuba Divers,Example List,,2015-06-24,Yes,,user2,Every generation throws a hero up the pop charts,77 +Be glad,Scuba Divers,Example List,2019-03-07,,,,user2,,1 \ No newline at end of file diff --git a/todo/management/commands/import_csv.py b/todo/management/commands/import_csv.py index 3b879ab..b27f2fc 100644 --- a/todo/management/commands/import_csv.py +++ b/todo/management/commands/import_csv.py @@ -1,20 +1,14 @@ -import csv -import unicodecsv import sys -from pathlib import Path from typing import Any from django.core.management.base import BaseCommand, CommandParser -from django.contrib.auth.models import Group -from django.contrib.auth import get_user_model - -from todo.models import Task, TaskList +from todo.operations.csv_importer import CSVImporter class Command(BaseCommand): - help = """Import specifically formatted CSV file of incoming tasks. - For specfic format of inbound CSV, see data/import example.csv. + 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. """ @@ -25,24 +19,12 @@ class Command(BaseCommand): ) def handle(self, *args: Any, **options: Any) -> None: - # ### Sanity checks ### - # Need a file to proceed if not options.get("file"): print("Sorry, we need a file name to work from.") sys.exit(1) else: - print(options.get("file")) - if not Path(options.get("file")).exists(): - print(f"Sorry, couldn't find file name specified: {options.get('file')}") - sys.exit(1) + # Don't check validity of filepath here; upserter will do that. + filepath = str(options.get("file")) - print("Have arg and good file path") - with open(Path(options.get("file")), 'rb') as csvfile: - # csvreader = csv.reader(csvfile, delimiter=' ', quotechar='|') - # csvreader = csv.DictReader(csvfile) - csvreader = unicodecsv.reader(csvfile, encoding='utf-8-sig') - for row in csvreader: - # print(', '.join(row)) - # print(row['Title'], row['Group']) - print(row) \ No newline at end of file + CSVImporter.upsert(filepath) diff --git a/todo/operations/__init__.py b/todo/operations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/operations/csv_importer.py b/todo/operations/csv_importer.py new file mode 100644 index 0000000..6112a0f --- /dev/null +++ b/todo/operations/csv_importer.py @@ -0,0 +1,39 @@ +import csv +import sys +from pathlib import Path + +from django.contrib.auth import get_user_model +from icecream import ic + +from todo.models import Task, TaskList + + +class CSVImporter: + """Core upsert functionality for CSV import, for re-use by `import_csv` management command, web UI and tests.""" + + def __init__(self): + pass + + def upsert(filepath): + + if not Path(filepath).exists(): + print(f"Sorry, couldn't find file: {filepath}") + sys.exit(1) + + # Have arg and good file path, read rows + with open(filepath, mode="r") as csv_file: + csv_reader = csv.DictReader(csv_file) + line_count = 0 + for row in csv_reader: + # Title, Group, Task List, Created Date, Due Date, Completed, Created By, Assigned To, Note, Priority + newrow = row # Copy so we can modify properties + newrow["Completed"] = True if row.get("Completed") == "Yes" else False + ic(newrow) + + if line_count == 0: + print(f'Column names are {", ".join(row)}') + line_count += 1 + print( + f"Row {line_count}: Title: {newrow['Title']}, Group: {newrow['Group']}, Completed: {newrow['Completed']}" + ) + line_count += 1