Move core of CSV importer to operations

This commit is contained in:
Scot Hacker 2019-03-09 00:10:53 -08:00
parent 60cd26d227
commit 5639f4e098
5 changed files with 50 additions and 28 deletions

View file

@ -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
1 Title Group Task List Created Date Due Date Completed Created By Assigned To Note Priority
2 Make dinner Scuba Divers Example Llist 2012-03-14 No shacker user1 This is as good as it gets 3
3 Bake bread Scuba Divers Example Llist 2012-03-14 2012-03-14 shacker
4 Eat food Scuba Divers Example Llist 2015-06-24 Yes user2 Every generation throws a hero up the pop charts 77

View file

@ -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
1 Title Group Task List Created Date Due Date Completed Created By Assigned To Note Priority
2 Make dinner Scuba Divers Example List 2012-03-14 No shacker user1 This is as good as it gets 3
3 Bake bread Scuba Divers Example List 2012-03-14 2012-03-14 shacker
4 Eat food Scuba Divers Example List 2015-06-24 Yes user2 Every generation throws a hero up the pop charts 77
5 Be glad Scuba Divers Example List 2019-03-07 user2 1

View file

@ -1,20 +1,14 @@
import csv
import unicodecsv
import sys import sys
from pathlib import Path
from typing import Any from typing import Any
from django.core.management.base import BaseCommand, CommandParser from django.core.management.base import BaseCommand, CommandParser
from django.contrib.auth.models import Group from todo.operations.csv_importer import CSVImporter
from django.contrib.auth import get_user_model
from todo.models import Task, TaskList
class Command(BaseCommand): class Command(BaseCommand):
help = """Import specifically formatted CSV file of incoming tasks. help = """Import specifically formatted CSV file containing incoming tasks to be loaded.
For specfic format of inbound CSV, see data/import example.csv. 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 field formats and required fields, see README.md.
""" """
@ -25,24 +19,12 @@ class Command(BaseCommand):
) )
def handle(self, *args: Any, **options: Any) -> None: def handle(self, *args: Any, **options: Any) -> None:
# ### Sanity checks ###
# Need a file to proceed # Need a file to proceed
if not options.get("file"): if not options.get("file"):
print("Sorry, we need a file name to work from.") print("Sorry, we need a file name to work from.")
sys.exit(1) sys.exit(1)
else: else:
print(options.get("file")) # Don't check validity of filepath here; upserter will do that.
if not Path(options.get("file")).exists(): filepath = str(options.get("file"))
print(f"Sorry, couldn't find file name specified: {options.get('file')}")
sys.exit(1)
print("Have arg and good file path") CSVImporter.upsert(filepath)
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)

View file

View file

@ -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