mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-19 22:50:24 +00:00
This new feature gives the user more control over *when* their images are validated. Image cache backends are now exclusively for controlling the *how*. This means you won't have to write a lot of code when you just want to change one or the other.
57 lines
1.4 KiB
Python
57 lines
1.4 KiB
Python
from .actions import validate_now, clear_now
|
|
from ..utils import get_singleton
|
|
|
|
|
|
class Pessimistic(object):
|
|
"""
|
|
A caching strategy that validates the file every time it's accessed.
|
|
|
|
"""
|
|
|
|
def on_access(self, file):
|
|
validate_now(file)
|
|
|
|
def on_source_delete(self, file):
|
|
clear_now(file)
|
|
|
|
def on_source_change(self, file):
|
|
validate_now(file)
|
|
|
|
|
|
class Optimistic(object):
|
|
"""
|
|
A caching strategy that validates when the source file changes and assumes
|
|
that the cached file will persist.
|
|
|
|
"""
|
|
|
|
def on_source_create(self, file):
|
|
validate_now(file)
|
|
|
|
def on_source_delete(self, file):
|
|
clear_now(file)
|
|
|
|
def on_source_change(self, file):
|
|
validate_now(file)
|
|
|
|
|
|
class DictStrategy(object):
|
|
def __init__(self, callbacks):
|
|
for k, v in callbacks.items():
|
|
setattr(self, k, v)
|
|
|
|
|
|
class StrategyWrapper(object):
|
|
def __init__(self, strategy):
|
|
if isinstance(strategy, basestring):
|
|
strategy = get_singleton(strategy, 'image cache strategy')
|
|
elif isinstance(strategy, dict):
|
|
strategy = DictStrategy(strategy)
|
|
elif callable(strategy):
|
|
strategy = strategy()
|
|
self._wrapped = strategy
|
|
|
|
def invoke_callback(self, name, *args, **kwargs):
|
|
func = getattr(self._wrapped, 'on_%s' % name, None)
|
|
if func:
|
|
func(*args, **kwargs)
|