Changed how cache files are named.

Removed the cache_dir, cache_filename_fields and cache_filename_format
properties of IKOptions. While these were very powerful, I felt that it was
unnecessarily confusing to have two properties (cache_dir and
cache_filename_format) that determine the filename. The new cache_to property is
modeled after ImageField's upload_to and behaves almost identically (the only
exception being that a callable value receives different arguments). In
addition, I felt that the interpolation of model properties provided by
cache_filename_fields, though useful, would be better handled by a utility
function outside of this library.
This commit is contained in:
Matthew Tretter 2011-09-18 21:08:49 -04:00
parent 8cfe485a5a
commit 82348d4931
3 changed files with 39 additions and 27 deletions

View file

@ -33,8 +33,8 @@ Create an ImageModel subclass and add specs to it.
original_image = models.ImageField(upload_to='photos')
num_views = models.PositiveIntegerField(editable=False, default=0)
thumbnail_image = ImageSpec([Crop(100, 75), Adjust(contrast=1.2, sharpness=1.1)], quality=90, pre_cache=True, image_field='original_image', cache_dir='photos')
display = ImageSpec([Fit(600)], quality=90, increment_count=True, image_field='original_image', cache_dir='photos', save_count_as='num_views')
thumbnail_image = ImageSpec([Crop(100, 75), Adjust(contrast=1.2, sharpness=1.1)], quality=90, pre_cache=True, image_field='original_image', cache_to='cache/photos/thumbnails/')
display = ImageSpec([Fit(600)], quality=90, increment_count=True, image_field='original_image', cache_to='cache/photos/display/', save_count_as='num_views')
Of course, you don't have to define your ImageSpecs inline if you don't want to:
@ -49,16 +49,17 @@ Of course, you don't have to define your ImageSpecs inline if you don't want to:
class _BaseSpec(ImageSpec):
quality = 90
image_field = 'original_image'
cache_dir = 'photos'
class DisplaySpec(_BaseSpec):
pre_cache = True
increment_count = True
save_count_as = 'num_views'
processors = [Fit(600)]
cache_to = 'cache/photos/display/'
class ThumbnailSpec(_BaseSpec):
processors = [Crop(100, 75), Adjust(contrast=1.2, sharpness=1.1)]
cache_to = 'cache/photos/thumbnails/'
# myapp/models.py

View file

@ -1,6 +1,8 @@
# Imagekit options
from imagekit import processors
from imagekit.specs import ImageSpec
import os
import os.path
class Options(object):
@ -26,13 +28,22 @@ class Options(object):
"""
def default_cache_to(self, instance, path, specname, extension):
"""Determines the filename to use for the transformed image. Can be
overridden on a per-spec basis by setting the cache_to property on the
spec, or on a per-model basis by defining default_cache_to on your own
IKOptions class.
"""
filepath, basename = os.path.split(path)
filename = os.path.splitext(basename)[0]
new_name = '{0}_{1}.{2}'.format(filename, specname, extension)
return os.path.join(os.path.join('cache', filepath), new_name)
crop_horz_field = 'crop_horz'
crop_vert_field = 'crop_vert'
preprocessor_spec = None
cache_dir = 'cache'
save_count_as = None
cache_filename_fields = ['pk', ]
cache_filename_format = "%(filename)s_%(specname)s.%(extension)s"
specs = None
def __init__(self, opts):

View file

@ -6,11 +6,13 @@ spec found.
"""
import os
import datetime
from StringIO import StringIO
from imagekit import processors
from imagekit.lib import *
from imagekit.utils import img_to_fobj
from django.core.files.base import ContentFile
from django.utils.encoding import force_unicode, smart_str
class ImageSpec(object):
@ -91,29 +93,27 @@ class Accessor(object):
@property
def name(self):
if self._imgfield.name:
filepath, basename = os.path.split(self._imgfield.name)
filename, extension = os.path.splitext(basename)
for processor in self.spec.processors:
if isinstance(processor, processors.Format):
extension = processor.extension
filename_format_dict = {'filename': filename,
'specname': self.property_name,
'extension': extension.lstrip('.')}
cache_filename_fields = self._obj._ik.cache_filename_fields
filename_format_dict.update(dict(zip(
cache_filename_fields,
[getattr(self._obj, field) for
field in cache_filename_fields])))
cache_filename = self._obj._ik.cache_filename_format % \
filename_format_dict
filename = self._imgfield.name
if filename:
cache_to = getattr(self.spec, 'cache_to', None) or \
getattr(self._obj._ik, 'default_cache_to', None)
if callable(self._obj._ik.cache_dir):
return self._obj._ik.cache_dir(self._obj, filepath,
cache_filename)
if not cache_to:
raise Exception('No cache_to or default_cache_to value specified')
if callable(cache_to):
extension = os.path.splitext(filename)[1]
for processor in self.spec.processors:
if isinstance(processor, processors.Format):
extension = processor.extension
new_filename = force_unicode(datetime.datetime.now().strftime( \
smart_str(cache_to(self._obj, self._imgfield.name, \
self.property_name, extension.lstrip('.')))))
else:
return os.path.join(self._obj._ik.cache_dir, filepath,
cache_filename)
dir_name = os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(cache_to))))
filename = os.path.normpath(os.path.basename(filename))
new_filename = os.path.join(dir_name, filename)
return new_filename
@property
def _storage(self):