annotate photologue/management/commands/plcache.py @ 139:c962f58c2693

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