Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from django.db.models.signals import post_save
from django.dispatch import receiver
from AKModel.availability.models import Availability
from AKModel.models import AK, AKSlot, Room, Event, AKOwner, ConstraintViolation
@receiver(post_save, sender=AK)
def ak_changed_handler(sender, instance: AK, **kwargs):
# Changes might affect: Owner(s), Requirements, Conflicts, Prerequisites, Category, Interest
print(f"{instance} changed")
event = instance.event
# Owner might have changed: Might affect multiple AKs by the same owner at the same time
conflicts = []
type = ConstraintViolation.ViolationType.OWNER_TWO_SLOTS
# For all owners...
for owner in instance.owners.all():
# ...find overlapping AKs...
slots_by_owner : [AKSlot] = []
slots_by_owner_this_ak : [AKSlot] = []
aks_by_owner = owner.ak_set.all()
for ak in aks_by_owner:
if ak != instance:
slots_by_owner.extend(ak.akslot_set.filter(start__isnull=False))
else:
# ToDo Fill this outside of loop?
slots_by_owner_this_ak.extend(ak.akslot_set.filter(start__isnull=False))
for slot in slots_by_owner_this_ak:
for other_slot in slots_by_owner:
if slot.overlaps(other_slot):
# TODO Create ConstraintViolation here
c = ConstraintViolation(
type=type,
level=ConstraintViolation.ViolationLevel.VIOLATION,
event=event,
ak_owner=owner
)
c.aks_tmp.add(instance)
c.aks_tmp.add(other_slot.ak)
c.ak_slots_tmp.add(slot)
c.ak_slots_tmp.add(other_slot)
conflicts.append(c)
print(f"{owner} has the following conflicts: {conflicts}")
# ... and compare to/update list of existing violations of this type:
current_violations = instance.constraintviolation_set.filter(type=type)
for conflict in conflicts:
pass
# TODO Remove from list of current_violations if an equal new one is found
# TODO Otherwise, store this conflict in db
# TODO Remove all violations still in current_violations
@receiver(post_save, sender=AKSlot)
def akslot_changed_handler(sender, instance, **kwargs):
# Changes might affect: Duplicate parallel, Two in room
print(f"{sender} changed")
# TODO Replace with real handling
@receiver(post_save, sender=Room)
def room_changed_handler(sender, **kwargs):
# Changes might affect: Room size, Requirement
print(f"{sender} changed")
# TODO Replace with real handling
@receiver(post_save, sender=Availability)
def availability_changed_handler(sender, **kwargs):
# Changes might affect: category availability, AK availability, Room availability
print(f"{sender} changed")
# TODO Replace with real handling
@receiver(post_save, sender=Event)
def room_changed_handler(sender, **kwargs):
# Changes might affect: Reso-Deadline
print(f"{sender} changed")
# TODO Replace with real handling