<s> from itertools import chain <EOL> from urllib import urlencode <EOL> from django import forms <EOL> from django . db . models . fields import BLANK_CHOICE_DASH <EOL> from django . forms . widgets import flatatt <EOL> from django . utils . encoding import force_unicode <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ <EOL> class LinkWidget ( forms . Widget ) : <EOL> def __init__ ( self , attrs = None , choices = ( ) ) : <EOL> super ( LinkWidget , self ) . __init__ ( attrs ) <EOL> self . choices = choices <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> value = super ( LinkWidget , self ) . value_from_datadict ( data , files , name ) <EOL> self . data = data <EOL> return value <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> if not hasattr ( self , '<STR_LIT:data>' ) : <EOL> self . data = { } <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> output = [ u'<STR_LIT>' % flatatt ( final_attrs ) ] <EOL> options = self . render_options ( choices , [ value ] , name ) <EOL> if options : <EOL> output . append ( options ) <EOL> output . append ( '<STR_LIT>' ) <EOL> return mark_safe ( u'<STR_LIT:\n>' . join ( output ) ) <EOL> def render_options ( self , choices , selected_choices , name ) : <EOL> selected_choices = set ( force_unicode ( v ) for v in selected_choices ) <EOL> output = [ ] <EOL> for option_value , option_label in chain ( self . choices , choices ) : <EOL> if isinstance ( option_label , ( list , tuple ) ) : <EOL> for option in option_label : <EOL> output . append ( self . render_option ( name , selected_choices , * option ) ) <EOL> else : <EOL> output . append ( self . render_option ( name , selected_choices , option_value , option_label ) ) <EOL> return u'<STR_LIT:\n>' . join ( output ) <EOL> def render_option ( self , name , selected_choices , option_value , option_label ) : <EOL> option_value = force_unicode ( option_value ) <EOL> if option_label == BLANK_CHOICE_DASH [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> option_label = _ ( "<STR_LIT>" ) <EOL> data = self . data . copy ( ) <EOL> data [ name ] = option_value <EOL> selected = data == self . data or option_value in selected_choices <EOL> try : <EOL> url = data . urlencode ( ) <EOL> except AttributeError : <EOL> url = urlencode ( data ) <EOL> return self . option_string ( ) % { <EOL> '<STR_LIT>' : selected and '<STR_LIT>' or '<STR_LIT>' , <EOL> '<STR_LIT>' : url , <EOL> '<STR_LIT:label>' : force_unicode ( option_label ) <EOL> } <EOL> def option_string ( self ) : <EOL> return '<STR_LIT>' <EOL> class RangeWidget ( forms . MultiWidget ) : <EOL> def __init__ ( self , attrs = None ) : <EOL> widgets = ( forms . TextInput ( attrs = attrs ) , forms . TextInput ( attrs = attrs ) ) <EOL> super ( RangeWidget , self ) . __init__ ( widgets , attrs ) <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> return [ value . start , value . stop ] <EOL> return [ None , None ] <EOL> def format_output ( self , rendered_widgets ) : <EOL> return u'<STR_LIT>' . join ( rendered_widgets ) <EOL> class LookupTypeWidget ( forms . MultiWidget ) : <EOL> def decompress ( self , value ) : <EOL> if value is None : <EOL> return [ None , None ] <EOL> return value </s>
