diff --git a/src/tt/import_csv.py b/src/tt/import_csv.py new file mode 100644 index 0000000..93845e9 --- /dev/null +++ b/src/tt/import_csv.py @@ -0,0 +1,52 @@ +import sys +import csv +from datetime import datetime +from tt.db import initialize_db, Task, Category, TaskStatus + + +def import_tasks_from_csv(filename: str): + initialize_db() + + with open(filename, "r") as f: + reader = csv.DictReader(f) + + for row in reader: + # Parse the due date if it exists and isn't empty + due_date = None + if row["due"] and row["due"].strip(): + try: + due_date = datetime.strptime(row["due"].strip(), "%Y-%m-%d") + except ValueError: + print( + f"Warning: Couldn't parse date '{row['due']}', skipping due date" + ) + + # Validate status + status = row["status"].lower() if row["status"] else "zero" + try: + TaskStatus(status) + except ValueError: + print(f"Warning: Invalid status '{status}', defaulting to 'zero'") + status = "zero" + + category_id = None + if row["category"].strip(): + category_name = row["category"].strip() + category, _ = Category.get_or_create( + name=category_name, defaults={"description": None} + ) + category_id = category.id + + Task.create( + text=row["task"], + type=row["type"], + status=status, + due=due_date, + category_id=category_id, + ) + print(f"Imported task: {row['task']}") + + +if __name__ == "__main__": + import_tasks_from_csv(sys.argv[1]) + print("Import complete!")