Commit 33e8344d authored by Jelle Besseling's avatar Jelle Besseling
Browse files

Merge branch 'fix-recursion-error' into 'master'

Fix recursion error events models

Closes #1018 and #1015

See merge request !1511
parents 7190f04e 69ac87c2
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
from django.conf import settings from django.conf import settings
from django.core import validators from django.core import validators
from django.core.exceptions import ValidationError, ObjectDoesNotExist 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 import Q
from django.db.models.deletion import Collector
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
...@@ -395,6 +396,10 @@ class Event(models.Model, metaclass=ModelTranslateMeta): ...@@ -395,6 +396,10 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
return reverse("events:event", args=[str(self.pk)]) return reverse("events:event", args=[str(self.pk)])
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
delete_collector = Collector(
using=router.db_for_write(self.__class__, instance=self)
)
if not self.pk: if not self.pk:
super().save(*args, **kwargs) super().save(*args, **kwargs)
...@@ -433,8 +438,8 @@ class Event(models.Model, metaclass=ModelTranslateMeta): ...@@ -433,8 +438,8 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
self.registration_reminder = registration_reminder self.registration_reminder = registration_reminder
self.registration_reminder.users.set(Member.current_members.all()) self.registration_reminder.users.set(Member.current_members.all())
elif registration_reminder.pk is not None: elif registration_reminder.pk is not None:
delete_collector.add([self.registration_reminder])
self.registration_reminder = None self.registration_reminder = None
registration_reminder.delete()
start_reminder_time = self.start - timezone.timedelta(hours=1) start_reminder_time = self.start - timezone.timedelta(hours=1)
start_reminder = ScheduledMessage() start_reminder = ScheduledMessage()
...@@ -457,32 +462,39 @@ class Event(models.Model, metaclass=ModelTranslateMeta): ...@@ -457,32 +462,39 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
else: else:
self.start_reminder.users.set(Member.current_members.all()) self.start_reminder.users.set(Member.current_members.all())
elif start_reminder.pk is not None: elif start_reminder.pk is not None:
delete_collector.add([self.start_reminder])
self.start_reminder = None self.start_reminder = None
start_reminder.delete()
else: else:
if ( if (
self.registration_reminder is not None self.registration_reminder is not None
and not self.registration_reminder.sent and not self.registration_reminder.sent
): ):
self.registration_reminder.delete() delete_collector.add([self.registration_reminder])
self.registration_reminder = None self.registration_reminder = None
if self.start_reminder is not None and not self.start_reminder.sent: 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 self.start_reminder = None
super().save() super().save()
delete_collector.delete()
def delete(self, using=None, keep_parents=False): 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 ( if (
self.registration_reminder is not None self.registration_reminder is not None
and not self.registration_reminder.sent 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: 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(): if self.is_pizza_event():
self.pizzaevent.delete() collector.add([self.pizzaevent])
return super().delete(using, keep_parents)
return collector.delete()
def __str__(self): def __str__(self):
return "{}: {}".format( return "{}: {}".format(
......
Supports Markdown
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