From de360cf1007d842b419712b65dba5d2fde65ed41 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 27 Dec 2020 19:23:13 +0100 Subject: [PATCH] Add admin action to reprocess attachments --- attachments/admin.py | 15 ++++++++++++++- .../commands/reprocess_all_attachments.py | 3 +-- attachments/models.py | 4 ++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/attachments/admin.py b/attachments/admin.py index 5125a60..c0078d7 100644 --- a/attachments/admin.py +++ b/attachments/admin.py @@ -29,7 +29,10 @@ class AttachmentAdmin(admin.ModelAdmin): "processed_file_url", "open_graph_image", ] - actions = ["set_as_open_graph_image"] + actions = [ + "set_as_open_graph_image", + "reprocess_selected_attachments", + ] class Media: js = ["attachments/js/copy_url.js"] @@ -61,3 +64,13 @@ class AttachmentAdmin(admin.ModelAdmin): messages.success(request, "Done") set_as_open_graph_image.short_description = "Set as open graph image" + + def reprocess_selected_attachments(self, request, queryset): + if len(queryset) == 0: + messages.error(request, "You must select at least one attachment") + return + for attachment in queryset: + attachment.reprocess() + messages.success(request, "Attachments were successfully reprocessed.") + + reprocess_selected_attachments.short_description = "Reprocess selected attachments" diff --git a/attachments/management/commands/reprocess_all_attachments.py b/attachments/management/commands/reprocess_all_attachments.py index 79b75bc..9414638 100644 --- a/attachments/management/commands/reprocess_all_attachments.py +++ b/attachments/management/commands/reprocess_all_attachments.py @@ -9,6 +9,5 @@ class Command(BaseCommand): def handle(self, *args, **options): for attachment in Attachment.objects.all(): self.stdout.write(f"Processing {attachment}...") - attachment.processed_file = None - attachment.save() + attachment.reprocess() self.stdout.write(self.style.SUCCESS("Successfully processed all attachments.")) diff --git a/attachments/models.py b/attachments/models.py index 593186a..81c8da3 100644 --- a/attachments/models.py +++ b/attachments/models.py @@ -40,6 +40,10 @@ class Attachment(models.Model): def __str__(self): return f"{self.description} ({self.original_file.name})" + def reprocess(self): + self.processed_file = None + self.save() + def save(self, *args, **kwargs): super().save(*args, **kwargs)