<s> """<STR_LIT>""" <EOL> import os <EOL> import cStringIO <EOL> from PIL import Image <EOL> from django . db . models import ImageField <EOL> from django . db . models . fields . files import ImageFieldFile <EOL> from django . conf import settings <EOL> from django . core . cache import cache <EOL> from django . core . files . base import ContentFile <EOL> from athumb . exceptions import UploadedImageIsUnreadableError <EOL> from athumb . pial . engines . pil_engine import PILEngine <EOL> from validators import ImageUploadExtensionValidator <EOL> try : <EOL> from south . modelsinspector import add_introspection_rules <EOL> add_introspection_rules ( [ ] , [ "<STR_LIT>" ] ) <EOL> except ImportError : <EOL> pass <EOL> THUMBNAIL_ENGINE = PILEngine ( ) <EOL> THUMBNAIL_URL_CACHE_TIME = getattr ( settings , '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> ) <EOL> MEDIA_CACHE_BUSTER = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> IMAGE_EXTENSION_VALIDATOR = ImageUploadExtensionValidator ( ) <EOL> class ImageWithThumbsFieldFile ( ImageFieldFile ) : <EOL> """<STR_LIT>""" <EOL> def generate_url ( self , thumb_name , ssl_mode = False , check_cache = True , cache_bust = True ) : <EOL> ssl_postfix = '<STR_LIT>' if ssl_mode else '<STR_LIT>' <EOL> cache_key = None <EOL> if check_cache : <EOL> cache_key = "<STR_LIT>" % ( self . url , <EOL> thumb_name , <EOL> ssl_postfix ) <EOL> cache_key = cache_key . strip ( ) <EOL> cached_val = cache . get ( cache_key ) <EOL> if cached_val : <EOL> return cached_val <EOL> new_filename = self . _calc_thumb_filename ( thumb_name ) <EOL> url_get_split = self . url . rsplit ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> url_str = url_get_split [ <NUM_LIT:0> ] <EOL> url_minus_filename = url_str . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> new_url = "<STR_LIT>" % ( url_minus_filename , <EOL> os . path . basename ( new_filename ) ) <EOL> if cache_bust and MEDIA_CACHE_BUSTER : <EOL> new_url = "<STR_LIT>" % ( new_url , MEDIA_CACHE_BUSTER ) <EOL> if ssl_mode : <EOL> new_url = new_url . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if cache_key : <EOL> cache . set ( cache_key , new_url , THUMBNAIL_URL_CACHE_TIME ) <EOL> return new_url <EOL> def get_thumbnail_format ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . field . thumbnail_format : <EOL> return self . field . thumbnail_format . lower ( ) <EOL> else : <EOL> filename_split = self . name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> return filename_split [ - <NUM_LIT:1> ] <EOL> def save ( self , name , content , save = True ) : <EOL> """<STR_LIT>""" <EOL> super ( ImageWithThumbsFieldFile , self ) . save ( name , content , save ) <EOL> try : <EOL> self . generate_thumbs ( name , content ) <EOL> except IOError , exc : <EOL> if '<STR_LIT>' in exc . message or '<STR_LIT>' in exc . message : <EOL> raise UploadedImageIsUnreadableError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> raise <EOL> def generate_thumbs ( self , name , content ) : <EOL> content . seek ( <NUM_LIT:0> ) <EOL> image = Image . open ( content ) <EOL> if image . mode not in ( '<STR_LIT:L>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> image = image . convert ( '<STR_LIT>' ) <EOL> for thumb in self . field . thumbs : <EOL> thumb_name , thumb_options = thumb <EOL> self . create_and_store_thumb ( image , thumb_name , thumb_options ) <EOL> def _calc_thumb_filename ( self , thumb_name ) : <EOL> """<STR_LIT>""" <EOL> filename_split = self . name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> file_name = filename_split [ <NUM_LIT:0> ] <EOL> file_extension = self . get_thumbnail_format ( ) <EOL> return '<STR_LIT>' % ( file_name , thumb_name , file_extension ) <EOL> def create_and_store_thumb ( self , image , thumb_name , thumb_options ) : <EOL> """<STR_LIT>""" <EOL> size = thumb_options [ '<STR_LIT:size>' ] <EOL> upscale = thumb_options . get ( '<STR_LIT>' , True ) <EOL> crop = thumb_options . get ( '<STR_LIT>' ) <EOL> if crop is True : <EOL> crop = '<STR_LIT>' <EOL> thumb_filename = self . _calc_thumb_filename ( thumb_name ) <EOL> file_extension = self . get_thumbnail_format ( ) <EOL> thumbed_image = THUMBNAIL_ENGINE . create_thumbnail ( <EOL> image , <EOL> size , <EOL> crop = crop , <EOL> upscale = upscale <EOL> ) <EOL> img_fobj = cStringIO . StringIO ( ) <EOL> THUMBNAIL_ENGINE . write ( thumbed_image , img_fobj , format = file_extension ) <EOL> thumb_content = ContentFile ( img_fobj . getvalue ( ) ) <EOL> self . storage . save ( thumb_filename , thumb_content ) <EOL> img_fobj . close ( ) <EOL> def delete ( self , save = True ) : <EOL> """<STR_LIT>""" <EOL> for thumb in self . field . thumbs : <EOL> thumb_name , thumb_options = thumb <EOL> thumb_filename = self . _calc_thumb_filename ( thumb_name ) <EOL> self . storage . delete ( thumb_filename ) <EOL> super ( ImageWithThumbsFieldFile , self ) . delete ( save ) <EOL> class ImageWithThumbsField ( ImageField ) : <EOL> """<STR_LIT>""" <EOL> attr_class = ImageWithThumbsFieldFile <EOL> default_validators = [ IMAGE_EXTENSION_VALIDATOR ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . thumbs = kwargs . pop ( '<STR_LIT>' , ( ) ) <EOL> self . thumbnail_format = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if '<STR_LIT:max_length>' not in kwargs : <EOL> kwargs [ '<STR_LIT:max_length>' ] = <NUM_LIT:255> <EOL> super ( ImageWithThumbsField , self ) . __init__ ( * args , ** kwargs ) <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( ImageWithThumbsField , self ) . deconstruct ( ) <EOL> if self . thumbs : <EOL> kwargs [ '<STR_LIT>' ] = self . thumbs <EOL> if self . thumbnail_format : <EOL> kwargs [ '<STR_LIT>' ] = self . thumbnail_format <EOL> if self . validators == [ IMAGE_EXTENSION_VALIDATOR ] and '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> return name , path , args , kwargs </s>
