<s> from __future__ import unicode_literals <EOL> from wtforms import Form <EOL> from wtforms import RadioField <EOL> from wtforms import IntegerField <EOL> from wtforms import validators <EOL> from billy . db import tables <EOL> from billy . api . utils import MINIMUM_AMOUNT <EOL> class EnumRadioField ( RadioField ) : <EOL> def __init__ ( self , enum_type , ** kwargs ) : <EOL> super ( EnumRadioField , self ) . __init__ ( <EOL> coerce = self . _value_to_enum , <EOL> ** kwargs <EOL> ) <EOL> self . enum_type = enum_type <EOL> self . choices = [ <EOL> ( enum . value . lower ( ) , enum . description ) for enum in self . enum_type <EOL> ] <EOL> def _value_to_enum ( self , key ) : <EOL> if key is None : <EOL> return key <EOL> return self . enum_type . from_string ( key . upper ( ) ) <EOL> def pre_validate ( self , form ) : <EOL> for enum in self . enum_type : <EOL> if self . data == enum : <EOL> break <EOL> else : <EOL> raise ValueError ( <EOL> self . gettext ( '<STR_LIT>' ) <EOL> . format ( <EOL> self . enum_type . __name__ , <EOL> list ( self . enum_type . values ( ) ) , <EOL> ) <EOL> ) <EOL> class PlanCreateForm ( Form ) : <EOL> plan_type = EnumRadioField ( <EOL> enum_type = tables . PlanType , <EOL> label = '<STR_LIT>' , <EOL> validators = [ <EOL> validators . Required ( ) , <EOL> ] , <EOL> ) <EOL> frequency = EnumRadioField ( <EOL> enum_type = tables . PlanFrequency , <EOL> label = '<STR_LIT>' , <EOL> validators = [ <EOL> validators . Required ( ) , <EOL> ] , <EOL> ) <EOL> amount = IntegerField ( '<STR_LIT>' , [ <EOL> validators . Required ( ) , <EOL> validators . NumberRange ( min = MINIMUM_AMOUNT ) <EOL> ] ) <EOL> interval = IntegerField ( <EOL> '<STR_LIT>' , <EOL> [ <EOL> validators . Optional ( ) , <EOL> validators . NumberRange ( min = <NUM_LIT:1> ) , <EOL> ] , <EOL> default = <NUM_LIT:1> <EOL> ) </s>
