<s> from django import forms <EOL> from django . forms . util import flatatt <EOL> from django . template . loader import render_to_string <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ <EOL> from transifex . languages . models import Language <EOL> from transifex . storage . models import StorageFile <EOL> from transifex . txcommon . log import logger <EOL> from transifex . txcommon . utils import get_url_pattern <EOL> class StorageFileWidget ( forms . MultiWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> attrs = kwargs . pop ( '<STR_LIT>' , { } ) <EOL> language = kwargs . pop ( '<STR_LIT>' , { } ) <EOL> display_language = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if not display_language : <EOL> language_field = forms . HiddenInput ( attrs = attrs ) <EOL> else : <EOL> self . language_choices = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> if not self . language_choices : <EOL> queryset = Language . objects . all ( ) <EOL> self . language_choices = [ ( l . code , l ) for l in queryset ] <EOL> self . language_choices . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> language_field = forms . Select ( attrs = attrs , <EOL> choices = self . language_choices ) <EOL> widgets = ( <EOL> language_field , <EOL> forms . HiddenInput ( attrs = attrs ) <EOL> ) <EOL> super ( StorageFileWidget , self ) . __init__ ( widgets , attrs ) <EOL> def render ( self , name , value , attrs ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> value = self . initial <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> self . html_id = final_attrs . pop ( '<STR_LIT:id>' , name ) <EOL> if not isinstance ( value , list ) : <EOL> value = self . decompress ( value ) <EOL> language , storagefile = None , None <EOL> if value : <EOL> try : <EOL> language = Language . objects . by_code_or_alias ( value [ <NUM_LIT:0> ] ) <EOL> except Exception , e : <EOL> pass <EOL> try : <EOL> storagefile = StorageFile . objects . get ( id = int ( value [ <NUM_LIT:1> ] ) ) <EOL> except Exception , e : <EOL> pass <EOL> rendered_fields = [ ] <EOL> rendered_fields . append ( self . widgets [ <NUM_LIT:0> ] . render ( name + '<STR_LIT>' , <EOL> getattr ( language , '<STR_LIT:code>' , None ) , final_attrs ) ) <EOL> rendered_fields . append ( self . widgets [ <NUM_LIT:1> ] . render ( name + '<STR_LIT>' , <EOL> getattr ( storagefile , '<STR_LIT:id>' , None ) , final_attrs ) ) <EOL> context = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : [ name + '<STR_LIT>' % n for n , w in enumerate ( self . widgets ) ] , <EOL> '<STR_LIT>' : storagefile , <EOL> '<STR_LIT>' : rendered_fields , <EOL> '<STR_LIT>' : get_url_pattern ( '<STR_LIT>' ) , <EOL> } <EOL> return mark_safe ( <EOL> render_to_string ( '<STR_LIT>' , context ) <EOL> ) <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> return value <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] </s>
