Commit 69ac87c2 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg Committed by Jelle Besseling
Browse files

Fix recursion error events models

parent 7190f04e
......@@ -2,8 +2,9 @@
from django.conf import settings
from django.core import validators
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.db import models
from django.db import models, router
from django.db.models import Q
from django.db.models.deletion import Collector
from django.urls import reverse
from django.utils import timezone
from django.utils.crypto import get_random_string
......@@ -395,6 +396,10 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
return reverse("events:event", args=[str(self.pk)])
def save(self, *args, **kwargs):
delete_collector = Collector(
using=router.db_for_write(self.__class__, instance=self)
)
if not self.pk:
super().save(*args, **kwargs)
......@@ -433,8 +438,8 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
self.registration_reminder = registration_reminder
self.registration_reminder.users.set(Member.current_members.all())
elif registration_reminder.pk is not None:
delete_collector.add([self.registration_reminder])
self.registration_reminder = None
registration_reminder.delete()
start_reminder_time = self.start - timezone.timedelta(hours=1)
start_reminder = ScheduledMessage()
......@@ -457,32 +462,39 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
else:
self.start_reminder.users.set(Member.current_members.all())
elif start_reminder.pk is not None:
delete_collector.add([self.start_reminder])
self.start_reminder = None
start_reminder.delete()
else:
if (
self.registration_reminder is not None
and not self.registration_reminder.sent
):
self.registration_reminder.delete()
delete_collector.add([self.registration_reminder])
self.registration_reminder = None
if self.start_reminder is not None and not self.start_reminder.sent:
self.start_reminder.delete()
delete_collector.add([self.start_reminder])
self.start_reminder = None
super().save()
delete_collector.delete()
def delete(self, using=None, keep_parents=False):
using = using or router.db_for_write(self.__class__, instance=self)
collector = Collector(using=using)
collector.collect([self], keep_parents=keep_parents)
if (
self.registration_reminder is not None
and not self.registration_reminder.sent
):
self.registration_reminder.delete()
collector.add([self.registration_reminder])
if self.start_reminder is not None and not self.start_reminder.sent:
self.start_reminder.delete()
collector.add([self.start_reminder])
if self.is_pizza_event():
self.pizzaevent.delete()
return super().delete(using, keep_parents)
collector.add([self.pizzaevent])
return collector.delete()
def __str__(self):
return "{}: {}".format(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment