<s> import re <EOL> from itertools import chain <EOL> from django import forms <EOL> from django . forms import widgets <EOL> from django . forms . extras . widgets import SelectDateWidget <EOL> from django . utils . html import escape , conditional_escape <EOL> from django . utils . encoding import force_unicode <EOL> from django . utils . safestring import mark_safe <EOL> time_pattern = r'<STR_LIT>' <EOL> RE_TIME = re . compile ( time_pattern ) <EOL> HOURS = <NUM_LIT:0> <EOL> MINUTES = <NUM_LIT:1> <EOL> SECONDS = <NUM_LIT:3> <EOL> MERIDIEM = <NUM_LIT:4> <EOL> class SelectTimeWidget ( widgets . Widget ) : <EOL> """<STR_LIT>""" <EOL> hour_field = '<STR_LIT>' <EOL> minute_field = '<STR_LIT>' <EOL> second_field = '<STR_LIT>' <EOL> meridiem_field = '<STR_LIT>' <EOL> twelve_hr = False <EOL> def __init__ ( self , attrs = None , hour_step = None , minute_step = None , <EOL> second_step = None , twelve_hr = False ) : <EOL> '''<STR_LIT>''' <EOL> self . attrs = attrs or { } <EOL> if twelve_hr : <EOL> self . twelve_hr = True <EOL> self . meridiem_val = '<STR_LIT>' <EOL> if hour_step and twelve_hr : <EOL> self . hours = range ( <NUM_LIT:1> , <NUM_LIT> , hour_step ) <EOL> elif hour_step : <EOL> self . hours = range ( <NUM_LIT:0> , <NUM_LIT> , hour_step ) <EOL> elif twelve_hr : <EOL> self . hours = range ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> else : <EOL> self . hours = range ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> if minute_step : <EOL> self . minutes = range ( <NUM_LIT:0> , <NUM_LIT> , minute_step ) <EOL> else : <EOL> self . minutes = range ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> if second_step : <EOL> self . seconds = range ( <NUM_LIT:0> , <NUM_LIT> , second_step ) <EOL> else : <EOL> self . seconds = range ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> try : <EOL> hour_val , minute_val , second_val = value . hour , value . minute , value . second <EOL> if self . twelve_hr : <EOL> if hour_val >= <NUM_LIT:12> : <EOL> self . meridiem_val = '<STR_LIT>' <EOL> else : <EOL> self . meridiem_val = '<STR_LIT>' <EOL> except AttributeError : <EOL> hour_val = minute_val = second_val = <NUM_LIT:0> <EOL> if isinstance ( value , basestring ) : <EOL> match = RE_TIME . match ( value ) <EOL> if match : <EOL> time_groups = match . groups ( ) <EOL> hour_val = int ( time_groups [ HOURS ] ) % <NUM_LIT> <EOL> minute_val = int ( time_groups [ MINUTES ] ) <EOL> if time_groups [ SECONDS ] is None : <EOL> second_val = <NUM_LIT:0> <EOL> else : <EOL> second_val = int ( time_groups [ SECONDS ] ) <EOL> if time_groups [ MERIDIEM ] is not None : <EOL> self . meridiem_val = time_groups [ MERIDIEM ] <EOL> else : <EOL> if self . twelve_hr : <EOL> if hour_val >= <NUM_LIT:12> : <EOL> self . meridiem_val = '<STR_LIT>' <EOL> else : <EOL> self . meridiem_val = '<STR_LIT>' <EOL> else : <EOL> self . meridiem_val = None <EOL> if self . twelve_hr and self . meridiem_val : <EOL> if self . meridiem_val . lower ( ) . startswith ( '<STR_LIT:p>' ) and hour_val > <NUM_LIT:12> and hour_val < <NUM_LIT> : <EOL> hour_val = hour_val % <NUM_LIT:12> <EOL> elif hour_val == <NUM_LIT:0> : <EOL> hour_val = <NUM_LIT:12> <EOL> output = [ ] <EOL> if '<STR_LIT:id>' in self . attrs : <EOL> id_ = self . attrs [ '<STR_LIT:id>' ] <EOL> else : <EOL> id_ = '<STR_LIT>' % name <EOL> hour_val = u"<STR_LIT>" % hour_val <EOL> minute_val = u"<STR_LIT>" % minute_val <EOL> second_val = u"<STR_LIT>" % second_val <EOL> hour_choices = [ ( "<STR_LIT>" % i , "<STR_LIT>" % i ) for i in self . hours ] <EOL> local_attrs = self . build_attrs ( id = self . hour_field % id_ ) <EOL> select_html = widgets . Select ( choices = hour_choices ) . render ( <EOL> self . hour_field % name , hour_val , local_attrs ) <EOL> output . append ( select_html ) <EOL> minute_choices = [ ( "<STR_LIT>" % i , "<STR_LIT>" % i ) for i in self . minutes ] <EOL> local_attrs [ '<STR_LIT:id>' ] = self . minute_field % id_ <EOL> select_html = widgets . Select ( choices = minute_choices ) . render ( <EOL> self . minute_field % name , minute_val , local_attrs ) <EOL> output . append ( select_html ) <EOL> second_choices = [ ( "<STR_LIT>" % i , "<STR_LIT>" % i ) for i in self . seconds ] <EOL> local_attrs [ '<STR_LIT:id>' ] = self . second_field % id_ <EOL> select_html = widgets . Select ( choices = second_choices ) . render ( <EOL> self . second_field % name , second_val , local_attrs ) <EOL> output . append ( select_html ) <EOL> if self . twelve_hr : <EOL> if self . meridiem_val is not None and self . meridiem_val . startswith ( '<STR_LIT:p>' ) : <EOL> meridiem_choices = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> else : <EOL> meridiem_choices = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> local_attrs [ '<STR_LIT:id>' ] = local_attrs [ '<STR_LIT:id>' ] = self . meridiem_field % id_ <EOL> select_html = Select ( choices = meridiem_choices ) . render ( <EOL> self . meridiem_field % name , self . meridiem_val , local_attrs ) <EOL> output . append ( select_html ) <EOL> return mark_safe ( u'<STR_LIT:\n>' . join ( output ) ) <EOL> def id_for_label ( self , id_ ) : <EOL> return '<STR_LIT>' % id_ <EOL> id_for_label = classmethod ( id_for_label ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> h = data . get ( self . hour_field % name , <NUM_LIT:0> ) <EOL> m = data . get ( self . minute_field % name , <NUM_LIT:0> ) <EOL> s = data . get ( self . second_field % name , <NUM_LIT:0> ) <EOL> meridiem = data . get ( self . meridiem_field % name , None ) <EOL> if meridiem is not None : <EOL> if meridiem . lower ( ) . startswith ( '<STR_LIT:p>' ) and int ( h ) != <NUM_LIT:12> : <EOL> h = ( int ( h ) + <NUM_LIT:12> ) % <NUM_LIT> <EOL> elif meridiem . lower ( ) . startswith ( '<STR_LIT:a>' ) and int ( h ) == <NUM_LIT:12> : <EOL> h = <NUM_LIT:0> <EOL> if ( int ( h ) == <NUM_LIT:0> or h ) and m and s : <EOL> return '<STR_LIT>' % ( h , m , s ) <EOL> return data . get ( name , None ) <EOL> class SplitSelectDateTimeWidget ( widgets . MultiWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None , hour_step = None , minute_step = None , second_step = None , twelve_hr = None , years = None ) : <EOL> """<STR_LIT>""" <EOL> widgets = ( SelectDateWidget ( attrs = attrs , years = years ) , SelectTimeWidget ( attrs = attrs , hour_step = hour_step , minute_step = minute_step , second_step = second_step , twelve_hr = twelve_hr ) ) <EOL> super ( SplitSelectDateTimeWidget , self ) . __init__ ( widgets , attrs ) <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> return [ value . date ( ) , value . time ( ) . replace ( microsecond = <NUM_LIT:0> ) ] <EOL> return [ None , None ] <EOL> def format_output ( self , rendered_widgets ) : <EOL> """<STR_LIT>""" <EOL> rendered_widgets . insert ( - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> return u'<STR_LIT>' . join ( rendered_widgets ) <EOL> class ReadOnlyWidget ( forms . Widget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> super ( ReadOnlyWidget , self ) . __init__ ( ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> return unicode ( self . value ) <EOL> class SelectWithDisabledOptions ( forms . Select ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None , choices = ( ) , disabled_choices = ( ) ) : <EOL> super ( SelectWithDisabledOptions , self ) . __init__ ( attrs ) <EOL> self . choices = list ( choices ) <EOL> self . disabled_choices = list ( disabled_choices ) <EOL> def render_option ( self , selected_choices , option_value , option_label ) : <EOL> option_value = force_unicode ( option_value ) <EOL> selected_html = ( option_value in selected_choices ) and u'<STR_LIT>' or '<STR_LIT>' <EOL> disabled_html = ( int ( option_value ) in self . disabled_choices ) and u'<STR_LIT>' or '<STR_LIT>' <EOL> return u'<STR_LIT>' % ( <EOL> escape ( option_value ) , selected_html + disabled_html , <EOL> conditional_escape ( force_unicode ( option_label ) ) ) </s>
