From b6cec8ab1164034a29e36e4173e073f3b696b7f3 Mon Sep 17 00:00:00 2001 From: meeb Date: Sun, 14 Jul 2024 19:13:14 +1000 Subject: [PATCH] move saving all media items linked to a source when the source is saved to recalculate media skip status to background task, resolves #94 --- tubesync/sync/signals.py | 15 +++++++-------- tubesync/sync/tasks.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/tubesync/sync/signals.py b/tubesync/sync/signals.py index 52146c5..3e57fb0 100644 --- a/tubesync/sync/signals.py +++ b/tubesync/sync/signals.py @@ -67,10 +67,13 @@ def source_post_save(sender, instance, created, **kwargs): verbose_name=verbose_name.format(instance.name), remove_existing_tasks=True ) - # Trigger the post_save signal for each media item linked to this source as various - # flags may need to be recalculated - for media in Media.objects.filter(source=instance): - media.save() + verbose_name = _('Checking all media for source "{}"') + save_all_media_for_source( + str(instance.pk), + priority=0, + verbose_name=verbose_name.format(instance.name), + remove_existing_tasks=True + ) @receiver(pre_delete, sender=Source) @@ -82,7 +85,6 @@ def source_pre_delete(sender, instance, **kwargs): media.delete() - @receiver(post_delete, sender=Source) def source_post_delete(sender, instance, **kwargs): # Triggered after a source is deleted @@ -173,8 +175,6 @@ def media_post_save(sender, instance, created, **kwargs): ) - - @receiver(pre_delete, sender=Media) def media_pre_delete(sender, instance, **kwargs): # Triggered before media is deleted, delete any scheduled tasks @@ -195,7 +195,6 @@ def media_pre_delete(sender, instance, **kwargs): delete_file(file) - @receiver(post_delete, sender=Media) def media_post_delete(sender, instance, **kwargs): # Schedule a task to update media servers diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 305c0b8..c5f64f3 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -49,6 +49,7 @@ def map_task_to_instance(task): 'sync.tasks.check_source_directory_exists': Source, 'sync.tasks.download_media_thumbnail': Media, 'sync.tasks.download_media': Media, + 'sync.tasks.save_all_media_for_source': Source, } MODEL_URL_MAP = { Source: 'sync:source', @@ -464,3 +465,24 @@ def rescan_media_server(mediaserver_id): # Request an rescan / update log.info(f'Updating media server: {mediaserver}') mediaserver.update() + + +@background(schedule=0) +def save_all_media_for_source(source_id): + ''' + Iterates all media items linked to a source and saves them to + trigger the post_save signal for every media item. Used when a + source has its parameters changed and all media needs to be + checked to see if its download status has changed. + ''' + try: + source = Source.objects.get(pk=source_id) + except Source.DoesNotExist: + # Task triggered but the source no longer exists, do nothing + log.error(f'Task save_all_media_for_source(pk={source_id}) called but no ' + f'source exists with ID: {source_id}') + return + # Trigger the post_save signal for each media item linked to this source as various + # flags may need to be recalculated + for media in Media.objects.filter(source=source): + media.save()