[hg] galaxy 2584: Improved the form rendering method
details: http://www.bx.psu.edu/hg/galaxy/rev/f3d25adcace6 changeset: 2584:f3d25adcace6 user: rc date: Wed Aug 12 10:31:33 2009 -0400 description: Improved the form rendering method - now supports AddressField 4 file(s) affected in this change: lib/galaxy/web/controllers/admin.py lib/galaxy/web/controllers/forms.py lib/galaxy/web/controllers/library.py lib/galaxy/web/controllers/requests.py diffs (225 lines): diff -r 2630316ff75e -r f3d25adcace6 lib/galaxy/web/controllers/admin.py --- a/lib/galaxy/web/controllers/admin.py Tue Aug 11 16:56:47 2009 -0400 +++ b/lib/galaxy/web/controllers/admin.py Wed Aug 12 10:31:33 2009 -0400 @@ -759,11 +759,7 @@ # See if we have any field contents info = info_association.info if info: - field_contents = {} - for index, value in enumerate( info.content ): - key = 'field_%i' % index - field_contents[ key ] = value - widgets = get_form_widgets( trans, template, field_contents ) + widgets = get_form_widgets( trans, template, info.content ) else: widgets = get_form_widgets( trans, template ) else: @@ -960,11 +956,7 @@ # See if we have any field contents info = info_association.info if info: - field_contents = {} - for index, value in enumerate( info.content ): - key = 'field_%i' % index - field_contents[ key ] = value - widgets = get_form_widgets( trans, template, field_contents ) + widgets = get_form_widgets( trans, template, info.content ) else: widgets = get_form_widgets( trans, template ) else: @@ -1216,11 +1208,7 @@ info = info_association.info log.debug("####In library_dataset_dataset_association, info.content: %s" % str( info.content)) if info: - field_contents = {} - for index, value in enumerate( info.content ): - key = 'field_%i' % index - field_contents[ key ] = value - widgets = get_form_widgets( trans, template, field_contents ) + widgets = get_form_widgets( trans, template, info.content ) else: widgets = get_form_widgets( trans, template ) else: diff -r 2630316ff75e -r f3d25adcace6 lib/galaxy/web/controllers/forms.py --- a/lib/galaxy/web/controllers/forms.py Tue Aug 11 16:56:47 2009 -0400 +++ b/lib/galaxy/web/controllers/forms.py Wed Aug 12 10:31:33 2009 -0400 @@ -419,22 +419,34 @@ else: fdc_list = trans.app.model.FormDefinitionCurrent.query().all() return [ fdc.latest_form for fdc in fdc_list ] -def get_form_widgets( trans, form, contents={} ): + + +def get_form_widgets( trans, form, contents=[], **kwd ): ''' Return the list of widgets that comprise a form definition, including field contents if any. ''' + params = util.Params( kwd ) widgets = [] for index, field in enumerate( form.fields ): field_name = 'field_%i' % index - if field_name in contents: - value = contents[ field_name ] - elif field[ 'type' ] == 'CheckboxField': - # Since we do not have contents, set checkbox value to False - value = False + # determine the value of the field + if field_name in kwd: + # the user had already filled out this field and the same form is re-rendered + # due to some reason like required fields have been left out. + value = util.restore_text( params.get( field_name, '' ) ) + elif contents: + # this field has a saved value + value = str(contents[ index ]) else: - # Set other field types to empty string - value = '' + # if none of the above, then leave the field empty + if field[ 'type' ] == 'CheckboxField': + # Since we do not have contents, set checkbox value to False + value = False + else: + # Set other field types to empty string + value = '' + # create the field widget field_widget = eval( field[ 'type' ] )( field_name ) if field[ 'type' ] == 'TextField': field_widget.set_size( 40 ) @@ -442,6 +454,10 @@ elif field[ 'type' ] == 'TextArea': field_widget.set_size( 3, 40 ) field_widget.value = value + elif field['type'] == 'AddressField': + field_widget.user = trans.user + field_widget.value = value + field_widget.params = params elif field[ 'type' ] == 'SelectField': for option in field[ 'selectlist' ]: if option == value: diff -r 2630316ff75e -r f3d25adcace6 lib/galaxy/web/controllers/library.py --- a/lib/galaxy/web/controllers/library.py Tue Aug 11 16:56:47 2009 -0400 +++ b/lib/galaxy/web/controllers/library.py Wed Aug 12 10:31:33 2009 -0400 @@ -137,11 +137,7 @@ # See if we have any field contents info = library.info_association[0].info if info: - field_contents = {} - for index, value in enumerate( info.content ): - key = 'field_%i' % index - field_contents[ key ] = value - widgets = get_form_widgets( trans, template, field_contents ) + widgets = get_form_widgets( trans, template, info.content ) else: widgets = get_form_widgets( trans, template ) else: @@ -475,11 +471,7 @@ # See if we have any field contents info = info_association.info if info: - field_contents = {} - for index, value in enumerate( info.content ): - key = 'field_%i' % index - field_contents[ key ] = value - widgets = get_form_widgets( trans, template, field_contents ) + widgets = get_form_widgets( trans, template, info.content ) else: widgets = get_form_widgets( trans, template ) else: @@ -996,11 +988,7 @@ # See if we have any field contents info = info_association.info if info: - field_contents = {} - for index, value in enumerate( info.content ): - key = 'field_%i' % index - field_contents[ key ] = value - widgets = get_form_widgets( trans, template, field_contents ) + widgets = get_form_widgets( trans, template, info.content ) else: widgets = get_form_widgets( trans, template ) else: diff -r 2630316ff75e -r f3d25adcace6 lib/galaxy/web/controllers/requests.py --- a/lib/galaxy/web/controllers/requests.py Tue Aug 11 16:56:47 2009 -0400 +++ b/lib/galaxy/web/controllers/requests.py Wed Aug 12 10:31:33 2009 -0400 @@ -8,6 +8,7 @@ from galaxy.web.form_builder import * from datetime import datetime, timedelta from cgi import escape, FieldStorage +from galaxy.web.controllers.forms import get_form_widgets log = logging.getLogger( __name__ ) @@ -464,9 +465,7 @@ libraries = self.get_authorized_libs(trans) libui = self.__library_ui(libraries, **kwd) widgets = widgets + libui - widgets = self.__create_form(trans, request_type.request_form_id, widgets, - form_values, **kwd) - title = 'Add a new request of type: %s' % request_type.name + widgets = widgets + get_form_widgets(trans, request_type.request_form, contents=[], **kwd) return trans.fill_template( '/requests/new_request.mako', select_request_type=select_request_type, request_type=request_type, @@ -507,51 +506,6 @@ return [widget, new_lib] else: return [widget] - - def __create_form(self, trans, form_id, widgets=[], form_values=None, **kwd): - # TODO: RC - replace this method by importing as follows: - # from galaxy.web.controllers.forms import get_form_widgets - params = util.Params( kwd ) - form = trans.app.model.FormDefinition.get(form_id) - # form fields - for index, field in enumerate(form.fields): - # value of the field - if field['type'] == 'CheckboxField': - value = util.restore_text( params.get( 'field_%i' % index, False ) ) - else: - value = util.restore_text( params.get( 'field_%i' % index, '' ) ) - if not value: - if form_values: - value = str(form_values.content[index]) - # create the field - fw = eval(field['type'])('field_%i' % index) - if field['type'] == 'TextField': - fw.set_size(40) - fw.value = value - elif field['type'] == 'TextArea': - fw.set_size(3, 40) - fw.value = value - elif field['type'] == 'AddressField': - fw.user = trans.user - fw.value = value - fw.params = params - elif field['type'] == 'SelectField': - for option in field['selectlist']: - if option == value: - fw.add_option(option, option, selected=True) - else: - fw.add_option(option, option) - elif field['type'] == 'CheckboxField': - fw.checked = value - # require/optional - if field['required'] == 'required': - req = 'Required' - else: - req = 'Optional' - widgets.append(dict(label=field['label'], - widget=fw, - helptext=field['helptext']+' ('+req+')')) - return widgets def __validate(self, trans, request): ''' Validates the request entered by the user @@ -706,8 +660,7 @@ libraries = self.get_authorized_libs(trans) libui = self.__library_ui(libraries, request, **kwd) widgets = widgets + libui - widgets = self.__create_form(trans, request.type.request_form_id, widgets, - request.values, **kwd) + widgets = widgets + get_form_widgets(trans, request.type.request_form, request.values.content, **kwd) return trans.fill_template( '/requests/edit_request.mako', select_request_type=select_request_type, request_type=request.type,
participants (1)
-
Greg Von Kuster