Add admin action to reprocess attachments
This commit is contained in:
parent
83db1f43cf
commit
de360cf100
3 changed files with 19 additions and 3 deletions
|
@ -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"
|
||||
|
|
|
@ -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."))
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Reference in a new issue