diff --git a/program/management/commands/addtimeslots.py b/program/management/commands/addtimeslots.py index 091e3d72464f434783d89b840bfec02fd8f89bc0..5fda80081a4ddd09877d78a0b737dab525b1c7d9 100644 --- a/program/management/commands/addtimeslots.py +++ b/program/management/commands/addtimeslots.py @@ -3,6 +3,7 @@ from datetime import datetime from dateutil.rrule import rrule from django.core.management import BaseCommand +from django.db import IntegrityError from django.utils.timezone import make_aware from program.models import RRule, Schedule, TimeSlot @@ -34,6 +35,8 @@ class Command(BaseCommand): for schedule in ( Schedule.objects.exclude(rrule=once).filter(last_date=None).order_by("show__name") ): + added = 0 + skipped = 0 last_timeslot = schedule.timeslots.last() # add timeslots to the schedules with no timeslot this year @@ -65,8 +68,16 @@ class Command(BaseCommand): ) ) else: - TimeSlot.objects.bulk_create(timeslots, ignore_conflicts=True) + for timeslot in timeslots: + try: + timeslot.save() + except IntegrityError: + skipped += 1 + else: + added += 1 self.stdout.write( - self.style.SUCCESS(f"added {str(len(timeslots))} timeslots to {schedule}") + self.style.SUCCESS( + f"added {str(added)} timeslots to {schedule}, skipped {str(skipped)}" + ) )