From 8b6ba687e4ce0d3306cced4ef93be2438d7c0934 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Thu, 2 Feb 2012 00:19:46 -0500 Subject: [PATCH] Commands for validating and invalidating the cache --- .../management/commands/ikcacheinvalidate.py | 14 +++++++++ .../management/commands/ikcachevalidate.py | 30 ++++++++++++++++++ imagekit/management/commands/ikflush.py | 31 ------------------- imagekit/utils.py | 31 +++++++++++++++++++ 4 files changed, 75 insertions(+), 31 deletions(-) create mode 100644 imagekit/management/commands/ikcacheinvalidate.py create mode 100644 imagekit/management/commands/ikcachevalidate.py delete mode 100644 imagekit/management/commands/ikflush.py diff --git a/imagekit/management/commands/ikcacheinvalidate.py b/imagekit/management/commands/ikcacheinvalidate.py new file mode 100644 index 0000000..2b6e915 --- /dev/null +++ b/imagekit/management/commands/ikcacheinvalidate.py @@ -0,0 +1,14 @@ +from django.core.management.base import BaseCommand +from django.db.models.loading import cache +from ...utils import invalidate_app_cache + + +class Command(BaseCommand): + help = ('Invalidates the image cache for a list of apps.') + args = '[apps]' + requires_model_validation = True + can_import_settings = True + + def handle(self, *args, **options): + apps = args or cache.app_models.keys() + invalidate_app_cache(apps) diff --git a/imagekit/management/commands/ikcachevalidate.py b/imagekit/management/commands/ikcachevalidate.py new file mode 100644 index 0000000..fc9a9c0 --- /dev/null +++ b/imagekit/management/commands/ikcachevalidate.py @@ -0,0 +1,30 @@ +from optparse import make_option +from django.core.management.base import BaseCommand +from django.db.models.loading import cache +from ...utils import validate_app_cache + + +class Command(BaseCommand): + help = ('Validates the image cache for a list of apps.') + args = '[apps]' + requires_model_validation = True + can_import_settings = True + + option_list = BaseCommand.option_list + ( + make_option('--force', + dest='force_revalidation', + action='store_true', + default=False, + help='Invalidate each image file before validating it, thereby' + ' ensuring its revalidation. This is very similar to' + ' running ikcacheinvalidate and then running' + ' ikcachevalidate; the difference being that this option' + ' causes files to be invalidated and validated' + ' one-at-a-time, whereas running the two commands in series' + ' would invalidate all images before validating any.' + ), + ) + + def handle(self, *args, **options): + apps = args or cache.app_models.keys() + validate_app_cache(apps, options['force_revalidation']) diff --git a/imagekit/management/commands/ikflush.py b/imagekit/management/commands/ikflush.py deleted file mode 100644 index de4be3f..0000000 --- a/imagekit/management/commands/ikflush.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Flushes and re-caches all images under ImageKit. - -""" -from django.db.models.loading import cache -from django.core.management.base import BaseCommand -from .utils import get_spec_files - - -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): - apps = [a.strip(',') for a in apps] - if apps: - for app_label in apps: - app = cache.get_app(app_label) - for model in [m for m in cache.get_models(app)]: - print 'Flushing cache for "%s.%s"' % (app_label, model.__name__) - for obj in model.objects.order_by('-pk'): - for f in get_spec_files(obj): - f.invalidate() - else: - print 'Please specify one or more app names' diff --git a/imagekit/utils.py b/imagekit/utils.py index 02e71c1..305bdd4 100644 --- a/imagekit/utils.py +++ b/imagekit/utils.py @@ -1,6 +1,7 @@ import tempfile import types +from django.db.models.loading import cache from django.utils.functional import wraps from imagekit.lib import Image, ImageFile @@ -134,3 +135,33 @@ def format_to_extension(format): if not extension: raise UnknownFormatError(format) return extension + + +def _get_models(apps): + models = [] + for app_label in apps or []: + app = cache.get_app(app_label) + models += [m for m in cache.get_models(app)] + return models + + +def invalidate_app_cache(apps): + for model in _get_models(apps): + print 'Invalidating cache for "%s.%s"' % (model._meta.app_label, model.__name__) + for obj in model._default_manager.order_by('-pk'): + for f in get_spec_files(obj): + f.invalidate() + + +def validate_app_cache(apps, force_revalidation=False): + for model in _get_models(apps): + for obj in model._default_manager.order_by('-pk'): + model_name = '%s.%s' % (model._meta.app_label, model.__name__) + if force_revalidation: + print 'Invalidating & validating cache for "%s"' % model_name + else: + print 'Validating cache for "%s"' % model_name + for f in get_spec_files(obj): + if force_revalidation: + f.invalidate() + f.validate()