2011-09-21 15:37:29 +00:00
import os
import datetime
2011-10-20 03:12:47 +00:00
2008-12-28 21:48:21 +00:00
from django . db import models
2011-09-22 04:24:13 +00:00
from django . db . models . fields . files import ImageFieldFile
2012-02-11 05:04:56 +00:00
from django . db . models . signals import post_init , post_save , post_delete
2011-10-20 03:12:47 +00:00
from django . utils . encoding import force_unicode , smart_str
2009-01-04 18:40:22 +00:00
2012-02-12 04:27:25 +00:00
from . . imagecache import get_default_image_cache_backend
2012-02-12 04:48:54 +00:00
from . . generators import SpecFileGenerator
2012-02-12 01:11:56 +00:00
2009-12-19 16:01:54 +00:00
2012-02-02 04:26:39 +00:00
class BoundImageKitMeta ( object ) :
def __init__ ( self , instance , spec_fields ) :
self . instance = instance
self . spec_fields = spec_fields
@property
def spec_files ( self ) :
return [ getattr ( self . instance , n ) for n in self . spec_fields ]
class ImageKitMeta ( object ) :
def __init__ ( self , spec_fields = None ) :
self . spec_fields = spec_fields or [ ]
def __get__ ( self , instance , owner ) :
if instance is None :
return self
else :
ik = BoundImageKitMeta ( instance , self . spec_fields )
setattr ( instance , ' _ik ' , ik )
return ik
2012-02-12 01:11:56 +00:00
class ImageSpecField ( object ) :
2011-10-31 14:12:03 +00:00
"""
2012-02-11 18:06:48 +00:00
The heart and soul of the ImageKit library , ImageSpecField allows you to add
2011-09-26 00:38:43 +00:00
variants of uploaded images to your models .
2009-12-19 16:01:54 +00:00
2009-03-03 16:44:51 +00:00
"""
2011-11-16 15:00:35 +00:00
def __init__ ( self , processors = None , format = None , options = { } ,
2012-02-02 03:37:58 +00:00
image_field = None , pre_cache = None , storage = None , cache_to = None ,
2012-02-12 21:18:34 +00:00
autoconvert = True , image_cache_backend = None ) :
2011-09-26 00:38:43 +00:00
"""
: param processors : A list of processors to run on the original image .
2011-10-31 14:12:03 +00:00
: param format : The format of the output file . If not provided ,
2012-02-11 18:06:48 +00:00
ImageSpecField will try to guess the appropriate format based on the
2011-10-31 14:12:03 +00:00
extension of the filename and the format of the input image .
2011-11-16 15:00:35 +00:00
: param options : A dictionary that will be passed to PIL ' s
` ` Image . save ( ) ` ` method as keyword arguments . Valid options vary
between formats , but some examples include ` ` quality ` ` ,
` ` optimize ` ` , and ` ` progressive ` ` for JPEGs . See the PIL
documentation for others .
2011-09-26 00:38:43 +00:00
: param image_field : The name of the model property that contains the
2011-10-31 14:12:03 +00:00
original image .
2011-09-26 00:38:43 +00:00
: param storage : A Django storage system to use to save the generated
2011-10-31 14:12:03 +00:00
image .
2011-09-26 00:38:43 +00:00
: param cache_to : Specifies the filename to use when saving the image
2011-10-31 14:12:03 +00:00
cache file . This is modeled after ImageField ' s ``upload_to`` and
can be either a string ( that specifies a directory ) or a
callable ( that returns a filepath ) . Callable values should
accept the following arguments :
- instance - - The model instance this spec belongs to
- path - - The path of the original image
- specname - - the property name that the spec is bound to on
the model instance
- extension - - A recommended extension . If the format of the
spec is set explicitly , this suggestion will be
based on that format . if not , the extension of the
original file will be passed . You do not have to use
this extension , it ' s only a recommendation.
2011-11-07 02:40:31 +00:00
: param autoconvert : Specifies whether the AutoConvert processor
should be run before saving .
2012-02-12 21:18:34 +00:00
: param image_cache_backend : An object responsible for managing the state
2012-02-02 03:37:58 +00:00
of cached files . Defaults to an instance of
2012-02-12 21:18:34 +00:00
IMAGEKIT_DEFAULT_IMAGE_CACHE_BACKEND
2011-09-26 00:38:43 +00:00
"""
2011-09-21 15:37:29 +00:00
2012-02-02 03:37:58 +00:00
if pre_cache is not None :
raise Exception ( ' The pre_cache argument has been removed in favor '
' of cache state backends. ' )
2012-02-12 04:22:06 +00:00
# The generator accepts a callable value for processors, but it
# takes different arguments than the callable that ImageSpecField
# expects, so we create a partial application and pass that instead.
# TODO: Should we change the signatures to match? Even if `instance` is not part of the signature, it's accessible through the source file object's instance property.
p = lambda file : processors ( instance = file . instance , file = file ) if \
callable ( processors ) else processors
self . generator = SpecFileGenerator ( p , format = format , options = options ,
2012-02-12 04:41:07 +00:00
autoconvert = autoconvert , storage = storage )
2011-09-22 04:24:13 +00:00
self . image_field = image_field
self . storage = storage
self . cache_to = cache_to
2012-02-12 04:39:16 +00:00
self . image_cache_backend = image_cache_backend or \
get_default_image_cache_backend ( )
2011-09-21 15:37:29 +00:00
def contribute_to_class ( self , cls , name ) :
2012-02-11 18:06:48 +00:00
setattr ( cls , name , _ImageSpecFieldDescriptor ( self , name ) )
2011-10-04 02:51:03 +00:00
try :
ik = getattr ( cls , ' _ik ' )
except AttributeError :
2012-02-02 04:26:39 +00:00
ik = ImageKitMeta ( )
2011-10-04 02:51:03 +00:00
setattr ( cls , ' _ik ' , ik )
2012-02-02 04:26:39 +00:00
ik . spec_fields . append ( name )
2011-10-04 02:51:03 +00:00
2011-09-21 15:37:29 +00:00
# Connect to the signals only once for this class.
uid = ' %s . %s ' % ( cls . __module__ , cls . __name__ )
2012-02-12 22:06:37 +00:00
post_init . connect ( ImageSpecField . _post_init_receiver , sender = cls ,
2012-02-11 05:04:56 +00:00
dispatch_uid = uid )
2012-02-12 22:06:37 +00:00
post_save . connect ( ImageSpecField . _post_save_receiver , sender = cls ,
2012-02-11 05:04:56 +00:00
dispatch_uid = uid )
2012-02-12 22:06:37 +00:00
post_delete . connect ( ImageSpecField . _post_delete_receiver , sender = cls ,
2012-02-11 05:04:56 +00:00
dispatch_uid = uid )
2011-09-21 15:37:29 +00:00
2012-02-12 21:18:34 +00:00
# Register the field with the image_cache_backend
2012-02-02 03:37:58 +00:00
try :
2012-02-12 21:18:34 +00:00
self . image_cache_backend . register_field ( cls , self , name )
2012-02-02 03:37:58 +00:00
except AttributeError :
pass
2012-02-11 05:04:56 +00:00
@staticmethod
def _post_save_receiver ( sender , instance = None , created = False , raw = False , * * kwargs ) :
if not raw :
old_hashes = instance . _ik . _source_hashes . copy ( )
2012-02-12 22:06:37 +00:00
new_hashes = ImageSpecField . _update_source_hashes ( instance )
2012-02-11 05:04:56 +00:00
for attname in instance . _ik . spec_fields :
if old_hashes [ attname ] != new_hashes [ attname ] :
getattr ( instance , attname ) . invalidate ( )
@staticmethod
def _update_source_hashes ( instance ) :
"""
Stores hashes of the source image files so that they can be compared
later to see whether the source image has changed ( and therefore whether
the spec file needs to be regenerated ) .
"""
instance . _ik . _source_hashes = dict ( ( f . attname , hash ( f . source_file ) ) \
for f in instance . _ik . spec_files )
return instance . _ik . _source_hashes
@staticmethod
def _post_delete_receiver ( sender , instance = None , * * kwargs ) :
for spec_file in instance . _ik . spec_files :
spec_file . clear ( )
@staticmethod
def _post_init_receiver ( sender , instance , * * kwargs ) :
2012-02-12 22:06:37 +00:00
ImageSpecField . _update_source_hashes ( instance )
2011-09-21 15:37:29 +00:00
2012-02-12 01:11:56 +00:00
class ImageSpecFieldFile ( ImageFieldFile ) :
2011-12-02 07:28:08 +00:00
def __init__ ( self , instance , field , attname ) :
2011-10-10 21:00:41 +00:00
ImageFieldFile . __init__ ( self , instance , field , None )
2011-09-22 01:04:40 +00:00
self . attname = attname
2011-12-02 07:28:08 +00:00
self . storage = self . field . storage or self . source_file . storage
@property
def source_file ( self ) :
field_name = getattr ( self . field , ' image_field ' , None )
if field_name :
field_file = getattr ( self . instance , field_name )
else :
image_fields = [ getattr ( self . instance , f . attname ) for f in \
self . instance . __class__ . _meta . fields if \
isinstance ( f , models . ImageField ) ]
if len ( image_fields ) == 0 :
2012-01-26 23:19:39 +00:00
raise Exception ( ' %s does not define any ImageFields, so your ' \
2012-02-11 18:06:48 +00:00
' %s ImageSpecField has no image to act on. ' % \
2012-01-26 23:19:39 +00:00
( self . instance . __class__ . __name__ , self . attname ) )
2011-12-02 07:28:08 +00:00
elif len ( image_fields ) > 1 :
2012-01-26 23:19:39 +00:00
raise Exception ( ' %s defines multiple ImageFields, but you ' \
' have not specified an image_field for your %s ' \
2012-02-11 18:06:48 +00:00
' ImageSpecField. ' % ( self . instance . __class__ . __name__ ,
2011-12-02 07:28:08 +00:00
self . attname ) )
else :
field_file = image_fields [ 0 ]
return field_file
2011-09-21 15:37:29 +00:00
2011-10-10 21:00:41 +00:00
def _require_file ( self ) :
if not self . source_file :
raise ValueError ( " The ' %s ' attribute ' s image_field has no file associated with it. " % self . attname )
def _get_file ( self ) :
2012-02-02 03:37:58 +00:00
self . validate ( )
2011-10-10 21:00:41 +00:00
return super ( ImageFieldFile , self ) . file
file = property ( _get_file , ImageFieldFile . _set_file , ImageFieldFile . _del_file )
2012-02-03 04:20:09 +00:00
def clear ( self ) :
2012-02-12 04:39:16 +00:00
return self . field . image_cache_backend . clear ( self )
2011-10-10 21:00:41 +00:00
2012-02-02 03:37:58 +00:00
def invalidate ( self ) :
2012-02-12 04:39:16 +00:00
return self . field . image_cache_backend . invalidate ( self )
2011-10-20 03:06:10 +00:00
2012-02-02 03:37:58 +00:00
def validate ( self ) :
2012-02-12 04:39:16 +00:00
return self . field . image_cache_backend . validate ( self )
2011-09-21 15:37:29 +00:00
2012-02-03 14:16:50 +00:00
def generate ( self , save = True ) :
2011-09-21 15:37:29 +00:00
"""
2012-02-02 03:37:58 +00:00
Generates a new image file by processing the source file and returns
the content of the result , ready for saving .
2011-10-10 21:00:41 +00:00
2012-02-02 03:37:58 +00:00
"""
2012-02-12 01:38:17 +00:00
return self . field . generator . generate_file ( self . name , self . source_file ,
save )
2012-02-02 03:37:58 +00:00
@property
def url ( self ) :
self . validate ( )
return super ( ImageFieldFile , self ) . url
2008-12-28 21:48:21 +00:00
2011-10-10 21:00:41 +00:00
def delete ( self , save = False ) :
2011-10-31 14:12:03 +00:00
"""
Pulled almost verbatim from ` ` ImageFieldFile . delete ( ) ` ` and
2011-10-10 21:00:41 +00:00
` ` FieldFile . delete ( ) ` ` but with the attempts to reset the instance
property removed .
2009-12-19 16:01:54 +00:00
2011-10-10 21:00:41 +00:00
"""
# Clear the image dimensions cache
if hasattr ( self , ' _dimensions_cache ' ) :
del self . _dimensions_cache
2009-12-19 16:01:54 +00:00
2011-10-10 21:00:41 +00:00
# Only close the file if it's already open, which we know by the
2011-10-31 14:12:03 +00:00
# presence of self._file.
2011-10-10 21:00:41 +00:00
if hasattr ( self , ' _file ' ) :
self . close ( )
del self . file
2011-12-07 05:52:06 +00:00
if self . name and self . storage . exists ( self . name ) :
try :
self . storage . delete ( self . name )
except NotImplementedError :
pass
2011-10-10 21:00:41 +00:00
2011-10-31 14:12:03 +00:00
# Delete the filesize cache.
2011-10-10 21:00:41 +00:00
if hasattr ( self , ' _size ' ) :
del self . _size
self . _committed = False
if save :
self . instance . save ( )
2009-12-19 16:01:54 +00:00
2011-09-22 01:02:12 +00:00
def _default_cache_to ( self , instance , path , specname , extension ) :
2011-10-31 14:12:03 +00:00
"""
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 .
2011-09-22 01:02:12 +00:00
"""
filepath , basename = os . path . split ( path )
filename = os . path . splitext ( basename ) [ 0 ]
2012-01-26 23:19:39 +00:00
new_name = ' %s _ %s %s ' % ( filename , specname , extension )
2011-09-22 01:02:12 +00:00
return os . path . join ( os . path . join ( ' cache ' , filepath ) , new_name )
2009-12-19 16:01:54 +00:00
2009-12-18 06:26:19 +00:00
@property
2011-09-21 15:37:29 +00:00
def name ( self ) :
"""
Specifies the filename that the cached image will use . The user can
2012-02-11 18:06:48 +00:00
control this by providing a ` cache_to ` method to the ImageSpecField .
2011-09-21 15:37:29 +00:00
"""
2011-11-01 23:39:54 +00:00
name = getattr ( self , ' _name ' , None )
if not name :
filename = self . source_file . name
new_filename = None
if filename :
cache_to = self . field . cache_to or self . _default_cache_to
if not cache_to :
2012-02-12 04:27:25 +00:00
raise Exception ( ' No cache_to or default_cache_to value '
' specified ' )
2011-11-01 23:39:54 +00:00
if callable ( cache_to ) :
2012-02-12 04:27:25 +00:00
suggested_extension = \
self . field . generator . suggest_extension (
2012-02-12 01:11:56 +00:00
self . source_file . name )
2012-02-12 04:27:25 +00:00
new_filename = force_unicode (
datetime . datetime . now ( ) . strftime (
smart_str ( cache_to ( self . instance ,
self . source_file . name , self . attname ,
suggested_extension ) ) ) )
2011-11-01 23:39:54 +00:00
else :
2012-02-12 04:27:25 +00:00
dir_name = os . path . normpath (
force_unicode ( datetime . datetime . now ( ) . strftime (
smart_str ( cache_to ) ) ) )
2011-11-01 23:39:54 +00:00
filename = os . path . normpath ( os . path . basename ( filename ) )
new_filename = os . path . join ( dir_name , filename )
self . _name = new_filename
return self . _name
2011-09-21 15:37:29 +00:00
2011-10-10 21:00:41 +00:00
@name.setter
def name ( self , value ) :
2011-10-31 14:12:03 +00:00
# TODO: Figure out a better way to handle this. We really don't want
# to allow anybody to set the name, but ``File.__init__`` (which is
2012-02-11 18:06:48 +00:00
# called by ``ImageSpecFieldFile.__init__``) does, so we have to allow
# it at least that one time.
2011-10-10 21:00:41 +00:00
pass
2011-09-21 15:37:29 +00:00
2012-02-11 18:06:48 +00:00
class _ImageSpecFieldDescriptor ( object ) :
2011-09-22 22:13:32 +00:00
def __init__ ( self , field , attname ) :
2011-09-24 03:02:17 +00:00
self . attname = attname
2011-09-22 22:13:32 +00:00
self . field = field
2011-09-21 15:37:29 +00:00
def __get__ ( self , instance , owner ) :
if instance is None :
2011-09-22 22:13:32 +00:00
return self . field
2009-09-02 18:20:30 +00:00
else :
2012-02-11 18:06:48 +00:00
img_spec_file = ImageSpecFieldFile ( instance , self . field ,
self . attname )
2011-09-25 16:42:27 +00:00
setattr ( instance , self . attname , img_spec_file )
return img_spec_file
2011-09-21 15:37:29 +00:00
def _post_save_handler ( sender , instance = None , created = False , raw = False , * * kwargs ) :
if raw :
return
2011-09-22 13:20:37 +00:00
spec_files = get_spec_files ( instance )
for spec_file in spec_files :
2011-09-22 23:44:47 +00:00
if not created :
2011-10-10 21:00:41 +00:00
spec_file . delete ( save = False )
2011-09-22 23:44:47 +00:00
if spec_file . field . pre_cache :
2011-11-08 04:01:52 +00:00
spec_file . generate ( False )
2011-09-21 15:37:29 +00:00
def _post_delete_handler ( sender , instance = None , * * kwargs ) :
assert instance . _get_pk_val ( ) is not None , " %s object can ' t be deleted because its %s attribute is set to None. " % ( instance . _meta . object_name , instance . _meta . pk . attname )
2011-09-22 13:20:37 +00:00
spec_files = get_spec_files ( instance )
for spec_file in spec_files :
2011-10-10 21:00:41 +00:00
spec_file . delete ( save = False )
2011-09-21 15:37:29 +00:00
2012-02-12 01:11:56 +00:00
class ProcessedImageFieldFile ( ImageFieldFile ) :
2011-09-22 04:24:13 +00:00
def save ( self , name , content , save = True ) :
new_filename = self . field . generate_filename ( self . instance , name )
2012-02-12 04:22:06 +00:00
img , content = self . field . generator . process_content ( content ,
new_filename , self )
2011-09-22 04:24:13 +00:00
return super ( ProcessedImageFieldFile , self ) . save ( name , content , save )
2012-02-12 01:11:56 +00:00
class ProcessedImageField ( models . ImageField ) :
2011-10-31 14:12:03 +00:00
"""
ProcessedImageField is an ImageField that runs processors on the uploaded
2011-09-26 00:38:43 +00:00
image * before * saving it to storage . This is in contrast to specs , which
maintain the original . Useful for coercing fileformats or keeping images
within a reasonable size .
"""
2011-09-22 04:24:13 +00:00
attr_class = ProcessedImageFieldFile
2011-11-16 15:00:35 +00:00
def __init__ ( self , processors = None , format = None , options = { } ,
2011-09-22 04:24:13 +00:00
verbose_name = None , name = None , width_field = None , height_field = None ,
2011-11-07 02:40:31 +00:00
autoconvert = True , * * kwargs ) :
2011-09-26 00:38:43 +00:00
"""
The ProcessedImageField constructor accepts all of the arguments that
2011-10-31 14:12:03 +00:00
the : class : ` django . db . models . ImageField ` constructor accepts , as well
2011-11-16 15:00:35 +00:00
as the ` ` processors ` ` , ` ` format ` ` , and ` ` options ` ` arguments of
2012-02-12 19:53:49 +00:00
: class : ` imagekit . models . fields . ImageSpecField ` .
2011-09-26 00:38:43 +00:00
"""
2011-11-16 15:00:35 +00:00
if ' quality ' in kwargs :
raise Exception ( ' The " quality " keyword argument has been '
""" deprecated. Use `options= { ' quality ' : %s }` instead. """ \
% kwargs [ ' quality ' ] )
2011-09-22 04:24:13 +00:00
models . ImageField . __init__ ( self , verbose_name , name , width_field ,
height_field , * * kwargs )
2012-02-12 01:11:56 +00:00
self . generator = SpecFileGenerator ( processors , format = format ,
2012-02-12 01:20:11 +00:00
options = options , autoconvert = autoconvert )
2011-09-22 04:24:13 +00:00
def get_filename ( self , filename ) :
2012-02-12 04:27:25 +00:00
filename = os . path . normpath ( self . storage . get_valid_name (
os . path . basename ( filename ) ) )
2011-09-22 04:24:13 +00:00
name , ext = os . path . splitext ( filename )
2012-02-12 04:32:25 +00:00
ext = self . generator . suggest_extension ( filename )
2012-01-26 23:19:39 +00:00
return ' %s %s ' % ( name , ext )
2011-10-24 21:58:38 +00:00
try :
from south . modelsinspector import add_introspection_rules
except ImportError :
pass
else :
2012-02-12 19:53:49 +00:00
add_introspection_rules ( [ ] , [ r ' ^imagekit \ .models \ .fields \ .ProcessedImageField$ ' ] )