import datetime from datetime import timedelta from dispatcher.models import Task, Project 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}" def str_to_timedelta(duration: str): parts = duration.split(":") hours = int(parts[0]) minutes = int(parts[1]) return timedelta(hours=hours, minutes=minutes) 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")