2017-08-14 16:58:19 +00:00
|
|
|
import logging
|
|
|
|
|
2017-01-23 17:57:30 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2019-06-16 12:32:24 +00:00
|
|
|
|
2020-03-05 11:23:42 +00:00
|
|
|
from .email import add_event_scheduled_email
|
|
|
|
|
2017-08-14 16:58:19 +00:00
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
2017-01-23 17:57:30 +00:00
|
|
|
|
2017-07-09 14:48:02 +00:00
|
|
|
|
2017-07-09 14:54:21 +00:00
|
|
|
def check_speaker_event_camp_consistency(sender, instance, **kwargs):
|
2019-06-16 12:32:24 +00:00
|
|
|
if kwargs["action"] == "pre_add":
|
2017-07-11 20:50:31 +00:00
|
|
|
from program.models import Speaker, Event
|
2019-06-16 12:32:24 +00:00
|
|
|
|
2017-07-11 20:50:31 +00:00
|
|
|
if isinstance(instance, Event):
|
|
|
|
# loop over speakers being added to this event
|
2019-06-16 12:32:24 +00:00
|
|
|
for pk in kwargs["pk_set"]:
|
2017-07-11 20:50:31 +00:00
|
|
|
# check if this speaker belongs to a different Camp than the event does
|
|
|
|
speaker = Speaker.objects.get(id=pk)
|
|
|
|
if speaker.camp != instance.camp:
|
2019-06-16 12:32:24 +00:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"speakers": "The speaker (%s) belongs to a different camp (%s) than the event does (%s)"
|
|
|
|
% (speaker, speaker.camp, instance.camp)
|
|
|
|
}
|
|
|
|
)
|
2017-07-11 20:50:31 +00:00
|
|
|
elif isinstance(instance, Speaker):
|
|
|
|
# loop over events being added to this speaker
|
2019-06-16 12:32:24 +00:00
|
|
|
for pk in kwargs["pk_set"]:
|
2017-07-11 20:50:31 +00:00
|
|
|
# check if this event belongs to a different Camp than the speaker does
|
|
|
|
event = Event.objects.get(id=pk)
|
|
|
|
if event.camp != instance.camp:
|
2019-06-16 12:32:24 +00:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
2020-03-05 11:23:42 +00:00
|
|
|
"events": f"The event ({event}) belongs to a different camp ({event.camp}) than the speaker does ({instance.camp})"
|
2019-06-16 12:32:24 +00:00
|
|
|
}
|
|
|
|
)
|
2017-07-09 14:48:02 +00:00
|
|
|
|
2017-01-23 17:57:30 +00:00
|
|
|
|
|
|
|
def check_speaker_camp_change(sender, instance, **kwargs):
|
2017-01-25 22:48:37 +00:00
|
|
|
if instance.pk:
|
|
|
|
for event in instance.events.all():
|
|
|
|
if event.camp != instance.camp:
|
2019-06-16 12:32:24 +00:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"camp": "You cannot change the camp a speaker belongs to if the speaker is associated with one or more events."
|
|
|
|
}
|
|
|
|
)
|
2020-03-05 11:23:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def eventinstance_pre_save(sender, instance, **kwargs):
|
|
|
|
""" Save the old instance.when value so we can later determine if it changed """
|
|
|
|
try:
|
|
|
|
# get the old instance from the database, if we have one
|
|
|
|
instance.old_when = sender.objects.get(pk=instance.pk).when
|
|
|
|
except sender.DoesNotExist:
|
|
|
|
# nothing found in the DB with this pk, this is a new eventinstance
|
|
|
|
instance.old_when = instance.when
|
|
|
|
|
|
|
|
|
|
|
|
def eventinstance_post_save(sender, instance, created, **kwargs):
|
|
|
|
""" Send an email if this is a new eventinstance, or if the "when" field changed """
|
|
|
|
if created:
|
|
|
|
add_event_scheduled_email(eventinstance=instance, action="scheduled")
|
|
|
|
else:
|
|
|
|
if instance.old_when != instance.when:
|
|
|
|
# date/time for this eventinstance changed, send a rescheduled email
|
|
|
|
add_event_scheduled_email(eventinstance=instance, action="rescheduled")
|