<s> """<STR_LIT>""" <EOL> import datetime <EOL> import re <EOL> from django . forms . widgets import Widget , Select <EOL> from django . utils import datetime_safe <EOL> from django . utils . dates import MONTHS <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . formats import get_format <EOL> from django . conf import settings <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> RE_DATE = re . compile ( r'<STR_LIT>' ) <EOL> def _parse_date_fmt ( ) : <EOL> fmt = get_format ( '<STR_LIT>' ) <EOL> escaped = False <EOL> output = [ ] <EOL> for char in fmt : <EOL> if escaped : <EOL> escaped = False <EOL> elif char == '<STR_LIT:\\>' : <EOL> escaped = True <EOL> elif char in '<STR_LIT>' : <EOL> output . append ( '<STR_LIT>' ) <EOL> elif char in '<STR_LIT>' : <EOL> output . append ( '<STR_LIT>' ) <EOL> elif char in '<STR_LIT>' : <EOL> output . append ( '<STR_LIT>' ) <EOL> return output <EOL> class SelectDateWidget ( Widget ) : <EOL> """<STR_LIT>""" <EOL> none_value = ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> month_field = '<STR_LIT>' <EOL> day_field = '<STR_LIT>' <EOL> year_field = '<STR_LIT>' <EOL> def __init__ ( self , attrs = None , years = None , required = True ) : <EOL> self . attrs = attrs or { } <EOL> self . required = required <EOL> if years : <EOL> self . years = years <EOL> else : <EOL> this_year = datetime . date . today ( ) . year <EOL> self . years = range ( this_year , this_year + <NUM_LIT:10> ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> try : <EOL> year_val , month_val , day_val = value . year , value . month , value . day <EOL> except AttributeError : <EOL> year_val = month_val = day_val = None <EOL> if isinstance ( value , basestring ) : <EOL> if settings . USE_L10N : <EOL> try : <EOL> input_format = get_format ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> v = datetime . datetime . strptime ( value , input_format ) <EOL> year_val , month_val , day_val = v . year , v . month , v . day <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> match = RE_DATE . match ( value ) <EOL> if match : <EOL> year_val , month_val , day_val = [ int ( v ) for v in match . groups ( ) ] <EOL> choices = [ ( i , i ) for i in self . years ] <EOL> year_html = self . create_select ( name , self . year_field , value , year_val , choices ) <EOL> choices = MONTHS . items ( ) <EOL> month_html = self . create_select ( name , self . month_field , value , month_val , choices ) <EOL> choices = [ ( i , i ) for i in range ( <NUM_LIT:1> , <NUM_LIT:32> ) ] <EOL> day_html = self . create_select ( name , self . day_field , value , day_val , choices ) <EOL> output = [ ] <EOL> for field in _parse_date_fmt ( ) : <EOL> if field == '<STR_LIT>' : <EOL> output . append ( year_html ) <EOL> elif field == '<STR_LIT>' : <EOL> output . append ( month_html ) <EOL> elif field == '<STR_LIT>' : <EOL> output . append ( day_html ) <EOL> return mark_safe ( u'<STR_LIT:\n>' . join ( output ) ) <EOL> def id_for_label ( self , id_ ) : <EOL> first_select = None <EOL> field_list = _parse_date_fmt ( ) <EOL> if field_list : <EOL> first_select = field_list [ <NUM_LIT:0> ] <EOL> if first_select is not None : <EOL> return '<STR_LIT>' % ( id_ , first_select ) <EOL> else : <EOL> return '<STR_LIT>' % id_ <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> y = data . get ( self . year_field % name ) <EOL> m = data . get ( self . month_field % name ) <EOL> d = data . get ( self . day_field % name ) <EOL> if y == m == d == "<STR_LIT:0>" : <EOL> return None <EOL> if y and m and d : <EOL> if settings . USE_L10N : <EOL> input_format = get_format ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> try : <EOL> date_value = datetime . date ( int ( y ) , int ( m ) , int ( d ) ) <EOL> except ValueError : <EOL> return '<STR_LIT>' % ( y , m , d ) <EOL> else : <EOL> date_value = datetime_safe . new_date ( date_value ) <EOL> return date_value . strftime ( input_format ) <EOL> else : <EOL> return '<STR_LIT>' % ( y , m , d ) <EOL> return data . get ( name , None ) <EOL> def create_select ( self , name , field , value , val , choices ) : <EOL> if '<STR_LIT:id>' in self . attrs : <EOL> id_ = self . attrs [ '<STR_LIT:id>' ] <EOL> else : <EOL> id_ = '<STR_LIT>' % name <EOL> if not ( self . required and val ) : <EOL> choices . insert ( <NUM_LIT:0> , self . none_value ) <EOL> local_attrs = self . build_attrs ( id = field % id_ ) <EOL> s = Select ( choices = choices ) <EOL> select_html = s . render ( field % name , val , local_attrs ) <EOL> return select_html <EOL> def _has_changed ( self , initial , data ) : <EOL> try : <EOL> input_format = get_format ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> data = datetime_safe . datetime . strptime ( data , input_format ) . date ( ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> return super ( SelectDateWidget , self ) . _has_changed ( initial , data ) </s>
