tubesync/app/sync/signals.py

48 lines
1.8 KiB
Python
Raw Normal View History

2020-12-06 01:22:16 +00:00
from django.conf import settings
2020-12-06 02:48:10 +00:00
from django.db.models.signals import post_save, pre_delete, post_delete
2020-12-06 01:22:16 +00:00
from django.dispatch import receiver
from .models import Source, Media
from .tasks import delete_index_source_task, index_source_task, download_media_thumbnail
2020-12-06 02:48:10 +00:00
from .utils import delete_file
2020-12-06 01:22:16 +00:00
@receiver(post_save, sender=Source)
def source_post_save(sender, instance, created, **kwargs):
# Triggered when a source is saved, delete any source tasks that might exist
delete_index_source_task(str(instance.pk))
# Create a new scheduled indexing task as the repeat schedule may have changed
index_source_task(str(instance.pk), repeat=instance.index_schedule)
2020-12-06 01:22:16 +00:00
2020-12-06 02:48:10 +00:00
@receiver(pre_delete, sender=Source)
def source_post_delete(sender, instance, **kwargs):
# Triggered just before a source is deleted, delete all media objects to trigger
# the Media models post_delete signal
for media in Media.objects.filter(source=instance):
media.delete()
2020-12-06 01:22:16 +00:00
@receiver(post_delete, sender=Source)
def source_post_delete(sender, instance, **kwargs):
# Triggered when a source is deleted
delete_index_source_task(str(instance.pk))
@receiver(post_save, sender=Media)
def media_post_save(sender, instance, created, **kwargs):
# Triggered when media is saved
if created:
# If the media is newly created fire a task off to download its thumbnail
metadata = instance.loaded_metadata
thumbnail_url = metadata.get('thumbnail', '')
if thumbnail_url:
download_media_thumbnail(str(instance.pk), thumbnail_url)
@receiver(post_delete, sender=Media)
def media_post_delete(sender, instance, **kwargs):
2020-12-06 02:48:10 +00:00
# Triggered when media is deleted, delete media thumbnail
if instance.thumb:
delete_file(instance.thumb.path)