photo26/photologue/management/commands/plflush.py
2022-03-02 21:23:40 +01:00

31 lines
1 KiB
Python

# Based on https://github.com/richardbarran/django-photologue/
# by Richard Barran, BSD-3 licensed
from django.core.management.base import BaseCommand, CommandError
from photologue.models import ImageModel, PhotoSize
class Command(BaseCommand):
help = "Clears the Photologue cache for the given sizes."
def add_arguments(self, parser):
parser.add_argument("sizes", nargs="*", type=str, help="Name of the photosize.")
def handle(self, *args, **options):
sizes = options["sizes"]
if not sizes:
photosizes = PhotoSize.objects.all()
else:
photosizes = PhotoSize.objects.filter(name__in=sizes)
if not len(photosizes):
raise CommandError("No photo sizes were found.")
print("Flushing cache...")
for cls in ImageModel.__subclasses__():
for photosize in photosizes:
print("Flushing %s size images" % photosize.name)
for obj in cls.objects.all():
obj.remove_size(photosize)