<s> from __future__ import absolute_import <EOL> from wtforms import ValidationError <EOL> from wtforms . validators import StopValidation <EOL> try : <EOL> from validators import email <EOL> except ImportError : <EOL> from validators import is_email as email <EOL> class ControlStructure ( object ) : <EOL> """<STR_LIT>""" <EOL> message = None <EOL> def reraise ( self , exc ) : <EOL> if not self . message : <EOL> raise exc <EOL> else : <EOL> raise type ( exc ) ( self . message ) <EOL> class Chain ( ControlStructure ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , validators , message = None ) : <EOL> self . validators = validators <EOL> if message : <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> for validator in self . validators : <EOL> try : <EOL> validator ( form , field ) <EOL> except ValidationError as exc : <EOL> self . reraise ( exc ) <EOL> except StopValidation as exc : <EOL> self . reraise ( exc ) <EOL> class If ( ControlStructure ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , condition , validator , message = None ) : <EOL> self . condition = condition <EOL> self . validator = validator <EOL> if message : <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> if self . condition ( form , field ) : <EOL> try : <EOL> self . validator ( form , field ) <EOL> except ValidationError as exc : <EOL> self . reraise ( exc ) <EOL> except StopValidation as exc : <EOL> self . reraise ( exc ) <EOL> class BaseDateTimeRange ( object ) : <EOL> def __init__ ( self , min = None , max = None , format = '<STR_LIT>' , message = None ) : <EOL> self . min = min <EOL> self . max = max <EOL> self . format = format <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> data = field . data <EOL> min_ = self . min ( ) if callable ( self . min ) else self . min <EOL> max_ = self . max ( ) if callable ( self . max ) else self . max <EOL> if ( data is None or ( min_ is not None and data < min_ ) or <EOL> ( max_ is not None and data > max_ ) ) : <EOL> if self . message is None : <EOL> if max_ is None : <EOL> self . message = field . gettext ( self . greater_than_msg ) <EOL> elif min_ is None : <EOL> self . message = field . gettext ( self . less_than_msg ) <EOL> else : <EOL> self . message = field . gettext ( self . between_msg ) <EOL> raise ValidationError ( <EOL> self . message % dict ( <EOL> field_label = field . label , <EOL> min = min_ . strftime ( self . format ) if min_ else '<STR_LIT>' , <EOL> max = max_ . strftime ( self . format ) if max_ else '<STR_LIT>' <EOL> ) <EOL> ) <EOL> class TimeRange ( BaseDateTimeRange ) : <EOL> """<STR_LIT>""" <EOL> greater_than_msg = u'<STR_LIT>' <EOL> less_than_msg = u'<STR_LIT>' <EOL> between_msg = u'<STR_LIT>' <EOL> def __init__ ( self , min = None , max = None , format = '<STR_LIT>' , message = None ) : <EOL> super ( TimeRange , self ) . __init__ ( <EOL> min = min , max = max , format = format , message = message <EOL> ) <EOL> class DateRange ( BaseDateTimeRange ) : <EOL> """<STR_LIT>""" <EOL> greater_than_msg = u'<STR_LIT>' <EOL> less_than_msg = u'<STR_LIT>' <EOL> between_msg = u'<STR_LIT>' <EOL> def __init__ ( self , min = None , max = None , format = '<STR_LIT>' , message = None ) : <EOL> super ( DateRange , self ) . __init__ ( <EOL> min = min , max = max , format = format , message = message <EOL> ) <EOL> class Email ( object ) : <EOL> """<STR_LIT>""" <EOL> domain_whitelist = [ '<STR_LIT:localhost>' ] <EOL> def __init__ ( self , message = None , whitelist = None ) : <EOL> self . message = message <EOL> if whitelist is not None : <EOL> self . domain_whitelist = whitelist <EOL> def __call__ ( self , form , field ) : <EOL> if not email ( field . data , self . domain_whitelist ) : <EOL> if self . message is None : <EOL> self . message = field . gettext ( u'<STR_LIT>' ) <EOL> raise ValidationError ( self . message ) </s>
