importer
This commit is contained in:
parent
ce80e004cb
commit
85fa719b96
52
src/tt/import_csv.py
Normal file
52
src/tt/import_csv.py
Normal file
@ -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!")
|
Loading…
Reference in New Issue
Block a user