Skip to content
Snippets Groups Projects
Commit 8b5ffde8 authored by Benjamin Hättasch's avatar Benjamin Hättasch Committed by Nadja Geisler
Browse files

Update/delete constraint violations when corresponding slot is deleted

This fixes #131
parent c9e4be83
No related branches found
No related tags found
No related merge requests found
from django.db.models.signals import post_save, m2m_changed from django.db.models.signals import post_save, m2m_changed, pre_delete
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
...@@ -551,6 +551,19 @@ def akslot_changed_handler(sender, instance: AKSlot, **kwargs): ...@@ -551,6 +551,19 @@ def akslot_changed_handler(sender, instance: AKSlot, **kwargs):
update_constraint_violations(new_violations, existing_violations_to_check) update_constraint_violations(new_violations, existing_violations_to_check)
@receiver(pre_delete, sender=AKSlot)
def akslot_deleted_handler(sender, instance: AKSlot, **kwargs):
# Manually clean up or remove constraint violations that belong to this slot since there is no cascade deletion
# for many2many relationships. Explicitly listening for AK deletion signals is not necessary since they will
# transitively trigger this signal and we always set both AK and AKSlot references in a constraint violation
print(f"{instance} deleted")
for cv in instance.constraintviolation_set.all():
# Make sure not delete CVs that e.g., show three parallel slots in a single room
if cv.ak_slots.count() <= 2:
cv.delete()
@receiver(post_save, sender=Room) @receiver(post_save, sender=Room)
def room_changed_handler(sender, instance: Room, **kwargs): def room_changed_handler(sender, instance: Room, **kwargs):
# Changes might affect: Room size # Changes might affect: Room size
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment