Skip to content

Commit b26ce03

Browse files
committed
in prog
1 parent 47ee309 commit b26ce03

16 files changed

Lines changed: 1689 additions & 520 deletions

backend/ohq/migrations/0022_booking.py

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Generated by Django 5.0.3 on 2025-01-26 19:14
1+
# Generated by Django 5.0.3 on 2025-04-11 22:07
22

33
from django.db import migrations
44

55

66
class Migration(migrations.Migration):
77

88
dependencies = [
9+
("ohq", "0021_occurrence"),
910
("ohq", "0021_queue_question_timer_enabled_and_more"),
10-
("ohq", "0025_alter_booking_unique_together"),
1111
]
1212

1313
operations = []

backend/ohq/migrations/0023_alter_booking_end_alter_booking_start_and_more.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

backend/ohq/migrations/0024_alter_booking_options_and_more.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

backend/ohq/migrations/0025_alter_booking_unique_together.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

backend/ohq/models.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ class Occurrence(models.Model):
462462
original_end = models.DateTimeField(("original end"))
463463
created_on = models.DateTimeField(("created on"), auto_now_add=True)
464464
updated_on = models.DateTimeField(("updated on"), auto_now=True)
465-
interval = models.IntegerField(("interval"), blank=True, validators=[MinValueValidator(5), MaxValueValidator(60)])
465+
interval = models.IntegerField(("interval"), blank=True, null=True, validators=[MinValueValidator(5), MaxValueValidator(60)])
466466

467467
class Meta:
468468
verbose_name = ("occurrence")
@@ -480,23 +480,43 @@ def __init__(self, *args, **kwargs):
480480
self.location = event.location
481481

482482
def save(self, *args, **kwargs):
483+
# Get the old instance if this is an update
484+
old_instance = None
485+
if self.pk:
486+
old_instance = Occurrence.objects.get(pk=self.pk)
487+
483488
super().save(*args, **kwargs)
484489

485-
if self.pk: # If save is called on object update, not creation
486-
self.bookings.all().delete()
487-
488-
delta = self.end - self.start
489-
delta_minutes = delta.total_seconds() / 60
490-
booking_count = int(delta_minutes // self.interval)
491-
for i in range(booking_count):
492-
booking_start = self.start + timedelta(minutes=i * self.interval)
493-
booking_end = booking_start + timedelta(minutes=self.interval)
494-
Booking.objects.create(
495-
occurrence=self,
496-
user=None,
497-
start=booking_start,
498-
end = booking_end,
499-
)
490+
# Only update bookings if:
491+
# 1. This is a new occurrence
492+
# 2. The interval has changed
493+
# 3. The start or end time has changed
494+
should_update_bookings = (
495+
not old_instance or # New occurrence
496+
old_instance.interval != self.interval or # Interval changed
497+
old_instance.start != self.start or # Start time changed
498+
old_instance.end != self.end # End time changed
499+
)
500+
501+
if should_update_bookings and self.interval:
502+
# Delete existing bookings
503+
if self.pk:
504+
self.bookings.all().delete()
505+
506+
# Create new bookings
507+
delta = self.end - self.start
508+
delta_minutes = delta.total_seconds() / 60
509+
booking_count = int(delta_minutes // self.interval)
510+
511+
for i in range(booking_count):
512+
booking_start = self.start + timedelta(minutes=i * self.interval)
513+
booking_end = booking_start + timedelta(minutes=self.interval)
514+
Booking.objects.create(
515+
occurrence=self,
516+
user=None,
517+
start=booking_start,
518+
end=booking_end,
519+
)
500520

501521
def moved(self):
502522
return self.original_start != self.start or self.original_end != self.end

0 commit comments

Comments
 (0)