<s> import datetime <EOL> import pytz <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . core import validators <EOL> from django . core . exceptions import ValidationError <EOL> from django . forms . widgets import MultiWidget <EOL> from django . forms . fields import MultiValueField , DateField , TimeField <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . import zones <EOL> from . utils import adjust_datetime_to_timezone , coerce_timezone_value <EOL> from . import global_tz <EOL> class TimeZoneField ( forms . TypedChoiceField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if not "<STR_LIT>" in kwargs : <EOL> kwargs [ "<STR_LIT>" ] = zones . ALL_TIMEZONE_CHOICES <EOL> kwargs [ "<STR_LIT>" ] = coerce_timezone_value <EOL> super ( TimeZoneField , self ) . __init__ ( * args , ** kwargs ) <EOL> class TimeZoneDateTimeWidget ( MultiWidget ) : <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> if not value . tzinfo : <EOL> value = pytz . timezone ( '<STR_LIT>' ) . localize ( value ) <EOL> return [ value , value . tzinfo ] <EOL> return [ None , None ] <EOL> class TimeZoneDateTimeField ( MultiValueField ) : <EOL> """<STR_LIT>""" <EOL> widget = TimeZoneDateTimeWidget <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) <EOL> } <EOL> def __init__ ( self , input_datetime_formats = None , * args , ** kwargs ) : <EOL> errors = self . default_error_messages . copy ( ) <EOL> if '<STR_LIT>' in kwargs : <EOL> errors . update ( kwargs [ '<STR_LIT>' ] ) <EOL> self . localize = kwargs . get ( '<STR_LIT>' , True ) <EOL> fields = ( <EOL> forms . DateTimeField ( input_formats = input_datetime_formats , <EOL> error_messages = { '<STR_LIT>' : errors [ '<STR_LIT>' ] } , <EOL> localize = self . localize ) , <EOL> TimeZoneField ( error_messages = { '<STR_LIT>' : errors [ '<STR_LIT>' ] } ) <EOL> ) <EOL> dtw = kwargs . pop ( '<STR_LIT>' , fields [ <NUM_LIT:0> ] . widget ) <EOL> tzw = kwargs . pop ( '<STR_LIT>' , fields [ <NUM_LIT:1> ] . widget ) <EOL> if isinstance ( self . widget , type ) : <EOL> self . widget = self . widget ( ( dtw , tzw ) ) <EOL> super ( TimeZoneDateTimeField , self ) . __init__ ( fields , * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> if ( isinstance ( value , ( list , tuple ) ) and <EOL> ( not value or not [ v for v in value [ : - <NUM_LIT:1> ] if v not in validators . EMPTY_VALUES ] ) ) : <EOL> return super ( TimeZoneDateTimeField , self ) . clean ( [ ] ) <EOL> return super ( TimeZoneDateTimeField , self ) . clean ( value ) <EOL> def compress ( self , data_list ) : <EOL> if data_list : <EOL> if data_list [ <NUM_LIT:0> ] in validators . EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if data_list [ <NUM_LIT:1> ] in validators . EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return data_list [ <NUM_LIT:1> ] . localize ( data_list [ <NUM_LIT:0> ] ) <EOL> return None <EOL> class LocalizedDateTimeWidget ( TimeZoneDateTimeWidget ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . get_timezone = kwargs . get ( '<STR_LIT>' , <EOL> lambda value : global_tz . get_timezone ( ) ) <EOL> super ( LocalizedDateTimeWidget , self ) . __init__ ( * args , ** kwargs ) <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> if not value . tzinfo : <EOL> value = pytz . timezone ( settings . TIME_ZONE ) . localize ( value ) <EOL> tz = self . get_timezone ( value ) <EOL> value = adjust_datetime_to_timezone ( value , value . tzinfo , tz ) <EOL> return super ( LocalizedDateTimeWidget , self ) . decompress ( value ) <EOL> return [ None , self . get_timezone ( value ) ] <EOL> class LocalizedDateTimeField ( TimeZoneDateTimeField ) : <EOL> """<STR_LIT>""" <EOL> widget = LocalizedDateTimeWidget <EOL> def compress ( self , * args , ** kwargs ) : <EOL> result = super ( LocalizedDateTimeField , self ) . compress ( * args , ** kwargs ) <EOL> if result : <EOL> result = adjust_datetime_to_timezone ( result , result . tzinfo , <EOL> pytz . timezone ( settings . TIME_ZONE ) ) . replace ( tzinfo = None ) <EOL> return result <EOL> class SplitLocalizedDateTimeWidget ( LocalizedDateTimeWidget ) : <EOL> def decompress ( self , value ) : <EOL> splited = super ( SplitLocalizedDateTimeWidget , self ) . decompress ( value ) <EOL> if splited [ <NUM_LIT:0> ] : <EOL> return [ splited [ <NUM_LIT:0> ] . date ( ) , splited [ <NUM_LIT:0> ] . time ( ) . replace ( microsecond = <NUM_LIT:0> ) , splited [ <NUM_LIT:1> ] ] <EOL> return [ None , None , splited [ <NUM_LIT:1> ] ] <EOL> class SplitLocalizedDateTimeField ( MultiValueField ) : <EOL> widget = SplitLocalizedDateTimeWidget <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) <EOL> } <EOL> def __init__ ( self , input_date_formats = None , input_time_formats = None , * args , ** kwargs ) : <EOL> errors = self . default_error_messages . copy ( ) <EOL> if '<STR_LIT>' in kwargs : <EOL> errors . update ( kwargs [ '<STR_LIT>' ] ) <EOL> self . localize = kwargs . get ( '<STR_LIT>' , True ) <EOL> fields = ( <EOL> DateField ( input_formats = input_date_formats , <EOL> error_messages = { '<STR_LIT>' : errors [ '<STR_LIT>' ] } , <EOL> localize = self . localize ) , <EOL> TimeField ( input_formats = input_time_formats , <EOL> error_messages = { '<STR_LIT>' : errors [ '<STR_LIT>' ] } , <EOL> localize = self . localize ) , <EOL> TimeZoneField ( error_messages = { '<STR_LIT>' : errors [ '<STR_LIT>' ] } ) <EOL> ) <EOL> dw = kwargs . pop ( '<STR_LIT>' , fields [ <NUM_LIT:0> ] . widget ) <EOL> tw = kwargs . pop ( '<STR_LIT>' , fields [ <NUM_LIT:1> ] . widget ) <EOL> tzw = kwargs . pop ( '<STR_LIT>' , fields [ <NUM_LIT:2> ] . widget ) <EOL> if isinstance ( self . widget , type ) : <EOL> self . widget = self . widget ( ( dw , tw , tzw ) ) <EOL> super ( SplitLocalizedDateTimeField , self ) . __init__ ( fields , * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> if ( isinstance ( value , ( list , tuple ) ) and <EOL> ( not value or not [ v for v in value [ : - <NUM_LIT:1> ] if v not in validators . EMPTY_VALUES ] ) ) : <EOL> return super ( SplitLocalizedDateTimeField , self ) . clean ( [ ] ) <EOL> return super ( SplitLocalizedDateTimeField , self ) . clean ( value ) <EOL> def compress ( self , data_list ) : <EOL> if data_list : <EOL> if data_list [ <NUM_LIT:0> ] in validators . EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if data_list [ <NUM_LIT:1> ] in validators . EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if data_list [ <NUM_LIT:2> ] in validators . EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> dt = datetime . datetime . combine ( * data_list [ : <NUM_LIT:2> ] ) <EOL> tz = data_list [ <NUM_LIT:2> ] <EOL> if dt and tz : <EOL> result = adjust_datetime_to_timezone ( dt , tz , <EOL> pytz . timezone ( settings . TIME_ZONE ) ) . replace ( tzinfo = None ) <EOL> return result <EOL> return None <EOL> class TimeZoneForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> timezone = TimeZoneField ( ) </s>
