plcache.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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('--reset', '-r', action='store_true', dest='reset', help='Reset photo cache before generating'),
  7. )
  8. help = ('Manages Photologue cache file for the given sizes.')
  9. args = '[sizes]'
  10. requires_model_validation = True
  11. can_import_settings = True
  12. def handle(self, *args, **options):
  13. return create_cache(args, options)
  14. def create_cache(sizes, options):
  15. """
  16. Creates the cache for the given files
  17. """
  18. reset = options.get('reset', None)
  19. size_list = [size.strip(' ,') for size in sizes]
  20. if len(size_list) < 1:
  21. sizes = PhotoSize.objects.filter(pre_cache=True)
  22. else:
  23. sizes = PhotoSize.objects.filter(name__in=size_list)
  24. if not len(sizes):
  25. raise CommandError('No photo sizes were found.')
  26. print 'Caching photos, this may take a while...'
  27. for cls in ImageModel.__subclasses__():
  28. for photosize in sizes:
  29. print 'Cacheing %s size images' % photosize.name
  30. for obj in cls.objects.all():
  31. if reset:
  32. obj.remove_size(photosize)
  33. obj.create_size(photosize)