<s> from __future__ import absolute_import <EOL> from werkzeug . datastructures import FileStorage <EOL> import wtforms <EOL> from wtforms import SubmitField <EOL> from wtforms import validators <EOL> from wtforms . compat import string_types <EOL> from digits . utils . routing import get_request_arg <EOL> def validate_required_iff ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _validator ( form , field ) : <EOL> all_conditions_met = True <EOL> for key , value in kwargs . iteritems ( ) : <EOL> if getattr ( form , key ) . data != value : <EOL> all_conditions_met = False <EOL> if all_conditions_met : <EOL> if field . data is None or ( isinstance ( field . data , ( str , unicode ) ) <EOL> and not field . data . strip ( ) ) or ( isinstance ( field . data , FileStorage ) <EOL> and not field . data . filename . strip ( ) ) : <EOL> raise validators . ValidationError ( '<STR_LIT>' ) <EOL> else : <EOL> field . errors [ : ] = [ ] <EOL> raise validators . StopValidation ( ) <EOL> return _validator <EOL> def validate_greater_than ( fieldname ) : <EOL> """<STR_LIT>""" <EOL> def _validator ( form , field ) : <EOL> try : <EOL> other = form [ fieldname ] <EOL> except KeyError : <EOL> raise validators . ValidationError ( field . gettext ( u"<STR_LIT>" ) % fieldname ) <EOL> if field . data != '<STR_LIT>' and field . data < other . data : <EOL> message = field . gettext ( u'<STR_LIT>' % fieldname ) <EOL> raise validators . ValidationError ( message ) <EOL> return _validator <EOL> class Tooltip ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field_id , for_name , text ) : <EOL> self . field_id = field_id <EOL> self . text = text <EOL> self . for_name = for_name <EOL> def __str__ ( self ) : <EOL> return self ( ) <EOL> def __unicode__ ( self ) : <EOL> return self ( ) <EOL> def __html__ ( self ) : <EOL> return self ( ) <EOL> def __call__ ( self , text = None , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . field_id ) <EOL> return wtforms . widgets . HTMLString ( <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % ( self . for_name , self . text ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . field_id , self . for_name , self . text ) <EOL> class Explanation ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field_id , for_name , filename ) : <EOL> self . field_id = field_id <EOL> self . file = filename <EOL> self . for_name = for_name <EOL> def __str__ ( self ) : <EOL> return self ( ) <EOL> def __unicode__ ( self ) : <EOL> return self ( ) <EOL> def __html__ ( self ) : <EOL> return self ( ) <EOL> def __call__ ( self , file = None , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . field_id ) <EOL> import flask <EOL> from digits . webapp import app <EOL> html = '<STR_LIT>' <EOL> with app . app_context ( ) : <EOL> html = flask . render_template ( file if file else self . file ) <EOL> if len ( html ) == <NUM_LIT:0> : return '<STR_LIT>' <EOL> return wtforms . widgets . HTMLString ( <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT:%s>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % ( self . for_name , html , self . for_name ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . field_id , self . for_name , self . file ) <EOL> class IntegerField ( wtforms . IntegerField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( IntegerField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class FloatField ( wtforms . FloatField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( FloatField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class SelectField ( wtforms . SelectField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( SelectField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class SelectMultipleField ( wtforms . SelectMultipleField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( SelectMultipleField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class TextField ( wtforms . TextField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( TextField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class StringField ( wtforms . StringField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( StringField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class FileInput ( object ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , field , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT:id>' , field . id ) <EOL> return wtforms . widgets . HTMLString ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) % ( wtforms . widgets . html_params ( id = field . name + '<STR_LIT>' , name = field . name + '<STR_LIT>' ) , <EOL> wtforms . widgets . html_params ( name = field . name , type = '<STR_LIT:file>' , ** kwargs ) , <EOL> wtforms . widgets . html_params ( id = field . id + '<STR_LIT>' , name = field . name + '<STR_LIT>' , type = '<STR_LIT:text>' ) ) ) <EOL> class FileField ( wtforms . FileField ) : <EOL> widget = FileInput ( ) <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( FileField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class TextAreaField ( wtforms . TextAreaField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( TextAreaField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class BooleanField ( wtforms . BooleanField ) : <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( BooleanField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> class MultiIntegerField ( wtforms . Field ) : <EOL> """<STR_LIT>""" <EOL> widget = wtforms . widgets . TextInput ( ) <EOL> def is_int ( self , v ) : <EOL> try : <EOL> v = int ( v ) <EOL> return True <EOL> except : <EOL> return False <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( MultiIntegerField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip + '<STR_LIT>' ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> self . small_text = '<STR_LIT>' <EOL> def __setattr__ ( self , name , value ) : <EOL> if name == '<STR_LIT:data>' : <EOL> if not isinstance ( value , ( list , tuple ) ) : <EOL> value = [ value ] <EOL> value = [ int ( x ) for x in value if self . is_int ( x ) ] <EOL> if len ( value ) == <NUM_LIT:0> : <EOL> value = [ None ] <EOL> self . __dict__ [ name ] = value <EOL> def _value ( self ) : <EOL> if self . raw_data : <EOL> return self . raw_data [ <NUM_LIT:0> ] <EOL> return '<STR_LIT:U+002C>' . join ( [ str ( x ) for x in self . data if self . is_int ( x ) ] ) <EOL> def process_formdata ( self , valuelist ) : <EOL> if valuelist : <EOL> try : <EOL> valuelist [ <NUM_LIT:0> ] = valuelist [ <NUM_LIT:0> ] . replace ( '<STR_LIT:[>' , '<STR_LIT>' ) <EOL> valuelist [ <NUM_LIT:0> ] = valuelist [ <NUM_LIT:0> ] . replace ( '<STR_LIT:]>' , '<STR_LIT>' ) <EOL> valuelist [ <NUM_LIT:0> ] = valuelist [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) <EOL> self . data = [ int ( float ( datum ) ) for datum in valuelist [ <NUM_LIT:0> ] ] <EOL> except ValueError : <EOL> self . data = [ None ] <EOL> raise ValueError ( self . gettext ( '<STR_LIT>' ) ) <EOL> class MultiFloatField ( wtforms . Field ) : <EOL> """<STR_LIT>""" <EOL> widget = wtforms . widgets . TextInput ( ) <EOL> def is_float ( self , v ) : <EOL> try : <EOL> v = float ( v ) <EOL> return True <EOL> except : <EOL> return False <EOL> def __init__ ( self , label = '<STR_LIT>' , validators = None , tooltip = '<STR_LIT>' , explanation_file = '<STR_LIT>' , ** kwargs ) : <EOL> super ( MultiFloatField , self ) . __init__ ( label , validators , ** kwargs ) <EOL> self . tooltip = Tooltip ( self . id , self . short_name , tooltip + '<STR_LIT>' ) <EOL> self . explanation = Explanation ( self . id , self . short_name , explanation_file ) <EOL> self . small_text = '<STR_LIT>' <EOL> def __setattr__ ( self , name , value ) : <EOL> if name == '<STR_LIT:data>' : <EOL> if not isinstance ( value , ( list , tuple ) ) : <EOL> value = [ value ] <EOL> value = [ float ( x ) for x in value if self . is_float ( x ) ] <EOL> if len ( value ) == <NUM_LIT:0> : <EOL> value = [ None ] <EOL> self . __dict__ [ name ] = value <EOL> def _value ( self ) : <EOL> if self . raw_data : <EOL> return self . raw_data [ <NUM_LIT:0> ] <EOL> return '<STR_LIT:U+002C>' . join ( [ str ( x ) for x in self . data if self . is_float ( x ) ] ) <EOL> def process_formdata ( self , valuelist ) : <EOL> if valuelist : <EOL> try : <EOL> valuelist [ <NUM_LIT:0> ] = valuelist [ <NUM_LIT:0> ] . replace ( '<STR_LIT:[>' , '<STR_LIT>' ) <EOL> valuelist [ <NUM_LIT:0> ] = valuelist [ <NUM_LIT:0> ] . replace ( '<STR_LIT:]>' , '<STR_LIT>' ) <EOL> valuelist [ <NUM_LIT:0> ] = valuelist [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) <EOL> self . data = [ float ( datum ) for datum in valuelist [ <NUM_LIT:0> ] ] <EOL> except ValueError : <EOL> self . data = [ None ] <EOL> raise ValueError ( self . gettext ( '<STR_LIT>' ) ) <EOL> def data_array ( self ) : <EOL> if isinstance ( self . data , ( list , tuple ) ) : <EOL> return self . data <EOL> else : <EOL> return [ self . data ] <EOL> class MultiNumberRange ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , min = None , max = None , message = None ) : <EOL> self . min = min <EOL> self . max = max <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> fdata = field . data if isinstance ( field . data , ( list , tuple ) ) else [ field . data ] <EOL> for data in fdata : <EOL> if data is None or ( self . min is not None and data < self . min ) or ( self . max is not None and data > self . max ) : <EOL> message = self . message <EOL> if message is None : <EOL> if self . max is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> elif self . min is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> else : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> raise validators . ValidationError ( message % dict ( data = data , min = self . min , max = self . max ) ) <EOL> class MultiOptional ( object ) : <EOL> """<STR_LIT>""" <EOL> field_flags = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , strip_whitespace = True ) : <EOL> if strip_whitespace : <EOL> self . string_check = lambda s : s . strip ( ) <EOL> else : <EOL> self . string_check = lambda s : s <EOL> def __call__ ( self , form , field ) : <EOL> if not field . raw_data or isinstance ( field . raw_data [ <NUM_LIT:0> ] , string_types ) and not self . string_check ( field . raw_data [ <NUM_LIT:0> ] ) : <EOL> field . errors [ : ] = [ ] <EOL> raise validators . StopValidation ( ) <EOL> def add_warning ( form , warning ) : <EOL> if not hasattr ( form , '<STR_LIT>' ) : <EOL> form . warnings = tuple ( [ ] ) <EOL> form . warnings += tuple ( [ warning ] ) <EOL> return True <EOL> def iterate_over_form ( job , form , function , prefix = [ '<STR_LIT>' ] , indent = '<STR_LIT>' ) : <EOL> warnings = False <EOL> if not hasattr ( form , '<STR_LIT>' ) : return False <EOL> whitelist_fields = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> blacklist_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for attr_name in vars ( form ) : <EOL> if attr_name == '<STR_LIT>' or attr_name == '<STR_LIT>' : <EOL> continue <EOL> attr = getattr ( form , attr_name ) <EOL> if isinstance ( attr , object ) : <EOL> if isinstance ( attr , SubmitField ) : continue <EOL> warnings |= iterate_over_form ( job , attr , function , prefix + [ attr_name ] , indent + '<STR_LIT:U+0020>' ) <EOL> if hasattr ( attr , '<STR_LIT:data>' ) and hasattr ( attr , '<STR_LIT:type>' ) : <EOL> if ( isinstance ( attr . data , int ) or <EOL> isinstance ( attr . data , float ) or <EOL> isinstance ( attr . data , basestring ) or <EOL> attr . type in whitelist_fields ) : <EOL> key = '<STR_LIT>' % ( '<STR_LIT:.>' . join ( prefix ) , attr_name ) <EOL> warnings |= function ( job , attr , key , attr . data ) <EOL> if ( len ( attr . type ) > <NUM_LIT:5> and attr . type [ - <NUM_LIT:5> : ] == '<STR_LIT>' and <EOL> attr . type not in whitelist_fields and <EOL> attr . type not in blacklist_fields ) : <EOL> warnings |= add_warning ( attr , '<STR_LIT>' % attr . type ) <EOL> return warnings <EOL> def set_data ( job , form , key , value ) : <EOL> if not hasattr ( job , '<STR_LIT>' ) : job . form_data = dict ( ) <EOL> job . form_data [ key ] = value <EOL> if isinstance ( value , basestring ) : <EOL> value = '<STR_LIT>' + value + '<STR_LIT>' <EOL> return False <EOL> def get_data ( job , form , key , value ) : <EOL> if key not in job . form_data : <EOL> add_warning ( form , '<STR_LIT>' ) <EOL> return True <EOL> else : <EOL> form . data = job . form_data [ key ] <EOL> return False <EOL> def save_form_to_job ( job , form ) : <EOL> iterate_over_form ( job , form , set_data ) <EOL> def fill_form_from_job ( job , form ) : <EOL> form . warnings = iterate_over_form ( job , form , get_data ) <EOL> def fill_form_if_cloned ( form ) : <EOL> from digits . webapp import scheduler <EOL> clone = get_request_arg ( '<STR_LIT>' ) <EOL> if clone is not None : <EOL> clone_job = scheduler . get_job ( clone ) <EOL> fill_form_from_job ( clone_job , form ) </s>
