<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from sorl . thumbnail . fields import ImageField , ImageFormField <EOL> from sorl . thumbnail . shortcuts import get_thumbnail <EOL> class ClearableImageFormField ( forms . MultiValueField ) : <EOL> def __init__ ( self , max_length = None , ** kwargs ) : <EOL> fields = ( <EOL> ImageFormField ( max_length = max_length , ** kwargs ) , <EOL> forms . BooleanField ( ) <EOL> ) <EOL> super ( ClearableImageFormField , self ) . __init__ ( fields , ** kwargs ) <EOL> def compress ( self , data_list ) : <EOL> if data_list : <EOL> if not data_list [ <NUM_LIT:0> ] and data_list [ <NUM_LIT:1> ] : <EOL> return False <EOL> return data_list [ <NUM_LIT:0> ] <EOL> class AdminImageWidget ( forms . FileInput ) : <EOL> """<STR_LIT>""" <EOL> def render ( self , name , value , attrs = None ) : <EOL> output = super ( AdminImageWidget , self ) . render ( name , value , attrs ) <EOL> if value and hasattr ( value , '<STR_LIT:url>' ) : <EOL> ext = '<STR_LIT>' <EOL> try : <EOL> aux_ext = str ( value ) . split ( '<STR_LIT:.>' ) <EOL> if aux_ext [ len ( aux_ext ) - <NUM_LIT:1> ] . lower ( ) == '<STR_LIT>' : <EOL> ext = '<STR_LIT>' <EOL> if aux_ext [ len ( aux_ext ) - <NUM_LIT:1> ] . lower ( ) == '<STR_LIT>' : <EOL> ext = '<STR_LIT>' <EOL> except : <EOL> pass <EOL> try : <EOL> mini = get_thumbnail ( value , '<STR_LIT>' , upscale = False , format = ext ) <EOL> except Exception : <EOL> pass <EOL> else : <EOL> output = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % ( mini . width , value . url , mini . url , output ) <EOL> return mark_safe ( output ) <EOL> class AdminClearWidget ( forms . CheckboxInput ) : <EOL> def render ( self , name , value , attrs = None ) : <EOL> output = super ( AdminClearWidget , self ) . render ( name , value , attrs ) <EOL> output = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % ( name , _ ( '<STR_LIT>' ) , output ) <EOL> return mark_safe ( output ) <EOL> class AdminClearableImageWidget ( forms . MultiWidget ) : <EOL> def __init__ ( self , attrs = None ) : <EOL> widgets = ( AdminImageWidget ( attrs = attrs ) , AdminClearWidget ( ) ) <EOL> super ( AdminClearableImageWidget , self ) . __init__ ( widgets , attrs ) <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> return ( value , False ) <EOL> return ( None , None ) <EOL> class AdminImageMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def formfield_for_dbfield ( self , db_field , ** kwargs ) : <EOL> if isinstance ( db_field , ImageField ) : <EOL> if not db_field . blank : <EOL> return db_field . formfield ( widget = AdminImageWidget ) <EOL> return db_field . formfield ( <EOL> form_class = ClearableImageFormField , <EOL> widget = AdminClearableImageWidget , <EOL> ) <EOL> sup = super ( AdminImageMixin , self ) <EOL> return sup . formfield_for_dbfield ( db_field , ** kwargs ) </s>
