bgneal@1: from django.core.management.base import BaseCommand, CommandError bgneal@1: from optparse import make_option bgneal@1: from photologue.models import PhotoSize, ImageModel bgneal@1: bgneal@1: class Command(BaseCommand): bgneal@1: option_list = BaseCommand.option_list + ( bgneal@1: make_option('--reset', '-r', action='store_true', dest='reset', help='Reset photo cache before generating'), bgneal@1: ) bgneal@1: bgneal@1: help = ('Manages Photologue cache file for the given sizes.') bgneal@1: args = '[sizes]' bgneal@1: bgneal@1: requires_model_validation = True bgneal@1: can_import_settings = True bgneal@1: bgneal@1: def handle(self, *args, **options): bgneal@1: return create_cache(args, options) bgneal@1: bgneal@1: def create_cache(sizes, options): bgneal@1: """ bgneal@1: Creates the cache for the given files bgneal@1: """ bgneal@1: reset = options.get('reset', None) bgneal@1: bgneal@1: size_list = [size.strip(' ,') for size in sizes] bgneal@1: bgneal@1: if len(size_list) < 1: bgneal@1: sizes = PhotoSize.objects.filter(pre_cache=True) bgneal@1: else: bgneal@1: sizes = PhotoSize.objects.filter(name__in=size_list) bgneal@1: bgneal@1: if not len(sizes): bgneal@1: raise CommandError('No photo sizes were found.') bgneal@1: bgneal@1: print 'Caching photos, this may take a while...' bgneal@1: bgneal@1: for cls in ImageModel.__subclasses__(): bgneal@1: for photosize in sizes: bgneal@1: print 'Cacheing %s size images' % photosize.name bgneal@1: for obj in cls.objects.all(): bgneal@1: if reset: bgneal@1: obj.remove_size(photosize) bgneal@1: obj.create_size(photosize)