from dispatcher.models import Task, Project 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")