<s> from django import forms <EOL> from django . forms . fields import MultiValueField , CharField <EOL> from django . forms . utils import flatatt <EOL> from django . forms . widgets import CheckboxInput , HiddenInput , Input , RadioSelect , RadioFieldRenderer , RadioInput , TextInput , MultiWidget <EOL> from django . utils . encoding import force_unicode <EOL> from django . utils . html import conditional_escape <EOL> from django . utils . safestring import mark_safe <EOL> import json <EOL> from django . utils . translation import ugettext_noop <EOL> from dimagi . utils . dates import DateSpan <EOL> class BootstrapCheckboxInput ( CheckboxInput ) : <EOL> def __init__ ( self , attrs = None , check_test = bool , inline_label = "<STR_LIT>" ) : <EOL> super ( BootstrapCheckboxInput , self ) . __init__ ( attrs , check_test ) <EOL> self . inline_label = inline_label <EOL> def render ( self , name , value , attrs = None ) : <EOL> final_attrs = self . build_attrs ( attrs , type = '<STR_LIT>' , name = name ) <EOL> try : <EOL> result = self . check_test ( value ) <EOL> except : <EOL> result = False <EOL> if result : <EOL> final_attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if value not in ( '<STR_LIT>' , True , False , None ) : <EOL> final_attrs [ '<STR_LIT:value>' ] = force_unicode ( value ) <EOL> return mark_safe ( u'<STR_LIT>' % <EOL> ( flatatt ( final_attrs ) , self . inline_label ) ) <EOL> class BootstrapRadioInput ( RadioInput ) : <EOL> def __unicode__ ( self ) : <EOL> if '<STR_LIT:id>' in self . attrs : <EOL> label_for = '<STR_LIT>' % ( self . attrs [ '<STR_LIT:id>' ] , self . index ) <EOL> else : <EOL> label_for = '<STR_LIT>' <EOL> choice_label = conditional_escape ( force_unicode ( self . choice_label ) ) <EOL> return mark_safe ( u'<STR_LIT>' % ( label_for , self . tag ( ) , choice_label ) ) <EOL> class BootstrapRadioFieldRenderer ( RadioFieldRenderer ) : <EOL> def render ( self ) : <EOL> return mark_safe ( u'<STR_LIT:\n>' . join ( [ u'<STR_LIT:%s>' <EOL> % force_unicode ( w ) for w in self ] ) ) <EOL> def __iter__ ( self ) : <EOL> for i , choice in enumerate ( self . choices ) : <EOL> yield BootstrapRadioInput ( self . name , self . value , self . attrs . copy ( ) , choice , i ) <EOL> class BootstrapRadioSelect ( RadioSelect ) : <EOL> renderer = BootstrapRadioFieldRenderer <EOL> class BootstrapAddressField ( MultiValueField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , num_lines = <NUM_LIT:3> , * args , ** kwargs ) : <EOL> fields = tuple ( [ CharField ( widget = TextInput ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) ) for _ in range ( <NUM_LIT:0> , num_lines ) ] ) <EOL> self . widget = BootstrapAddressFieldWidget ( widgets = [ field . widget for field in fields ] ) <EOL> super ( BootstrapAddressField , self ) . __init__ ( fields = fields , * args , ** kwargs ) <EOL> def compress ( self , data_list ) : <EOL> return data_list <EOL> class BootstrapAddressFieldWidget ( MultiWidget ) : <EOL> def decompress ( self , value ) : <EOL> return [ '<STR_LIT>' ] * len ( self . widgets ) <EOL> def format_output ( self , rendered_widgets ) : <EOL> lines = list ( ) <EOL> for field in rendered_widgets : <EOL> lines . append ( "<STR_LIT>" % field ) <EOL> return u'<STR_LIT:\n>' . join ( lines ) <EOL> class BootstrapDisabledInput ( Input ) : <EOL> input_type = '<STR_LIT>' <EOL> def render ( self , name , value , attrs = None ) : <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> final_attrs = self . build_attrs ( attrs , type = self . input_type , name = name ) <EOL> if value != '<STR_LIT>' : <EOL> final_attrs [ '<STR_LIT:value>' ] = force_unicode ( self . _format_value ( value ) ) <EOL> return mark_safe ( u'<STR_LIT>' % <EOL> ( attrs . get ( '<STR_LIT:class>' , '<STR_LIT>' ) , value , flatatt ( final_attrs ) ) ) <EOL> class BootstrapPhoneNumberInput ( Input ) : <EOL> input_type = '<STR_LIT:text>' <EOL> def render ( self , name , value , attrs = None ) : <EOL> return mark_safe ( u"""<STR_LIT>""" % super ( BootstrapPhoneNumberInput , self ) . render ( name , value , attrs ) ) <EOL> class AutocompleteTextarea ( forms . Textarea ) : <EOL> """<STR_LIT>""" <EOL> def render ( self , name , value , attrs = None ) : <EOL> if hasattr ( self , '<STR_LIT>' ) and self . choices : <EOL> output = mark_safe ( """<STR_LIT>""" % ( attrs [ '<STR_LIT:id>' ] , json . dumps ( map ( lambda c : { '<STR_LIT:text>' : c , '<STR_LIT:id>' : c } , self . choices ) ) ) ) <EOL> else : <EOL> output = mark_safe ( "<STR_LIT>" ) <EOL> output += super ( AutocompleteTextarea , self ) . render ( name , value , <EOL> attrs = attrs ) <EOL> return output <EOL> class Select2MultipleChoiceWidget ( forms . SelectMultiple ) : <EOL> class Media : <EOL> css = { <EOL> '<STR_LIT:all>' : ( '<STR_LIT>' , ) <EOL> } <EOL> js = ( '<STR_LIT>' , ) <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> output = super ( Select2MultipleChoiceWidget , self ) . render ( name , value , attrs , choices ) <EOL> output += """<STR_LIT>""" % final_attrs . get ( '<STR_LIT:id>' ) <EOL> return mark_safe ( output ) <EOL> class DateRangePickerWidget ( Input ) : <EOL> """<STR_LIT>""" <EOL> class Range ( object ) : <EOL> LAST_7 = '<STR_LIT>' <EOL> LAST_MONTH = '<STR_LIT>' <EOL> LAST_30_DAYS = '<STR_LIT>' <EOL> range_labels = { <EOL> Range . LAST_7 : ugettext_noop ( '<STR_LIT>' ) , <EOL> Range . LAST_MONTH : ugettext_noop ( '<STR_LIT>' ) , <EOL> Range . LAST_30_DAYS : ugettext_noop ( '<STR_LIT>' ) , <EOL> } <EOL> separator = ugettext_noop ( '<STR_LIT>' ) <EOL> def __init__ ( self , attrs = None , range_labels = None , separator = None , <EOL> default_datespan = None ) : <EOL> self . range_labels = range_labels or self . range_labels <EOL> self . separator = separator or self . separator <EOL> self . default_datespan = default_datespan <EOL> super ( DateRangePickerWidget , self ) . __init__ ( attrs = attrs ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> output = super ( DateRangePickerWidget , self ) . render ( name , value , attrs ) <EOL> output += """<STR_LIT>""" % { <EOL> '<STR_LIT>' : final_attrs . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT>' : self . separator , <EOL> '<STR_LIT>' : json . dumps ( self . range_labels ) , <EOL> '<STR_LIT>' : ( self . default_datespan . startdate . strftime ( '<STR_LIT>' ) <EOL> if ( isinstance ( self . default_datespan , DateSpan ) <EOL> and self . default_datespan . startdate is not None ) <EOL> else '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( self . default_datespan . enddate . strftime ( '<STR_LIT>' ) <EOL> if ( isinstance ( self . default_datespan , DateSpan ) <EOL> and self . default_datespan . enddate is not None ) <EOL> else '<STR_LIT>' ) , <EOL> } <EOL> output = """<STR_LIT>""" + output <EOL> output = '<STR_LIT>' . format ( output ) <EOL> return mark_safe ( output ) </s>
