<s> from django . contrib . admin . widgets import AdminFileWidget <EOL> from django import forms <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . safestring import mark_safe <EOL> from django . conf import settings <EOL> from PIL import Image <EOL> import os <EOL> try : <EOL> from sorl . thumbnail . main import DjangoThumbnail <EOL> def thumbnail ( image_path ) : <EOL> t = DjangoThumbnail ( relative_source = image_path , requested_size = ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> return u'<STR_LIT>' % ( t . absolute_url , image_path ) <EOL> except ImportError : <EOL> def thumbnail ( image_path ) : <EOL> absolute_url = os . path . join ( settings . MEDIA_ROOT , image_path ) <EOL> return u'<STR_LIT>' % ( absolute_url , image_path ) <EOL> class AdminImageWidget ( AdminFileWidget ) : <EOL> """<STR_LIT>""" <EOL> def render ( self , name , value , attrs = None ) : <EOL> output = [ ] <EOL> file_name = str ( value ) <EOL> if file_name : <EOL> file_path = '<STR_LIT>' % ( settings . MEDIA_URL , file_name ) <EOL> try : <EOL> Image . open ( os . path . join ( settings . MEDIA_ROOT , file_name ) ) <EOL> output . append ( '<STR_LIT>' % ( file_path , thumbnail ( file_name ) , _ ( '<STR_LIT>' ) , file_path , file_name , _ ( '<STR_LIT>' ) ) ) <EOL> except IOError : <EOL> output . append ( '<STR_LIT>' % ( _ ( '<STR_LIT>' ) , file_path , file_name , _ ( '<STR_LIT>' ) ) ) <EOL> output . append ( super ( AdminFileWidget , self ) . render ( name , value , attrs ) ) <EOL> return mark_safe ( u'<STR_LIT>' . join ( output ) ) </s>
