summaryrefslogtreecommitdiffstats
path: root/imagekit/management/commands/ikflush.py
blob: c03440f4338c24000aadcbe1f3c05264a144c98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from django.db.models.loading import cache
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
from imagekit.models import ImageModel
from imagekit.specs import ImageSpec


class Command(BaseCommand):
    help = ('Clears all ImageKit cached files.')
    args = '[apps]'
    requires_model_validation = True
    can_import_settings = True

    def handle(self, *args, **options):
        return flush_cache(args, options)

def flush_cache(apps, options):
    """ Clears the image cache
    
    """
    apps = [a.strip(',') for a in apps]
    if apps:
        print 'Flushing cache for %s...' % ', '.join(apps)
    else:
        print 'Flushing caches...'
        
    for app_label in apps:
        app = cache.get_app(app_label)
        models = [m for m in cache.get_models(app) if issubclass(m, ImageModel)]

    for model in models:
        for obj in model.objects.all():
            for spec in model._ik.specs:
                prop = getattr(obj, spec.name(), None)
                if prop is not None:
                    prop._delete()
                if spec.pre_cache:
                    prop._create()