2025-02-04 00:19:05 +01:00
|
|
|
import datetime
|
2025-02-12 13:53:17 +01:00
|
|
|
from datetime import timedelta
|
2025-02-04 00:19:05 +01:00
|
|
|
|
2025-01-26 00:53:17 +01:00
|
|
|
from dispatcher.models import Task, Project
|
|
|
|
|
|
|
|
|
2025-02-04 00:19:05 +01:00
|
|
|
def convert_timedelta(td: datetime.timedelta):
|
|
|
|
total_seconds = td.total_seconds()
|
|
|
|
hours, remainder = divmod(total_seconds, 3600)
|
|
|
|
minutes, seconds = divmod(remainder, 60)
|
|
|
|
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
|
|
|
|
|
2025-02-12 13:53:17 +01:00
|
|
|
def str_to_timedelta(duration: str):
|
|
|
|
parts = duration.split(":")
|
|
|
|
hours = int(parts[0])
|
|
|
|
minutes = int(parts[1])
|
|
|
|
return timedelta(hours=hours, minutes=minutes)
|
|
|
|
|
2025-02-04 00:19:05 +01:00
|
|
|
|
2025-01-26 00:53:17 +01:00
|
|
|
def import_tasks(csv_content: str):
|
|
|
|
tasks = []
|
|
|
|
for line in csv_content.splitlines()[1:]:
|
|
|
|
if len(line.strip()) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
date, duration, project_name, name = line.split(";")
|
|
|
|
if duration == "-":
|
|
|
|
duration = 0
|
|
|
|
else:
|
|
|
|
hours, mins = duration.split(":")
|
|
|
|
duration = int(hours) * 60 + int(mins)
|
|
|
|
|
|
|
|
project, created = Project.objects.get_or_create(name=project_name)
|
|
|
|
if created:
|
|
|
|
print(f"Created new project {project}")
|
|
|
|
|
|
|
|
|
|
|
|
tasks.append(Task(
|
|
|
|
date=date,
|
|
|
|
duration=duration,
|
|
|
|
project=project,
|
|
|
|
name=name
|
|
|
|
))
|
|
|
|
|
|
|
|
Task.objects.bulk_create(
|
|
|
|
tasks,
|
|
|
|
update_conflicts=True,
|
|
|
|
unique_fields=["date", "project", "name"],
|
|
|
|
update_fields=["duration"]
|
|
|
|
)
|
|
|
|
print(f"Imported {len(tasks)} tasks")
|
|
|
|
|