From 83db1f43cf16524579177ffd48872df98d0023c9 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 27 Dec 2020 19:13:21 +0100 Subject: [PATCH] Allow reprocessing all attachments via django command --- .../commands/reprocess_all_attachments.py | 14 ++++++++++++++ attachments/models.py | 10 +++++----- 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 attachments/management/commands/reprocess_all_attachments.py diff --git a/attachments/management/commands/reprocess_all_attachments.py b/attachments/management/commands/reprocess_all_attachments.py new file mode 100644 index 0000000..79b75bc --- /dev/null +++ b/attachments/management/commands/reprocess_all_attachments.py @@ -0,0 +1,14 @@ +from django.core.management.base import BaseCommand + +from attachments.models import Attachment + + +class Command(BaseCommand): + help = "Reprocess all attachments" + + def handle(self, *args, **options): + for attachment in Attachment.objects.all(): + self.stdout.write(f"Processing {attachment}...") + attachment.processed_file = None + attachment.save() + self.stdout.write(self.style.SUCCESS("Successfully processed all attachments.")) diff --git a/attachments/models.py b/attachments/models.py index 8062bdf..593186a 100644 --- a/attachments/models.py +++ b/attachments/models.py @@ -41,15 +41,15 @@ class Attachment(models.Model): return f"{self.description} ({self.original_file.name})" def save(self, *args, **kwargs): - if self.processed_file: - return super().save(*args, **kwargs) + super().save(*args, **kwargs) + + if self.processed_file: + return - if self.id is None: - super().save(*args, **kwargs) try: Image.open(self.original_file.path) except IOError: - return super().save(*args, **kwargs) + return # Submit job to shortpixel base_data = {