<s> from datetime import datetime <EOL> from wtforms import ( Form , <EOL> IntegerField , <EOL> DecimalField , <EOL> FormField , <EOL> SelectField ) <EOL> from wtforms . validators import Required , Optional <EOL> from werkzeug . datastructures import MultiDict <EOL> from breezeminder . models . reminder import ReminderType <EOL> from breezeminder . forms . validators import ( ActiveStateOptional , <EOL> Conditional , <EOL> PositiveNumber ) <EOL> from breezeminder . forms . fields import BootstrapSelectMultipleField <EOL> def _create_tuple_range ( start , stop ) : <EOL> return [ ( int ( x ) , str ( x ) ) for x in xrange ( start , stop ) ] <EOL> DATE_QUANTIFIERS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> DATE_RANGE_CHOICES = [ ( x , x ) for x in DATE_QUANTIFIERS ] <EOL> DATE_THRESHOLD_CHOICES = _create_tuple_range ( <NUM_LIT:1> , <NUM_LIT:11> ) <EOL> MONTH_CHOICES = [ ( - <NUM_LIT:1> , '<STR_LIT>' ) ] <EOL> DAY_CHOICES = [ ( - <NUM_LIT:1> , '<STR_LIT>' ) ] <EOL> YEAR_CHOICES = [ ( - <NUM_LIT:1> , '<STR_LIT>' ) ] <EOL> MONTH_CHOICES . extend ( _create_tuple_range ( <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> DAY_CHOICES . extend ( _create_tuple_range ( <NUM_LIT:1> , <NUM_LIT:32> ) ) <EOL> YEAR_CHOICES . extend ( _create_tuple_range ( datetime . now ( ) . year , <EOL> datetime . now ( ) . year + <NUM_LIT:10> ) ) <EOL> REMINDER_TYPES = ReminderType . objects . all_tuples ( ) <EOL> NOTIFICATION_CHOICES = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> class _DateForm ( Form ) : <EOL> month = SelectField ( '<STR_LIT>' , <EOL> coerce = int , <EOL> choices = MONTH_CHOICES , <EOL> validators = [ Optional ( ) ] ) <EOL> day = SelectField ( '<STR_LIT>' , <EOL> coerce = int , <EOL> choices = DAY_CHOICES , <EOL> validators = [ Optional ( ) ] ) <EOL> year = SelectField ( '<STR_LIT>' , <EOL> coerce = int , <EOL> choices = YEAR_CHOICES , <EOL> validators = [ Optional ( ) ] ) <EOL> class ReminderForm ( Form ) : <EOL> type = SelectField ( '<STR_LIT>' , <EOL> choices = REMINDER_TYPES , <EOL> validators = [ Required ( ) ] ) <EOL> balance_threshold = DecimalField ( '<STR_LIT>' , <EOL> places = <NUM_LIT:2> , <EOL> validators = [ <EOL> ActiveStateOptional ( ) , <EOL> Conditional ( '<STR_LIT:type>' , '<STR_LIT>' , validators = [ Required ( ) , PositiveNumber ( ) ] ) <EOL> ] ) <EOL> ride_threshold = IntegerField ( '<STR_LIT>' , <EOL> validators = [ <EOL> ActiveStateOptional ( ) , <EOL> Conditional ( '<STR_LIT:type>' , '<STR_LIT>' , validators = [ Required ( ) , PositiveNumber ( ) ] ) <EOL> ] ) <EOL> round_trip_threshold = IntegerField ( '<STR_LIT>' , <EOL> validators = [ <EOL> ActiveStateOptional ( ) , <EOL> Conditional ( '<STR_LIT:type>' , '<STR_LIT>' , validators = [ Required ( ) , PositiveNumber ( ) ] ) <EOL> ] ) <EOL> exp_threshold = SelectField ( '<STR_LIT>' , <EOL> choices = DATE_THRESHOLD_CHOICES , coerce = int , <EOL> validators = [ <EOL> Conditional ( '<STR_LIT:type>' , '<STR_LIT>' , validators = Required ( ) ) <EOL> ] ) <EOL> exp_quantity = SelectField ( '<STR_LIT>' , <EOL> choices = DATE_RANGE_CHOICES , <EOL> validators = [ <EOL> Conditional ( '<STR_LIT:type>' , '<STR_LIT>' , validators = Required ( ) ) <EOL> ] ) <EOL> valid_until = FormField ( _DateForm , separator = '<STR_LIT:/>' , label = '<STR_LIT>' ) <EOL> notifications = BootstrapSelectMultipleField ( '<STR_LIT>' , <EOL> choices = NOTIFICATION_CHOICES , <EOL> validators = [ Required ( ) ] ) <EOL> def _disable_optional ( self , field ) : <EOL> field . flags . optional = False <EOL> for idx , obj in enumerate ( field . validators ) : <EOL> if isinstance ( obj , ActiveStateOptional ) : <EOL> obj . deactivate ( ) <EOL> break <EOL> def _enable_optional ( self , field ) : <EOL> field . flags . optional = True <EOL> for idx , obj in enumerate ( field . validators ) : <EOL> if isinstance ( obj , ActiveStateOptional ) : <EOL> obj . activate ( ) <EOL> break <EOL> def _pre_validate ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . type . data == '<STR_LIT>' : <EOL> self . _disable_optional ( self . balance_threshold ) <EOL> elif self . type . data == '<STR_LIT>' : <EOL> self . _disable_optional ( self . ride_threshold ) <EOL> elif self . type . data == '<STR_LIT>' : <EOL> self . _disable_optional ( self . round_trip_threshold ) <EOL> elif self . type . data == '<STR_LIT>' : <EOL> pass <EOL> def _post_validate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _enable_optional ( self . balance_threshold ) <EOL> self . _enable_optional ( self . ride_threshold ) <EOL> self . _enable_optional ( self . round_trip_threshold ) <EOL> def validate ( self , * args , ** kwargs ) : <EOL> self . _pre_validate ( ) <EOL> retval = super ( ReminderForm , self ) . validate ( * args , ** kwargs ) <EOL> self . _post_validate ( ) <EOL> return retval <EOL> def is_changed_from ( self , reminder ) : <EOL> if reminder . type != self . type . data : <EOL> return True <EOL> try : <EOL> if self . type . data == '<STR_LIT>' : <EOL> return float ( reminder . threshold ) != float ( self . balance_threshold . data ) <EOL> if self . type . data == '<STR_LIT>' : <EOL> return int ( reminder . threshold ) != int ( self . ride_threshold . data ) <EOL> if self . type . data == '<STR_LIT>' : <EOL> return int ( reminder . threshold ) != int ( self . round_trip_threshold . data ) <EOL> if self . type . data == '<STR_LIT>' : <EOL> if int ( reminder . threshold ) != int ( self . exp_threshold . data ) : <EOL> return True <EOL> elif reminder . quantifier != self . exp_quantity . data : <EOL> return True <EOL> except : <EOL> pass <EOL> return False <EOL> def populate_reminder ( self , reminder ) : <EOL> reminder . type = ReminderType . objects . get ( key = self . type . data ) <EOL> try : <EOL> reminder . valid_until = datetime ( month = self . valid_until . month . data , <EOL> day = self . valid_until . day . data , <EOL> year = self . valid_until . year . data ) <EOL> except : <EOL> reminder . valid_until = None <EOL> reminder . send_sms = '<STR_LIT>' in self . notifications . data <EOL> reminder . send_email = '<STR_LIT>' in self . notifications . data <EOL> if reminder . type == '<STR_LIT>' : <EOL> reminder . threshold = float ( '<STR_LIT>' % round ( self . balance_threshold . data , <NUM_LIT:2> ) ) <EOL> elif reminder . type == '<STR_LIT>' : <EOL> reminder . threshold = self . ride_threshold . data <EOL> elif reminder . type == '<STR_LIT>' : <EOL> reminder . threshold = self . round_trip_threshold . data <EOL> elif reminder . type == '<STR_LIT>' : <EOL> reminder . threshold = self . exp_threshold . data <EOL> reminder . quantifier = self . exp_quantity . data <EOL> return reminder <EOL> @ staticmethod <EOL> def from_reminder ( reminder ) : <EOL> data = { <EOL> '<STR_LIT:type>' : reminder . type . key , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> if reminder . valid_until : <EOL> for part in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> data [ '<STR_LIT>' % part ] = getattr ( reminder . valid_until , part , None ) <EOL> if reminder . type == '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' % round ( reminder . threshold , <NUM_LIT:2> ) <EOL> elif reminder . type == '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] = reminder . threshold <EOL> elif reminder . type == '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] = reminder . threshold <EOL> elif reminder . type == '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] = reminder . threshold <EOL> data [ '<STR_LIT>' ] = reminder . quantifier <EOL> if reminder . send_sms : <EOL> data [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if reminder . send_email : <EOL> data [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> for key in data . copy ( ) : <EOL> if data [ key ] is None : <EOL> del data [ key ] <EOL> return ReminderForm ( MultiDict ( data ) ) </s>
