Created
January 7, 2020 15:48
-
-
Save iksi/ec83f8e523bee73354f5292c1068831c to your computer and use it in GitHub Desktop.
Django management command to delete Wagtail’s image renditions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, shutil | |
from django.conf import settings | |
from django.core.management.base import BaseCommand, CommandError | |
from wagtail.images.models import Rendition | |
class Command(BaseCommand): | |
def handle(self, *args, **options): | |
renditions = Rendition.objects.all() | |
if renditions: | |
images_root = os.path.join(settings.MEDIA_ROOT, 'images') | |
message = ['\n'] | |
message.append('This will REMOVE ALL IMAGE RENDITION FILES in {}!\n'.format(images_root)) | |
message.append( | |
'Are you sure you want to do this?\n\n' | |
"Type 'yes' to continue, or 'no' to cancel: " | |
) | |
if input(''.join(message)) != 'yes': | |
raise CommandError("Remove renditions cancelled.") | |
else: | |
# doe het | |
renditions.delete() | |
shutil.rmtree(images_root) | |
self.stdout.write('Done') | |
else: | |
self.stdout.write('There are no image renditions') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment