portier/source/config/signals/restream.py

47 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-04-01 16:27:31 +02:00
import json
from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete
from config.models import Restream, Stream
from config.signals_shared import stream_active, stream_inactive
from concierge.models import Task
@receiver(stream_active)
def create_restream_tasks(sender, **kwargs):
stream = Stream.objects.get(stream=kwargs['stream'])
restreams = Restream.objects.filter(active=True, stream=stream)
for restream in restreams:
Task.objects.get_or_create(stream=restream.stream, type='restream', config_id=restream.id,
configuration=json.dumps(restream.get_concierge_configuration()))
@receiver(post_save, sender=Restream)
def update_restream_tasks(sender, **kwargs):
instance = kwargs['instance']
# TODO: check for breaking changes using update_fields. This needs custom save_model functions though.
# Get the current task instance if it exists, and remove it
try:
task = Task.objects.filter(type='restream', config_id=instance.id).get()
task.delete()
except Task.DoesNotExist:
pass
# If the configuration is set to be active, and the stream is published, (re)create new task
if instance.active and instance.stream.publish_counter > 0:
task = Task(stream=instance.stream, type='restream', config_id=instance.id,
configuration=json.dumps(instance.get_concierge_configuration()))
task.save()
@receiver(post_delete, sender=Restream)
def delete_restream_tasks(sender, **kwargs):
instance = kwargs['instance']
# Get the current task instance if it exists, and remove it
try:
task = Task.objects.filter(type='restream', config_id=instance.id).get()
task.delete()
except Task.DoesNotExist:
pass