Add admin action to reprocess attachments

This commit is contained in:
Gabriel Augendre 2020-12-27 19:23:13 +01:00
parent 83db1f43cf
commit de360cf100
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
3 changed files with 19 additions and 3 deletions

View file

@ -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"

View file

@ -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."))

View file

@ -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)