import json from django.dispatch import receiver from django.db.models.signals import post_save, post_delete from config.models import Stream, Pull from config.signals_shared import stream_active, stream_inactive, pull_active, pull_inactive from concierge.models import Task @receiver(pull_active) def create_pull_tasks_on_activate(sender, **kwargs): pull = Pull.objects.get(id=kwargs['pull_id']) Task.objects.get_or_create(stream=pull.stream, type='pull', config_id=pull.id, configuration=json.dumps(pull.get_concierge_configuration())) @receiver(pull_inactive) def delete_pull_tasks_on_inactivate(sender, **kwargs): pull = Pull.objects.get(id=kwargs['pull_id']) Task.objects.filter(type='pull', config_id=pull.id).delete() @receiver(post_save, sender=Pull) def update_pull_tasks(sender, **kwargs): instance = kwargs['instance'] try: task = Task.objects.filter(type='pull', config_id=instance.id).get() task.delete() except Task.DoesNotExist: pass if instance.active: task = Task(stream=instance.stream, type='pull', config_id=instance.id, configuration=json.dumps(instance.get_concierge_configuration())) task.save() @receiver(post_delete, sender=Pull) def delete_pull_tasks(sender, **kwargs): instance = kwargs['instance'] # Get the current task instance if it exists, and remove it try: task = Task.objects.filter(type='pull', config_id=instance.id).get() task.delete() except Task.DoesNotExist: pass