bornhack-website/src/program/signal_handlers.py
Thomas Steen Rasmussen 00af109e2f
add flake8 and isort to pre-commit config, make flake8 and isort happy (#441)
* add flake8 to pre-commit config, and fixup many things to make flake8 happy

* add isort and sort all imports, add to pre-commit and requirements
2020-02-12 13:10:41 +01:00

47 lines
1.9 KiB
Python

import logging
from django.core.exceptions import ValidationError
logger = logging.getLogger("bornhack.%s" % __name__)
def check_speaker_event_camp_consistency(sender, instance, **kwargs):
if kwargs["action"] == "pre_add":
from program.models import Speaker, Event
if isinstance(instance, Event):
# loop over speakers being added to this event
for pk in kwargs["pk_set"]:
# check if this speaker belongs to a different Camp than the event does
speaker = Speaker.objects.get(id=pk)
if speaker.camp != instance.camp:
raise ValidationError(
{
"speakers": "The speaker (%s) belongs to a different camp (%s) than the event does (%s)"
% (speaker, speaker.camp, instance.camp)
}
)
elif isinstance(instance, Speaker):
# loop over events being added to this speaker
for pk in kwargs["pk_set"]:
# check if this event belongs to a different Camp than the speaker does
event = Event.objects.get(id=pk)
if event.camp != instance.camp:
raise ValidationError(
{
"events": "The event (%s) belongs to a different camp (%s) than the event does (%s)"
% (event, event.camp, instance.camp)
}
)
def check_speaker_camp_change(sender, instance, **kwargs):
if instance.pk:
for event in instance.events.all():
if event.camp != instance.camp:
raise ValidationError(
{
"camp": "You cannot change the camp a speaker belongs to if the speaker is associated with one or more events."
}
)