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