45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
from django.dispatch import receiver
|
|
from django.db.models.signals import post_save, post_delete
|
|
from .models import Restream, Stream
|
|
from concierge.models import Task
|
|
from .signals_shared import stream_active, stream_inactive
|
|
|
|
@receiver(stream_active)
|
|
def create_tasks(sender, **kwargs):
|
|
stream = Stream.objects.get(stream=kwargs['stream'])
|
|
instances = Restream.objects.filter(active=True, stream=stream)
|
|
for instance in instances:
|
|
task = Task(stream=instance.stream, type='restream', config_id=instance.id,
|
|
configuration=instance.get_json_config())
|
|
task.save()
|
|
|
|
|
|
@receiver(post_save, sender=Restream)
|
|
def update_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=instance.get_json_config())
|
|
task.save()
|
|
|
|
|
|
@receiver(post_delete, sender=Restream)
|
|
def delete_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
|