From 60cd26d227b27be336f92b3a72a2d91bbc80bd61 Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Sun, 3 Mar 2019 18:26:18 -0800 Subject: [PATCH] Bare start on CSV support --- todo/data/import example.csv | 4 +++ todo/management/commands/import_csv.py | 48 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 todo/data/import example.csv create mode 100644 todo/management/commands/import_csv.py diff --git a/todo/data/import example.csv b/todo/data/import example.csv new file mode 100644 index 0000000..73f8c99 --- /dev/null +++ b/todo/data/import example.csv @@ -0,0 +1,4 @@ +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/management/commands/import_csv.py b/todo/management/commands/import_csv.py new file mode 100644 index 0000000..3b879ab --- /dev/null +++ b/todo/management/commands/import_csv.py @@ -0,0 +1,48 @@ +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 + + +class Command(BaseCommand): + help = """Import specifically formatted CSV file of incoming tasks. + For specfic format of inbound CSV, see data/import example.csv. + For documentation on field formats and required fields, see README.md. + """ + + def add_arguments(self, parser: CommandParser) -> None: + + parser.add_argument( + "-f", "--file", dest="file", default=None, help="File to to inbound CSV file." + ) + + 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) + + 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