plcache.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from django.core.management.base import BaseCommand, CommandError
  2. from optparse import make_option
  3. from photologue.models import PhotoSize, ImageModel
  4. class Command(BaseCommand):
  5. option_list = BaseCommand.option_list + (
  6. make_option(
  7. "--reset",
  8. "-r",
  9. action="store_true",
  10. dest="reset",
  11. help="Reset photo cache before generating",
  12. ),
  13. )
  14. help = "Manages Photologue cache file for the given sizes."
  15. args = "[sizes]"
  16. requires_model_validation = True
  17. can_import_settings = True
  18. def handle(self, *args, **options):
  19. return create_cache(args, options)
  20. def create_cache(sizes, options):
  21. """
  22. Creates the cache for the given files
  23. """
  24. reset = options.get("reset", None)
  25. size_list = [size.strip(" ,") for size in sizes]
  26. if len(size_list) < 1:
  27. sizes = PhotoSize.objects.filter(pre_cache=True)
  28. else:
  29. sizes = PhotoSize.objects.filter(name__in=size_list)
  30. if not len(sizes):
  31. raise CommandError("No photo sizes were found.")
  32. print "Caching photos, this may take a while..."
  33. for cls in ImageModel.__subclasses__():
  34. for photosize in sizes:
  35. print "Cacheing %s size images" % photosize.name
  36. for obj in cls.objects.all():
  37. if reset:
  38. obj.remove_size(photosize)
  39. obj.create_size(photosize)