class NewCircle(twf.TableForm): name = twf.TextField(label='Name : ', help_text="Enter the circle's name", validator=twc.Validator(required=True)) description = twf.TextField(label='Description : ', help_text="Enter the circle's description", validator=twc.Validator(required=True)) submit = twf.SubmitButton(id="submit", value="New")
class Details(twf.ListFieldSet): label = None key = 'details' email = twf.TextField(validator=twc.Validator(required=True), css_class="form-control", label=l_("Email")) phone = twf.TextField(validator=twc.Validator(), css_class="form-control", label=l_("Telefono"))
class EditProject(twf.TableForm): pid = twf.HiddenField() name = twf.TextField(label='Name : ', validator=twc.Validator(required=True)) tracks = twf.MultipleSelectField(label='Tracks : ', css_class='track_select') submit = twf.SubmitButton(id="submit", value="Edit")
def test_auto_unflatten(self): test = twc.CompoundWidget( id='a', children=[ twc.Widget(id='b', validator=twc.Validator(required=True)), ]) testapi.request(1) eq_(test.validate({'a:b': '10'}), {'b': '10'})
class EditTrack(twf.TableForm): color = twf.HiddenField() pid = twf.HiddenField() track_id = twf.HiddenField() name = twf.TextField(label='Name : ', help_text="Enter the track's name", validator=twc.Validator(required=True)) submit = twf.SubmitButton(id="submit", value="Edit")
class NewProject(twf.TableForm): # __omit_fields__ = ['id', 'user', 'date', 'created'] project_name = twf.TextField(label_text="Name :", validator=twc.Validator(required=True)) description = twf.TextArea(label_text="Description :", ) samples = twf.MultipleSelectField( label_text="Your samples : ", help_text="You can add some of your existing samples to this project.") submit = twf.SubmitButton(value="Create my project")
class child(twf.TableLayout): file = twf.FileField(validator=twf.FileValidator(required=True, extention='.html')) email = twf.TextField(validator=twc.EmailValidator(required=True)) # confirm_email = twf.TextField() class fred(twf.GridLayout): repetitions = 3 class child(twf.RowLayout): bob = twf.TextField() rob = twf.TextField() validator = twc.MatchValidator('bob', 'rob') select = twf.SingleSelectField(options=list(enumerate(opts)), validator=twc.Validator(required=True), item_validator=twc.IntValidator())
class ShipmentInfo(twf.ListFieldSet): label = l_("Dati Spedizione") key = 'shipment_info' receiver = twf.TextField(validator=twc.Validator(required=True), css_class="form-control", label=l_("Destinatario")) address = twf.TextField(validator=twc.Validator(required=True), css_class="form-control", label=l_("Indirizzo")) city = twf.TextField(validator=twc.Validator(required=True), css_class="form-control", label=l_("Città ")) province = twf.TextField(validator=twc.Validator(required=True), css_class="form-control", label=l_("Provincia")) zip_code = twf.TextField(validator=twc.Validator(required=True), css_class="form-control", label=l_("CAP")) country = twf.SingleSelectField( css_class="form-control", prompt_text=None, label=l_("Stato"), options=twc.Deferred(lambda: [('IT', l_('Stato'))]))
def validator(ptype, required): from bs.lib.operations import wordlist """ Get the right validator for the ptype given """ if type2validator.get(ptype, False): if required: return twc.IntValidator(required=True) return twc.IntValidator() elif required: if wordlist.is_of_type(ptype, wordlist.FILE): if required: return twf.FileValidator(required=True) return twf.FileValidator() return twc.Validator(required=True) return None
def set_validator(validator, field): required = False strip = False try: required = validator.required except AttributeError: pass try: strip = validator.strip except AttributeError: pass if 'BsTripleFileField' in str(field) or 'BsFileField' in str(field): field.validator = twb.BsFileFieldValidator(required=required, strip=strip) # elif 'BsMultiple' in str(field): # field.validator = twb.BsMultipleValidator(required=required, strip=strip) else: field.validator = twc.Validator(required=required, strip=strip)
class child(twf.TableLayout): id = 'xx' a = twd.CalendarDatePicker() aa = twd.WriteOnlyTextField(validator=twc.EmailValidator, value='xx') b = twf.CheckBox(validator=twc.Validator(required=True)) c = twd.LinkContainer(link='x$', child=twf.SingleSelectField(options=[''] + opts)) class d(twd.GrowingGridLayout): value = [{'a': 'aaa', 'b': 'bbb'}] a = twf.TextField(validator=twc.EmailValidator) b = twf.TextField() class e(twd.HidingTableLayout): a = twd.HidingSingleSelectField(options=[''] + opts, mapping={ 'Red': ['b'], 'Yellow': ['c'] }) b = twf.TextField(validator=twc.Required) c = twf.TextField()
class child(axf.bootstrap.BootstrapFormLayout): inline_engine_name = 'genshi' template = ''' <div xmlns:py="http://genshi.edgewall.org/" py:strip="True"> <py:for each="c in w.children_hidden"> ${c.display()} </py:for> <div> <span id="${w.compound_id}:error" class="error" > <p py:for="error in w.rollup_errors" class="alert alert-danger"> <span class="glyphicon glyphicon-exclamation-sign"></span> ${error} </p> </span> </div> <div class="form-horizontal"> <div py:for="c in w.children_non_hidden" class="form-group ${((c.validator and getattr(c.validator, 'required', getattr(c.validator, 'not_empty', False))) and ' required' or '') + (c.error_msg and ' has-error' or '')}"> <label py:if="c.label != None" class="col-sm-3 control-label" for="${c.compound_id}"> $c.label </label> <div class="col-sm-7"> ${c.display()} <span id="${c.compound_id}:error" class="error help-block" py:content="c.error_msg"/> </div> <div>${w.submit.display()}</div> </div> </div> </div> ''' uid = twf.HiddenField() name = twf.TextField(label='Dashboard Name', validator=twc.Validator(required=True)) submit = twf.SubmitButton(value=l_('Save'), css_class='btn btn-default col-sm-2')
class MatchyWidget(twc.CompoundWidget): validator = formencode.validators.FieldsMatch('one', 'two') one = twc.Widget two = twc.Widget three = twc.Widget(validator=twc.Validator(required=True))
import tw2.core.testbase as tb from tw2.core.validation import * import re import datetime import formencode from nose.tools import eq_, raises from webob.multidict import MultiDict from unittest import TestCase import sys HAS_SKIP = sys.version_info[0] == 2 and sys.version_info[1] == 7 compound_widget = twc.CompoundWidget( id='a', children=[ twc.Widget(id='b', validator=twc.Validator(required=True)), twc.Widget(id='c', validator=twc.Validator(required=True)), ]) repeating_widget = twc.RepeatingWidget( id='a', child=twc.Widget(validator=twc.Validator(required=True))) compound_keyed_widget = twc.CompoundWidget( id='a', children=[ twc.Widget(id='b', key='x', validator=twc.Validator(required=True)), twc.Widget(id='c', key='y', validator=formencode.validators.OpenId()), ]) #This is required to make tests pass on non english systems formencode.api.set_stdtranslation(languages=['en'])
class child(axf.bootstrap.BootstrapFormLayout): name = twf.TextField(label='Extraction Name', validator=twc.Validator(required=True), css_class="new_extraction_input")
class CompWidget(twc.CompoundWidget): one = twc.Widget(validator=formencode.validators.NotEmpty()) two = twc.Widget(validator=formencode.validators.NotEmpty()) three = twc.Widget(validator=twc.Validator(required=True))
def clone_form(user_lab, id_object): ''' to clone dynamic measurement form ''' lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() #static list ] list_static = [ twf.HiddenField(id="IDselected", label_text="ID selected :"), twf.TextField(id="name", label_text="Name :", placeholder="Measurement name...", validator=twc.Validator(required=True)), twf.TextArea(id="description", label_text="Description :"), twf.MultipleSelectField( id="samples", label_text="Your samples : ", help_text="You can add some of your existing data to this project." ), twf.CheckBox( id="status_type", label_text="Privacy : ", help_text= "Check to have it available from outside EPFL (required for UCSC visualisation)" ), twf.CheckBox(id="type", label_text="Raw data : ", help_text="Check if raw data"), #twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."), twd.HidingRadioButtonList(id="upload_way", label_text='Upload my file via...', options=('my computer', 'a Vital-IT path', 'a URL'), mapping={ 'my computer': ['upload'], 'a Vital-IT path': ['vitalit_path'], 'a URL': ['url_path', 'url_up'], }), twf.FileField(id="upload", help_text='Please provide a data'), twf.TextField(id="vitalit_path", label_text="Scratch path", placeholder="/scratch/el/biorepo/dropbox/"), twf.TextField(id="url_path", label_text="File's url", placeholder="http://www..."), twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ", help_text="tick it if you want to download it in BioRepo") ] list_dynamic = [] if lab is None: print "----- no dynamic fields detected ---------" list_fields = [list_static, list_dynamic] return list_fields else: to_clone = DBSession.query(Measurements).filter( Measurements.id == int(id_object)).first() tag = "measurements" if to_clone is not None: list_dynamic_attributes = to_clone.attributs for att in list_dynamic_attributes: if att.deprecated == False: twf_type = convert_widget(att.widget) twf_type.id = att.key list_a_values = att.values if att.widget == "textfield" or att.widget == "textarea" or att.widget == "hiding_textfield" or att.widget == "hiding_textarea": for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: twf_type.value = v.value list_dynamic.append(twf_type) elif att.widget == "checkbox" or att.widget == "hiding_checkbox": for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: #dynamic boolean are stored in varchar in the db, we have to cast them in boolean for the display value_2_display = check_boolean(v.value) twf_type.value = value_2_display list_dynamic.append(twf_type) elif att.widget == "multipleselectfield" or att.widget == "hiding_multipleselectfield": list_possible_values = [] for v in list_a_values: list_possible_values.append(v.value) twf_type.options = list_possible_values selected_values = [] for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: selected_values.append(v.value) twf_type.value = selected_values list_dynamic.append(twf_type) elif att.widget == "singleselectfield" or att.widget == "hiding_singleselectfield": list_possible_values = [] for v in list_a_values: if v.value not in list_possible_values: list_possible_values.append(v.value) twf_type.options = list_possible_values for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: twf_type.value = v.value list_dynamic.append(twf_type) else: print "Your measurement was not found. ID problem. id :", id_object raise list_fields = [list_static, list_dynamic] return list_fields
def new_form_parents(user_lab): '''for new form with parents''' lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() #static lists list_static_samples = [ twf.SingleSelectField(id="project", label_text="Your projects : ", help_text="Select project for this sample", prompt_text=None), twf.TextField(id="name", label_text="Name :", validator=twc.Validator(required=True)), twf.SingleSelectField(id="type", label_text="Type : ", help_text="Technique used", prompt_text=None), twf.TextArea( id="protocole", label_text="Protocole :", ) ] list_static_measurements = [ twf.HiddenField(id="IDselected", label_text="ID selected :"), twf.TextField(id="name", label_text="Name :", placeholder="Measurement name...", validator=twc.Validator(required=True)), twf.TextArea(id="description", label_text="Description :"), twf.MultipleSelectField( id="samples", label_text="Your samples : ", help_text="You can add some of your existing data to this project." ), twf.CheckBox( id="status_type", label_text="Privacy : ", help_text= "Check to have it available from outside EPFL (required for UCSC visualisation)" ), twf.CheckBox(id="type", label_text="Raw data : ", help_text="Check if raw data"), twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."), twd.HidingRadioButtonList(id="upload_way", label_text='Upload my file via...', options=('my computer', 'a Vital-IT path', 'a URL'), mapping={ 'my computer': ['upload'], 'a Vital-IT path': ['vitalit_path'], 'a URL': ['url_path', 'url_up'], }), twf.FileField(id="upload", help_text='Please provide a data'), twf.TextField(id="vitalit_path", label_text="Scratch path", placeholder="/scratch/el/biorepo/dropbox/"), twf.TextField(id="url_path", label_text="File's url", placeholder="http://www..."), twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ", help_text="tick it if you want to download it in BioRepo") ] list_dynamic_samples = [] list_hiding_samples = [] list_dynamic_measurements = [] list_hiding_meas = [] #catch the dynamic hiding fields dic_hiding_meas = session.get("hiding_meas", {}) dic_hiding_samples = session.get("hiding_sample", {}) if lab is None: print "----- no dynamic fields detected ---------" list_fields = [ list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements ] return list_fields else: lab_id = lab.id list_attributs = DBSession.query(Attributs).filter( Attributs.lab_id == lab_id).all() if len(list_attributs) > 0: #lists_construction(list_attributs) for a in list_attributs: attribut_key = a.key deprecated = a.deprecated fixed_value = a.fixed_value widget = a.widget owner = a.owner ############################# ######### NEW SAMPLE ######## ############################# if owner == "sample": #dynamic if not deprecated and fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "multipleselectfield" or widget == "singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_dynamic_samples.append(twf_type) elif widget == "checkbox": list_dynamic_samples.append(twf_type) elif widget == "hiding_singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_hiding_samples.append(twf_type) elif widget == "hiding_checkbox": list_hiding_samples.append(twf_type) else: print widget, "-----ERROR----- ELSE, type samples widget in forms.py" elif not deprecated and not fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "textfield" or widget == "textarea": twf_type.placeholder = "Write here..." list_dynamic_samples.append(twf_type) elif widget == "checkbox": list_dynamic_samples.append(twf_type) elif widget == "hiding_textfield" or widget == "hiding_textarea": twf_type.placeholder = "Write here..." list_hiding_samples.append(twf_type) elif widget == "hiding_checkbox": list_hiding_samples.append(twf_type) else: print widget, "WIDGET SAMPLE NOT FOUND, add an elif please" raise elif deprecated: pass else: print "WIDGET SAMPLES ERROR : widget type is not known --> ", widget raise ################################ ######## NEW MEASUREMENT ####### ################################ elif owner == "measurement": #dynamic #for attributes with fixed values if not deprecated and fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "multipleselectfield" or widget == "singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_dynamic_measurements.append(twf_type) elif widget == "hiding_singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_hiding_meas.append(twf_type) #elif widget == "checkbox": #list_dynamic_measurements.append(twf_type) else: print widget, "-----ERROR----- ELSE, type measurements widget in forms.py" raise #for others attributes elif not deprecated and not fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "textfield" or widget == "textarea": twf_type.placeholder = "Write here..." list_dynamic_measurements.append(twf_type) elif widget == "checkbox": list_dynamic_measurements.append(twf_type) elif widget == "hiding_textfield" or widget == "hiding_textarea": twf_type.placeholder = "Write here..." list_hiding_meas.append(twf_type) elif widget == "hiding_checkbox": list_hiding_meas.append(twf_type) else: print widget, "WIGDET MEASUREMENT NOT FOUND, add an elif please" raise elif deprecated: pass #in bugs case else: print "WIDGET MEASUREMENTS ERROR : widget type is not known --> ", widget raise #TO TEST WITH SEVERAL TWD OBJECTS #build dynamic dynamic fields #samples list_twd_s = [] for k in dic_hiding_samples: twd_object = twd.HidingRadioButtonList() twd_object.id = k dico_mapping = dic_hiding_samples[k] options = [] for key in dico_mapping.keys(): options.append(key) twd_object.options = options twd_object.mapping = dico_mapping list_twd_s.append(twd_object) list_dynamic_samples = list_dynamic_samples + list_twd_s + list_hiding_samples #measurements list_twd_m = [] for k in dic_hiding_meas: twd_object = twd.HidingRadioButtonList() twd_object.id = k dico_mapping = dic_hiding_meas[k] options = [] for key in dico_mapping.keys(): options.append(key) twd_object.options = options twd_object.mapping = dico_mapping list_twd_m.append(twd_object) list_dynamic_measurements = list_dynamic_measurements + list_twd_m + list_hiding_meas list_fields = [ list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements ] return list_fields else: print "-----forms.py----- Houston, we have a problem : The lab ", lab.name, " doesn't get any attributes -----" raise