Send messages on shift changes
parent
77d4ecadec
commit
552529cd6b
@ -0,0 +1,39 @@
|
||||
from django.db.models.signals import pre_save
|
||||
from django.dispatch import receiver
|
||||
from django.shortcuts import reverse
|
||||
from django.template import Context, Template
|
||||
from .models import Message, Shift
|
||||
|
||||
|
||||
@receiver(pre_save, dispatch_uid="notify_shift_changed")
|
||||
def notify_shift_changed(sender, **kwargs):
|
||||
if issubclass(sender, Shift):
|
||||
instance = kwargs["instance"]
|
||||
try:
|
||||
prev = Shift.objects.get(pk=instance.id)
|
||||
except Shift.DoesNotExist:
|
||||
return
|
||||
|
||||
if (
|
||||
prev.room == instance.room
|
||||
and prev.start_at == instance.start_at
|
||||
and prev.duration == instance.duration
|
||||
and prev.deleted == instance.deleted
|
||||
):
|
||||
return
|
||||
|
||||
message = f"Eine deiner kontakt-Schichten hat sich geändert: https://kontakt.rocks{reverse('shift', kwargs={'shiftid': instance.id})}"
|
||||
if instance.deleted:
|
||||
template = Template(
|
||||
'Deine kontakt-Schicht "{{ room }}" am {{ start_at|date:"l" }} um {{ start_at|date:"H:i" }} Uhr entfällt.'
|
||||
)
|
||||
message = template.render(
|
||||
Context({"room": prev.room, "start_at": prev.start_at})
|
||||
)
|
||||
|
||||
Message.objects.bulk_create(
|
||||
(
|
||||
Message(text=message, to=reg.helper)
|
||||
for reg in instance.shiftregistration_set.all()
|
||||
)
|
||||
)
|
Loading…
Reference in New Issue