[hg] galaxy 2882: Forms & request controllers
details: http://www.bx.psu.edu/hg/galaxy/rev/ca816e66ec69 changeset: 2882:ca816e66ec69 user: rc date: Wed Oct 14 02:22:11 2009 -0400 description: Forms & request controllers - refactored to work on main - cleanup Added more functional tests 12 file(s) affected in this change: lib/galaxy/model/__init__.py lib/galaxy/web/controllers/forms.py lib/galaxy/web/controllers/requests.py lib/galaxy/web/controllers/requests_admin.py lib/galaxy/web/framework/__init__.py templates/admin/forms/edit_form.mako templates/admin/forms/show_form_read_only.mako templates/admin/requests/show_request.mako templates/requests/show_request.mako templates/user/address.mako test/base/twilltestcase.py test/functional/test_forms_and_requests.py diffs (2308 lines): diff -r b16a6d767e65 -r ca816e66ec69 lib/galaxy/model/__init__.py --- a/lib/galaxy/model/__init__.py Tue Oct 13 18:15:06 2009 -0400 +++ b/lib/galaxy/model/__init__.py Wed Oct 14 02:22:11 2009 -0400 @@ -1090,11 +1090,12 @@ types = Bunch( REQUEST = 'Sequencing Request Form', SAMPLE = 'Sequencing Sample Form', LIBRARY_INFO_TEMPLATE = 'Library information template' ) - def __init__(self, name=None, desc=None, fields=[], current_form=None, form_type=None, layout=None): + def __init__(self, name=None, desc=None, fields=[], + form_definition_current=None, form_type=None, layout=None): self.name = name self.desc = desc self.fields = fields - self.form_definition_current = current_form + self.form_definition_current = form_definition_current self.type = form_type self.layout = layout def fields_of_grid(self, layout_grid_name): @@ -1263,20 +1264,26 @@ self.postal_code = postal_code self.country = country self.phone = phone - def display(self): - return self.name+'<br/>'+ \ - self.institution+'<br/>'+ \ - self.address+'<br/>'+ \ - self.city+' '+self.state+' '+self.postal_code+'<br/>'+ \ - self.country+'<br/>'+ \ - 'Phone: '+self.phone def get_html(self): - return self.name+'<br/>'+ \ - self.institution+'<br/>'+ \ - self.address+'<br/>'+ \ - self.city+' '+self.state+' '+self.postal_code+'<br/>'+ \ - self.country+'<br/>'+ \ - 'Phone: '+self.phone + html = '' + if self.name: + html = html + self.name + if self.institution: + html = html + '<br/>' + self.institution + if self.address: + html = html + '<br/>' + self.address + if self.city: + html = html + '<br/>' + self.city + if self.state: + html = html + ' ' + self.state + if self.postal_code: + html = html + ' ' + self.postal_code + if self.country: + html = html + '<br/>' + self.country + if self.phone: + html = html + '<br/>' + 'Phone: ' + self.phone + return html + class Page( object ): def __init__( self ): diff -r b16a6d767e65 -r ca816e66ec69 lib/galaxy/web/controllers/forms.py --- a/lib/galaxy/web/controllers/forms.py Tue Oct 13 18:15:06 2009 -0400 +++ b/lib/galaxy/web/controllers/forms.py Wed Oct 14 02:22:11 2009 -0400 @@ -12,6 +12,14 @@ log = logging.getLogger( __name__ ) class Forms( BaseController ): + # Empty form field + empty_field = { 'label': '', + 'helptext': '', + 'visible': True, + 'required': False, + 'type': BaseField.form_field_types()[0], + 'selectlist': [], + 'layout': 'none' } @web.expose @web.require_admin def index( self, trans, **kwd ): @@ -84,19 +92,12 @@ action='edit', form_id=fd.id, add_field_button='Add field', - num_fields=0, name=fd.name, description=fd.desc, form_type_selectbox=fd.type ) ) - self.current_form = {} - self.current_form[ 'name' ] = 'New Form' - self.current_form[ 'desc' ] = '' - self.current_form[ 'type' ] = params.get( 'form_type', 'none' ) - self.current_form[ 'layout' ] = [ 'Main' ] - self.current_form[ 'fields' ] = [] - inputs = [ ( 'Name', TextField( 'name', 40, self.current_form[ 'name' ] ) ), - ( 'Description', TextField( 'description', 40, self.current_form[ 'desc' ] ) ), - ( 'Type', self.__form_types_widget(trans, selected=self.current_form['type']) ), + inputs = [ ( 'Name', TextField( 'name', 40, 'New Form' ) ), + ( 'Description', TextField( 'description', 40, '' ) ), + ( 'Type', self.__form_types_widget(trans, selected=params.get( 'form_type', 'none' )) ), ( 'Import from csv file (Optional)', FileField( 'file_data', 40, '' ) ) ] return trans.fill_template( '/admin/forms/create_form.mako', inputs=inputs, @@ -134,102 +135,156 @@ renaming fields, adding/deleting fields, changing fields attributes. ''' params = util.Params( kwd ) + log.debug( kwd ) msg = util.restore_text( params.get( 'msg', '' ) ) messagetype = params.get( 'messagetype', 'done' ) - form_id = params.get( 'form_id', None ) - if not form_id: - msg = 'Invalid form id %s' % str( form_id ) - trans.response.send_redirect( web.url_for( controller='forms', - action='manage', - msg=msg, - messagetype='error' ) ) - fd = trans.app.model.FormDefinition.get( int( params.form_id ) ) + try: + fd = trans.app.model.FormDefinition.get( int( params.get( 'form_id', None ) ) ) + except: + return trans.response.send_redirect( web.url_for( controller='forms', + action='manage', + msg='Invalid form', + messagetype='error' ) ) + # # Show the form for editing + # if params.get( 'show_form', False ): - self.__get_saved_form( fd ) - # The following two dicts store the unsaved select box options - self.del_options = {} - self.add_options = {} - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) - #Add a layout grid + current_form = self.__get_saved_form( fd ) + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + # + # Add a layout grid + # elif params.get( 'add_layout_grid', False ): - self.__update_current_form( trans, **kwd ) - self.__add_layout_grid() + current_form = self.__get_form( trans, **kwd ) + current_form['layout'].append('') # show the form again - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + # # Delete a layout grid + # elif params.get( 'remove_layout_grid_button', False ): - self.__update_current_form( trans, **kwd ) + current_form = self.__get_form( trans, **kwd ) index = int( kwd[ 'remove_layout_grid_button' ].split( ' ' )[2] ) - 1 - self.__remove_layout_grid( index ) - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) + del current_form['layout'][index] + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + # + # Add a field + # + elif params.get( 'add_field_button', False ): + current_form = self.__get_form( trans, **kwd ) + current_form['fields'].append( self.empty_field ) + # show the form again with one empty field + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + # # Delete a field + # elif params.get( 'remove_button', False ): - self.__update_current_form( trans, **kwd ) + current_form = self.__get_form( trans, **kwd ) + # find the index of the field to be removed from the remove button label index = int( kwd[ 'remove_button' ].split( ' ' )[2] ) - 1 - self.__remove_field( index ) - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) + del current_form['fields'][index] + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + # # Save changes + # elif params.get( 'save_changes_button', False ): - self.__update_current_form( trans, **kwd ) - fd_new, msg = self.__save_form( trans, fd.form_definition_current.id, **kwd ) + fd_new, msg = self.__save_form( trans, fdc_id=fd.form_definition_current.id, **kwd ) + # if validation error encountered while saving the form, show the + # unsaved form, with the error message if not fd_new: - return self.__show( trans=trans, form=fd, msg=msg, messagetype='error', **kwd ) - else: - fd = fd_new + current_form = self.__get_form( trans, **kwd ) + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype='error', **kwd ) + # everything went fine. form saved successfully. Show the saved form + fd = fd_new + current_form = self.__get_saved_form( fd ) msg = "The form '%s' has been updated with the changes." % fd.name - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) - #Add a field - elif params.get( 'add_field_button', False ): - self.__update_current_form( trans, **kwd ) - self.__add_field() - # show the form again with one empty field - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + # # Show form read-only + # elif params.get( 'read_only', False ): return trans.fill_template( '/admin/forms/show_form_read_only.mako', form=fd, msg=msg, messagetype=messagetype ) - # Refresh page, SelectField is selected/deselected as the type of a field + # + # Add SelectField option + # + elif 'Add' in kwd.values(): + return self.__add_selectbox_option(trans, fd, msg, messagetype, **kwd) + # + # Remove SelectField option + # + elif 'Remove' in kwd.values(): + return self.__remove_selectbox_option(trans, fd, msg, messagetype, **kwd) + # + # Refresh page + # elif params.get( 'refresh', False ): - self.__update_current_form( trans, **kwd ) - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) - # Remove SelectField option - elif params.get( 'select_box_options', False ) == 'remove': - index = int( kwd[ 'field_index' ] ) - option = int( kwd[ 'option_index' ] ) - del self.current_form[ 'fields' ][ index ][ 'selectlist' ][ option ] - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) - # Add SelectField option - elif params.get( 'select_box_options', False ) == 'add': - index = int( kwd[ 'field_index' ] ) - self.current_form[ 'fields' ][ index ][ 'selectlist' ].append( '' ) - return self.__show( trans=trans, form=fd, msg=msg, messagetype=messagetype, **kwd ) - def __add_layout_grid(self): - self.current_form['layout'].append('') - def __remove_layout_grid(self, index): - del self.current_form['layout'][index] - def __remove_field(self, index): - del self.current_form['fields'][index] - def __add_field(self): + current_form = self.__get_form( trans, **kwd ) + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + + def __add_selectbox_option( self, trans, fd, msg, messagetype, **kwd ): ''' - add an empty field to the fields list + This method adds a selectbox option. The kwd dict searched for + the field index which needs to be removed ''' - empty_field = { 'label': '', - 'helptext': '', - 'visible': True, - 'required': False, - 'type': BaseField.form_field_types()[0], - 'selectlist': [], - 'layout': 'none' } - self.current_form['fields'].append(empty_field) + current_form = self.__get_form( trans, **kwd ) + index = -1 + for k, v in kwd.items(): + if v == 'Add': + # extract the field index from the + # button name of format: 'addoption_<field>' + index = int(k.split('_')[1]) + break + if index == -1: + # something wrong happened + return self.__show( trans=trans, form=fd, current_form=current_form, + msg='Error in adding selectfield option', + messagetype='error', **kwd ) + # add an empty option + current_form[ 'fields' ][ index ][ 'selectlist' ].append( '' ) + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + def __remove_selectbox_option( self, trans, fd, msg, messagetype, **kwd ): + ''' + This method removes a selectbox option. The kwd dict searched for + the field index and option index which needs to be removed + ''' + current_form = self.__get_form( trans, **kwd ) + option = -1 + for k, v in kwd.items(): + if v == 'Remove': + # extract the field & option indices from the + # button name of format: 'removeoption_<field>_<option>' + index = int(k.split('_')[1]) + option = int(k.split('_')[2]) + break + if option == -1: + # something wrong happened + return self.__show( trans=trans, form=fd, current_form=current_form, + msg='Error in removing selectfield option', + messagetype='error', **kwd ) + # remove the option + del current_form[ 'fields' ][ index ][ 'selectlist' ][ option ] + return self.__show( trans=trans, form=fd, current_form=current_form, + msg=msg, messagetype=messagetype, **kwd ) + + def __get_field(self, index, **kwd): + ''' + This method retrieves all the user-entered details of a field and + returns a dict. + ''' params = util.Params( kwd ) - #TODO: RC this needs to be handled so that it does not throw an exception. - # To reproduce, create a new form, click the "add field" button, click the - # browser back arrow, then click the "add field" button again. - # You should never attempt to "restore_text()" on a None object... name = util.restore_text( params.get( 'field_name_%i' % index, '' ) ) helptext = util.restore_text( params.get( 'field_helptext_%i' % index, '' ) ) required = params.get( 'field_required_%i' % index, False ) @@ -266,57 +321,54 @@ else: return sb_options def __get_saved_form(self, fd): - self.current_form = {} - self.current_form['name'] = fd.name - self.current_form['desc'] = fd.desc - self.current_form['type'] = fd.type - self.current_form['layout'] = list(copy.deepcopy(fd.layout)) - self.current_form['fields'] = list(copy.deepcopy(fd.fields)) - def __validate_form(self, **kwd): ''' - This method checks the following text inputs are filled out by the user - - the name of form - - name of all the fields + This retrieves the saved form and returns a dictionary containing the name, + desc, type, layout & fields of the form ''' - params = util.Params( kwd ) - # form name - if not util.restore_text( params.name ): - return None, 'Form name must be filled.' - # form type - if util.restore_text( params.form_type_selectbox ) == 'none': - return None, 'Form type must be selected.' - # fields -# for i in range( len(self.current_form['fields']) ): -# if not util.restore_text(params.get( 'field_name_%i' % i, None )): -# return None, "All the field label(s) must be completed." - return True, '' + return dict(name = fd.name, + desc = fd.desc, + type = fd.type, + layout = list(copy.deepcopy(fd.layout)), + fields = list(copy.deepcopy(fd.fields))) def __get_form(self, trans, **kwd): + ''' + This method gets all the user-entered form details and returns a + dictionary containing the name, desc, type, layout & fields of the form + ''' params = util.Params( kwd ) name = util.restore_text( params.name ) desc = util.restore_text( params.description ) or "" form_type = util.restore_text( params.form_type_selectbox ) + # get the user entered layout grids layout = [] - if form_type == trans.app.model.FormDefinition.types.SAMPLE: - for index in range(len(self.current_form[ 'layout' ])): - layout.append(params.get( 'grid_layout%i' % index, '' )) + index = 0 + while True: + grid_name = util.restore_text( params.get( 'grid_layout%i' % index, '' ) ) + if grid_name: + layout.append( grid_name ) + index = index + 1 + else: + break + # for csv file import csv_file = params.get( 'file_data', '' ) + fields = [] if csv_file == '': - # set form fields - fields = [] - for i in range( len(self.current_form['fields']) ): - fields.append(self.__get_field(i, **kwd)) + # get the user entered fields + index = 0 + while True: + if params.get( 'field_name_%i' % index, False ): + fields.append( self.__get_field( index, **kwd ) ) + index = index + 1 + else: + break fields = fields else: fields, layout = self.__import_fields(trans, csv_file, form_type) - return name, desc, form_type, layout, fields - def __update_current_form(self, trans, **kwd): - name, desc, form_type, layout, fields = self.__get_form(trans, **kwd) - self.current_form = {} - self.current_form['name'] = name - self.current_form['desc'] = desc - self.current_form['type'] = form_type - self.current_form['layout'] = layout - self.current_form['fields'] = fields + return dict(name = name, + desc = desc, + type = form_type, + layout = layout, + fields = fields) def __import_fields(self, trans, csv_file, form_type): ''' @@ -356,7 +408,20 @@ **kwd)) self.__imported_from_file = True return fields, list(layouts) - + def __validate_form(self, **kwd): + ''' + This method checks the following text inputs are filled out by the user + - the name of form + - form type + ''' + params = util.Params( kwd ) + # form name + if not util.restore_text( params.name ): + return None, 'Form name must be filled.' + # form type + if util.restore_text( params.form_type_selectbox ) == 'none': + return None, 'Form type must be selected.' + return True, '' def __save_form(self, trans, fdc_id=None, **kwd): ''' This method saves the current form @@ -365,13 +430,18 @@ flag, msg = self.__validate_form(**kwd) if not flag: return None, msg - name, desc, form_type, layout, fields = self.__get_form(trans, **kwd) + current_form = self.__get_form( trans, **kwd ) # validate fields - for field in fields: + for field in current_form[ 'fields' ]: if not field[ 'label' ]: return None, "All the field label(s) must be completed." - fd = trans.app.model.FormDefinition(name, desc, fields, current_form=None, - form_type=form_type, layout=layout ) + # create a new form definition + fd = trans.app.model.FormDefinition(name=current_form[ 'name' ], + desc=current_form[ 'desc' ], + fields=current_form[ 'fields' ], + form_definition_current=None, + form_type=current_form[ 'type' ], + layout=current_form[ 'layout' ] ) if fdc_id: # save changes to the existing form # change the pointer in the form_definition_current table to point # to this new record @@ -467,7 +537,7 @@ def label(self): return str(self.index)+'.'+self.label - def __show( self, trans, form, msg='', messagetype='done', **kwd ): + def __show( self, trans, form, current_form, msg='', messagetype='done', **kwd ): ''' This method displays the form and any of the changes made to it, The empty_form param allows for this method to simulate clicking @@ -476,18 +546,18 @@ ''' params = util.Params( kwd ) # name & description - form_details = [ ( 'Name', TextField( 'name', 40, self.current_form[ 'name' ] ) ), - ( 'Description', TextField( 'description', 40, self.current_form[ 'desc' ] ) ), - ( 'Type', self.__form_types_widget(trans, selected=self.current_form['type']) ) ] + form_details = [ ( 'Name', TextField( 'name', 40, current_form[ 'name' ] ) ), + ( 'Description', TextField( 'description', 40, current_form[ 'desc' ] ) ), + ( 'Type', self.__form_types_widget(trans, selected=current_form[ 'type' ]) ) ] form_layout = [] - if self.current_form['type'] == trans.app.model.FormDefinition.types.SAMPLE: - for index, lg in enumerate(self.current_form['layout']): + if current_form[ 'type' ] == trans.app.model.FormDefinition.types.SAMPLE: + for index, lg in enumerate(current_form[ 'layout' ]): form_layout.append( TextField( 'grid_layout%i' % index, 40, lg )) # fields field_details = [] - for index, field in enumerate( self.current_form[ 'fields' ] ): - if self.current_form['type'] == trans.app.model.FormDefinition.types.SAMPLE: - field_ui = self.FieldUI( self.current_form['layout'], index, field ) + for index, field in enumerate( current_form[ 'fields' ] ): + if current_form['type'] == trans.app.model.FormDefinition.types.SAMPLE: + field_ui = self.FieldUI( current_form['layout'], index, field ) else: field_ui = self.FieldUI( None, index, field ) field_details.append( field_ui.get() ) @@ -498,7 +568,7 @@ field_types=BaseField.form_field_types(), msg=msg, messagetype=messagetype, - current_form_type=self.current_form['type'], + current_form_type=current_form[ 'type' ], layout_grids=form_layout ) # Common methods for all components that use forms diff -r b16a6d767e65 -r ca816e66ec69 lib/galaxy/web/controllers/requests.py --- a/lib/galaxy/web/controllers/requests.py Tue Oct 13 18:15:06 2009 -0400 +++ b/lib/galaxy/web/controllers/requests.py Wed Oct 14 02:22:11 2009 -0400 @@ -115,20 +115,17 @@ status='error', message="Invalid request ID", **kwd) ) - self.current_samples = [] - self.edit_mode = False + current_samples = [] for s in request.samples: - self.current_samples.append([s.name, s.values.content]) + current_samples.append([s.name, s.values.content]) if add_sample: - self.current_samples.append(['Sample_%i' % (len(self.current_samples)+1),['' for field in request.type.sample_form.fields]]) - self.details_state = 'Show request details' + current_samples.append(['Sample_%i' % (len(current_samples)+1),['' for field in request.type.sample_form.fields]]) return trans.fill_template( '/requests/show_request.mako', request=request, request_details=self.request_details(trans, id), - current_samples = self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details='hide', edit_mode='False') def request_details(self, trans, id): ''' Shows the request details @@ -186,25 +183,34 @@ value=request.values.content[index], helptext=field['helptext']+' ('+req+')')) return request_details - def __update_samples(self, request, **kwd): + ''' + This method retrieves all the user entered sample information and + returns an list of all the samples and their field values + ''' params = util.Params( kwd ) - num_samples = len(self.current_samples) - self.current_samples = [] + current_samples = [] for s in request.samples: - self.current_samples.append([s.name, s.values.content]) - for index in range(num_samples-len(request.samples)): - sample_index = index + len(request.samples) - sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) - sample_values = [] - for field_index in range(len(request.type.sample_form.fields)): - sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) - self.current_samples.append([sample_name, sample_values]) - - def __copy_sample(self): + current_samples.append([s.name, s.values.content]) + index = len(request.samples) + while True: + if params.get( 'sample_%i_name' % index, '' ): + sample_index = index + sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) + sample_values = [] + for field_index in range(len(request.type.sample_form.fields)): + sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) + current_samples.append([sample_name, sample_values]) + index = index + 1 + else: + break + details = params.get( 'details', 'hide' ) + edit_mode = params.get( 'edit_mode', 'False' ) + return current_samples, details, edit_mode + def __copy_sample(self, current_samples): copy_list = SelectField('copy_sample') copy_list.add_option('None', -1, selected=True) - for i, s in enumerate(self.current_samples): + for i, s in enumerate(current_samples): copy_list.add_option(s[0], i) return copy_list @web.expose @@ -221,20 +227,22 @@ status='error', message="Invalid request ID", **kwd) ) + # get the user entered sample details + current_samples, details, edit_mode = self.__update_samples( request, **kwd ) if params.get('import_samples_button', False) == 'Import samples': try: file_obj = params.get('file_data', '') import csv reader = csv.reader(file_obj.file) for row in reader: - self.current_samples.append([row[0], row[1:]]) + current_samples.append([row[0], row[1:]]) return trans.fill_template( '/requests/show_request.mako', request=request, request_details=self.request_details(trans, request.id), - current_samples=self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) + current_samples=current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) except: return trans.response.send_redirect( web.url_for( controller='requests', action='list', @@ -242,39 +250,35 @@ message='Error in importing samples file', **kwd)) elif params.get('add_sample_button', False) == 'Add New': - # save the all (saved+unsaved) sample info in 'current_samples' - self.__update_samples(request, **kwd) # add an empty or filled sample # if the user has selected a sample no. to copy then copy the contents # of the src sample to the new sample else an empty sample src_sample_index = int(params.get( 'copy_sample', -1 )) if src_sample_index == -1: # empty sample - self.current_samples.append(['Sample_%i' % (len(self.current_samples)+1),['' for field in request.type.sample_form.fields]]) + current_samples.append(['Sample_%i' % (len(current_samples)+1),['' for field in request.type.sample_form.fields]]) else: - self.current_samples.append([self.current_samples[src_sample_index][0]+'_%i' % (len(self.current_samples)+1), - [val for val in self.current_samples[src_sample_index][1]]]) + current_samples.append([current_samples[src_sample_index][0]+'_%i' % (len(current_samples)+1), + [val for val in current_samples[src_sample_index][1]]]) return trans.fill_template( '/requests/show_request.mako', request=request, request_details=self.request_details(trans, request.id), - current_samples=self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) + current_samples=current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) elif params.get('save_samples_button', False) == 'Save': - # update current_samples - self.__update_samples(request, **kwd) # check for duplicate sample names msg = '' - for index in range(len(self.current_samples)-len(request.samples)): + for index in range(len(current_samples)-len(request.samples)): sample_index = index + len(request.samples) - sample_name = self.current_samples[sample_index][0] + sample_name = current_samples[sample_index][0] if not sample_name.strip(): msg = 'Please enter the name of sample number %i' % sample_index break count = 0 - for i in range(len(self.current_samples)): - if sample_name == self.current_samples[i][0]: + for i in range(len(current_samples)): + if sample_name == current_samples[i][0]: count = count + 1 if count > 1: msg = "This request has <b>%i</b> samples with the name <b>%s</b>.\nSamples belonging to a request must have unique names." % (count, sample_name) @@ -283,12 +287,13 @@ return trans.fill_template( '/requests/show_request.mako', request=request, request_details=self.request_details(trans, request.id), - current_samples = self.current_samples, - sample_copy=self.__copy_sample(), details_state=self.details_state, + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, edit_mode=edit_mode, messagetype='error', msg=msg) # save all the new/unsaved samples entered by the user - if not self.edit_mode: - for index in range(len(self.current_samples)-len(request.samples)): + if edit_mode == 'False': + for index in range(len(current_samples)-len(request.samples)): sample_index = index + len(request.samples) sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) sample_values = [] @@ -299,9 +304,9 @@ s = trans.app.model.Sample(sample_name, '', request, form_values) s.flush() else: - for index in range(len(self.current_samples)): + for index in range(len(current_samples)): sample_index = index - sample_name = self.current_samples[sample_index][0] + sample_name = current_samples[sample_index][0] new_sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) sample_values = [] for field_index in range(len(request.type.sample_form.fields)): @@ -318,14 +323,14 @@ operation='show_request', id=trans.security.encode_id(request.id)) ) elif params.get('edit_samples_button', False) == 'Edit samples': - self.edit_mode = True + edit_mode = 'True' return trans.fill_template( '/requests/show_request.mako', request=request, request_details=self.request_details(trans, request.id), - current_samples=self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) + current_samples=current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) elif params.get('cancel_changes_button', False) == 'Cancel': return trans.response.send_redirect( web.url_for( controller='requests', action='list', @@ -340,21 +345,22 @@ msg = util.restore_text( params.get( 'msg', '' ) ) messagetype = params.get( 'messagetype', 'done' ) request = trans.app.model.Request.get(int(params.get('request_id', 0))) + current_samples, details, edit_mode = self.__update_samples( request, **kwd ) sample_index = int(params.get('sample_id', 0)) - sample_name = self.current_samples[sample_index][0] + sample_name = current_samples[sample_index][0] s = request.has_sample(sample_name) if s: s.delete() s.flush() request.flush() - del self.current_samples[sample_index] + del current_samples[sample_index] return trans.fill_template( '/requests/show_request.mako', request=request, request_details=self.request_details(trans, request.id), - current_samples = self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) @web.expose @web.require_login( "create/submit sequencing requests" ) @@ -363,17 +369,14 @@ msg = util.restore_text( params.get( 'msg', '' ) ) messagetype = params.get( 'messagetype', 'done' ) request = trans.app.model.Request.get(int(params.get('request_id', 0))) - if self.details_state == 'Show request details': - self.details_state = 'Hide request details' - elif self.details_state == 'Hide request details': - self.details_state = 'Show request details' + current_samples, details, edit_mode = self.__update_samples( request, **kwd ) return trans.fill_template( '/requests/show_request.mako', request=request, request_details=self.request_details(trans, request.id), - current_samples = self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) def __select_request_type(self, trans, rtid): rt_ids = ['none'] for rt in trans.app.model.RequestType.query().all(): @@ -476,17 +479,22 @@ msg=msg, messagetype=messagetype) def __library_ui(self, trans, request=None, **kwd): + ''' + This method creates the data library & folder selectbox for new & + editing requests. First we get a list of all the libraries accessible to + the current user and display it in a selectbox. If the user has select an + existing library then display all the accessible sub folders of the selected + data library. + ''' params = util.Params( kwd ) lib_id = params.get( 'library_id', 'none' ) - # if editing a request selected_lib = None + # if editing a request and the user has already associated a library to + # this request, then set the selected_lib to the request.library if request and lib_id == 'none': if request.library: lib_id = str(request.library.id) selected_lib = request.library - else: - # new request - selected_lib = None # get all permitted libraries for this user all_libraries = trans.app.model.Library.filter( trans.app.model.Library.table.c.deleted == False ) \ .order_by( trans.app.model.Library.name ).all() @@ -497,15 +505,16 @@ can_show, hidden_folder_ids = trans.app.security_agent.show_library_item( user, roles, library, actions_to_check ) if can_show: libraries[ library ] = hidden_folder_ids + # create data library selectbox with refresh on change enabled lib_id_list = ['new'] + [str(lib.id) for lib in libraries.keys()] lib_list = SelectField( 'library_id', refresh_on_change=True, refresh_on_change_values=lib_id_list ) - # fill up the options in the Library selectfield - # first option + # fill up the options in the Library selectbox + # first option 'none' is the value for "Select one" option if lib_id == 'none': lib_list.add_option('Select one', 'none', selected=True) else: lib_list.add_option('Select one', 'none') - # all the libraries available to the user + # add all the libraries available to the user to the library selectbox for lib, hidden_folder_ids in libraries.items(): if str(lib.id) == lib_id: lib_list.add_option(lib.name, lib.id, selected=True) @@ -524,14 +533,21 @@ helptext='Data library where the resultant dataset will be stored.') # show the folder widget only if the user has selected a valid library above if selected_lib: - # when editing a request + # when editing a request, either the user has already selected a subfolder or not if request: if request.folder: current_fid = request.folder.id - else: - current_fid = request.library.root_folder.id + else: + # when a folder not yet associated with the request then the + # the current folder is set to the root_folder of the + # parent data library if present. + if request.library: + current_fid = request.library.root_folder.id + else: + current_fid = params.get( 'folder_id', 'none' ) else: current_fid = params.get( 'folder_id', 'none' ) + # create the folder selectbox folder_list = SelectField( 'folder_id') # first option if lib_id == 'none': @@ -543,6 +559,7 @@ selected_lib, actions_to_check, selected_hidden_folder_ids ) + # add all the folders to the folder selectbox for f in showable_folders: if str(f.id) == str(current_fid): folder_list.add_option(f.name, f.id, selected=True) @@ -568,8 +585,6 @@ Validates the request entered by the user ''' empty_fields = [] -# if not request.library: -# empty_fields.append('Data library') # check rest of the fields of the form for index, field in enumerate(request.type.request_form.fields): if field['required'] == 'required' and request.values.content[index] in ['', None]: diff -r b16a6d767e65 -r ca816e66ec69 lib/galaxy/web/controllers/requests_admin.py --- a/lib/galaxy/web/controllers/requests_admin.py Tue Oct 13 18:15:06 2009 -0400 +++ b/lib/galaxy/web/controllers/requests_admin.py Wed Oct 14 02:22:11 2009 -0400 @@ -10,6 +10,10 @@ from galaxy.web.controllers.forms import get_all_forms log = logging.getLogger( __name__ ) + +# +# ---- Request Grid ------------------------------------------------------------ +# class RequestsListGrid( grids.Grid ): title = "Sequencing Requests" @@ -60,6 +64,11 @@ else: return query + +# +# ---- Request Controller ------------------------------------------------------ +# + class Requests( BaseController ): request_grid = RequestsListGrid() @@ -67,6 +76,7 @@ @web.require_admin def index( self, trans ): return trans.fill_template( "/admin/requests/index.mako" ) + @web.expose @web.require_admin def list( self, trans, **kwargs ): @@ -104,51 +114,26 @@ self.request_grid.show_filter = kwargs.get('show_filter', trans.app.model.Request.states.SUBMITTED) # Render the list view return self.request_grid( trans, **kwargs ) - @web.expose - @web.require_admin - def edit(self, trans, **kwd): - params = util.Params( kwd ) - msg = util.restore_text( params.get( 'msg', '' ) ) - messagetype = params.get( 'messagetype', 'done' ) + def __show_request(self, trans, id, messagetype, msg): try: - request = trans.app.model.Request.get(int(params.get('request_id', None))) + request = trans.app.model.Request.get(id) except: return trans.response.send_redirect( web.url_for( controller='requests_admin', action='list', status='error', message="Invalid request ID", **kwd) ) - if params.get('show', False) == 'True': - return self.__edit_request(trans, request.id, **kwd) - elif params.get('save_changes_request_button', False) == 'Save changes' \ - or params.get('edit_samples_button', False) == 'Edit samples': - request_type = trans.app.model.RequestType.get(int(params.select_request_type)) - if not util.restore_text(params.get('name', '')): - msg = 'Please enter the <b>Name</b> of the request' - kwd['messagetype'] = 'error' - kwd['msg'] = msg - kwd['show'] = 'True' - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='edit', - **kwd) ) - request = self.__save_request(trans, request, **kwd) - msg = 'The changes made to the request named %s has been saved' % request.name - if params.get('save_changes_request_button', False) == 'Save changes': - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - message=msg , - status='done') ) - elif params.get('edit_samples_button', False) == 'Edit samples': - new_kwd = {} - new_kwd['request_id'] = request.id - new_kwd['edit_samples_button'] = 'Edit samples' - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='show_request', - msg=msg , - messagetype='done', - **new_kwd) ) - elif params.get('refresh', False) == 'true': - return self.__edit_request(trans, request.id, **kwd) + current_samples = [] + for s in request.samples: + current_samples.append([s.name, s.values.content]) + return trans.fill_template( '/admin/requests/show_request.mako', + request=request, + request_details=self.request_details(trans, id), + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details='hide', edit_mode='False', + msg=msg, messagetype=messagetype) + def __edit_request(self, trans, id, **kwd): try: request = trans.app.model.Request.get(id) @@ -276,317 +261,10 @@ action='list', show_filter=trans.app.model.Request.states.SUBMITTED, **kwd) ) - @web.expose - @web.require_admin - def submit_request(self, trans, **kwd): - params = util.Params( kwd ) - try: - id = int(params.get('id', False)) - request = trans.app.model.Request.get(id) - except: - msg = "Invalid request ID" - log.warn( msg ) - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - status='error', - message=msg, - **kwd) ) - msg = self.__validate(trans, request) - if msg: - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='edit', - messagetype='error', - msg=msg, - request_id=request.id, - show='True') ) - # get the new state - new_state = request.type.states[0] - for s in request.samples: - event = trans.app.model.SampleEvent(s, new_state, 'Samples submitted to the system') - event.flush() - # change request's submitted field - request.state = request.states.SUBMITTED - request.flush() - kwd['id'] = trans.security.encode_id(request.id) - kwd['status'] = 'done' - kwd['message'] = 'The request <b>%s</b> has been submitted.' % request.name - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - show_filter=trans.app.model.Request.states.SUBMITTED, - **kwd) ) - def __copy_sample(self): - copy_list = SelectField('copy_sample') - copy_list.add_option('None', -1, selected=True) - for i, s in enumerate(self.current_samples): - copy_list.add_option(s[0], i) - return copy_list - def __update_samples(self, request, **kwd): - params = util.Params( kwd ) - num_samples = len(self.current_samples) - self.current_samples = [] - for s in request.samples: - self.current_samples.append([s.name, s.values.content]) - for index in range(num_samples-len(request.samples)): - sample_index = index + len(request.samples) - sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) - sample_values = [] - for field_index in range(len(request.type.sample_form.fields)): - sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) - self.current_samples.append([sample_name, sample_values]) - def __show_request(self, trans, id, messagetype, msg): - try: - request = trans.app.model.Request.get(id) - except: - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - status='error', - message="Invalid request ID", - **kwd) ) - self.current_samples = [] - self.edit_mode = False - for s in request.samples: - self.current_samples.append([s.name, s.values.content]) - self.details_state = 'Show request details' - return trans.fill_template( '/admin/requests/show_request.mako', - request=request, - request_details=self.request_details(trans, id), - current_samples = self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode, - msg=msg, messagetype=messagetype) - @web.expose - @web.require_admin - def show_request(self, trans, **kwd): - params = util.Params( kwd ) - msg = util.restore_text( params.get( 'msg', '' ) ) - messagetype = params.get( 'messagetype', 'done' ) - try: - request = trans.app.model.Request.get(int(params.get('request_id', None))) - except: - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - status='error', - message="Invalid request ID", - **kwd) ) - if params.get('import_samples_button', False) == 'Import samples': - try: - file_obj = params.get('file_data', '') - import csv - reader = csv.reader(file_obj.file) - for row in reader: - self.current_samples.append([row[0], row[1:]]) - return trans.fill_template( '/admin/requests/show_request.mako', - request=request, - request_details=self.request_details(trans, request.id), - current_samples=self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) - except: - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - status='error', - operation='show_request', - id=trans.security.encode_id(request.id), - message='Error in importing samples from the given file.', - **kwd)) - elif params.get('add_sample_button', False) == 'Add New': - # save the all (saved+unsaved) sample info in 'current_samples' - self.__update_samples(request, **kwd) - # add an empty or filled sample - # if the user has selected a sample no. to copy then copy the contents - # of the src sample to the new sample else an empty sample - src_sample_index = int(params.get( 'copy_sample', -1 )) - if src_sample_index == -1: - # empty sample - self.current_samples.append(['Sample_%i' % (len(self.current_samples)+1),['' for field in request.type.sample_form.fields]]) - else: - self.current_samples.append([self.current_samples[src_sample_index][0]+'_%i' % (len(self.current_samples)+1), - [val for val in self.current_samples[src_sample_index][1]]]) - return trans.fill_template( '/admin/requests/show_request.mako', - request=request, - request_details=self.request_details(trans, request.id), - current_samples=self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) - elif params.get('save_samples_button', False) == 'Save': - # update current_samples - self.__update_samples(request, **kwd) - # check for duplicate sample names - msg = '' - for index in range(len(self.current_samples)-len(request.samples)): - sample_index = index + len(request.samples) - sample_name = self.current_samples[sample_index][0] - if not sample_name.strip(): - msg = 'Please enter the name of sample number %i' % sample_index - break - count = 0 - for i in range(len(self.current_samples)): - if sample_name == self.current_samples[i][0]: - count = count + 1 - if count > 1: - msg = "This request has <b>%i</b> samples with the name <b>%s</b>.\nSamples belonging to a request must have unique names." % (count, sample_name) - break - if msg: - return trans.fill_template( '/admin/requests/show_request.mako', - request=request, - request_details=self.request_details(trans, request.id), - current_samples = self.current_samples, - sample_copy=self.__copy_sample(), details_state=self.details_state, - messagetype='error', msg=msg) - # save all the new/unsaved samples entered by the user - if not self.edit_mode: - for index in range(len(self.current_samples)-len(request.samples)): - sample_index = index + len(request.samples) - sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) - sample_values = [] - for field_index in range(len(request.type.sample_form.fields)): - sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) - form_values = trans.app.model.FormValues(request.type.sample_form, sample_values) - form_values.flush() - s = trans.app.model.Sample(sample_name, '', request, form_values) - s.flush() - else: - for index in range(len(self.current_samples)): - sample_index = index - sample_name = self.current_samples[sample_index][0] - new_sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) - sample_values = [] - for field_index in range(len(request.type.sample_form.fields)): - sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) - sample = request.has_sample(sample_name) - if sample: - form_values = trans.app.model.FormValues.get(sample.values.id) - form_values.content = sample_values - form_values.flush() - sample.name = new_sample_name - sample.flush() - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - operation='show_request', - id=trans.security.encode_id(request.id)) ) - elif params.get('edit_samples_button', False) == 'Edit samples': - self.edit_mode = True - return trans.fill_template( '/admin/requests/show_request.mako', - request=request, - request_details=self.request_details(trans, request.id), - current_samples=self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) - elif params.get('cancel_changes_button', False) == 'Cancel': - return trans.response.send_redirect( web.url_for( controller='requests_admin', - action='list', - operation='show_request', - id=trans.security.encode_id(request.id)) ) - - @web.expose - @web.require_admin - def delete_sample(self, trans, **kwd): - params = util.Params( kwd ) - msg = util.restore_text( params.get( 'msg', '' ) ) - messagetype = params.get( 'messagetype', 'done' ) - request = trans.app.model.Request.get(int(params.get('request_id', 0))) - sample_index = int(params.get('sample_id', 0)) - sample_name = self.current_samples[sample_index][0] - s = request.has_sample(sample_name) - if s: - s.delete() - s.flush() - request.flush() - del self.current_samples[sample_index] - return trans.fill_template( '/admin/requests/show_request.mako', - request=request, - request_details=self.request_details(trans, request.id), - current_samples = self.current_samples, - sample_copy=self.__copy_sample(), - details_state=self.details_state, - edit_mode=self.edit_mode) - - @web.expose - @web.require_admin - def toggle_request_details(self, trans, **kwd): - params = util.Params( kwd ) - msg = util.restore_text( params.get( 'msg', '' ) ) - messagetype = params.get( 'messagetype', 'done' ) - request = trans.app.model.Request.get(int(params.get('request_id', 0))) - if self.details_state == 'Show request details': - self.details_state = 'Hide request details' - elif self.details_state == 'Hide request details': - self.details_state = 'Show request details' - copy_list = SelectField('copy_sample') - copy_list.add_option('None', -1, selected=True) - for i, s in enumerate(self.current_samples): - copy_list.add_option(i+1, i) - return trans.fill_template( '/admin/requests/show_request.mako', - request=request, - request_details=self.request_details(trans, request.id), - current_samples = self.current_samples, - sample_copy=copy_list, details_state=self.details_state) - def request_details(self, trans, id): - ''' - Shows the request details - ''' - request = trans.app.model.Request.get(id) - # list of widgets to be rendered on the request form - request_details = [] - # main details - request_details.append(dict(label='User', - value=str(request.user.email), - helptext='')) - request_details.append(dict(label='Description', - value=request.desc, - helptext='')) - request_details.append(dict(label='Type', - value=request.type.name, - helptext='')) - request_details.append(dict(label='State', - value=request.state, - helptext='')) - request_details.append(dict(label='Date created', - value=request.create_time, - helptext='')) - # library associated - if request.library: - value=request.library.name - else: - value = None - request_details.append(dict(label='Data library', - value=value, - helptext='Data library where the resultant dataset will be stored')) - # folder associated - if request.folder: - value = request.folder.name - else: - value = None - request_details.append( dict( label='Data library folder', - value=value, - helptext='Data library folder where the resultant dataset will be stored' ) ) - - # form fields - for index, field in enumerate(request.type.request_form.fields): - if field['required']: - req = 'Required' - else: - req = 'Optional' - if field['type'] == 'AddressField': - if request.values.content[index]: - request_details.append(dict(label=field['label'], - value=trans.app.model.UserAddress.get(int(request.values.content[index])).get_html(), - helptext=field['helptext']+' ('+req+')')) - else: - request_details.append(dict(label=field['label'], - value=None, - helptext=field['helptext']+' ('+req+')')) - else: - request_details.append(dict(label=field['label'], - value=request.values.content[index], - helptext=field['helptext']+' ('+req+')')) - return request_details - +# +#---- Request Creation ---------------------------------------------------------- +# def __select_request_type(self, trans, rtid): rt_ids = ['none'] for rt in trans.app.model.RequestType.query().all(): @@ -723,21 +401,22 @@ return select_user def __library_ui(self, trans, user, request=None, **kwd): - """ - Return a list of libraries for which user has the permission - to perform the LIBRARY_ADD action on any of it's folders - """ + ''' + This method creates the data library & folder selectbox for new & + editing requests. First we get a list of all the libraries accessible to + the current user and display it in a selectbox. If the user has select an + existing library then display all the accessible sub folders of the selected + data library. + ''' params = util.Params( kwd ) lib_id = params.get( 'library_id', 'none' ) - # if editing a request + # if editing a request and the user has already associated a library to + # this request, then set the selected_lib to the request.library selected_lib = None if request and lib_id == 'none': if request.library: lib_id = str(request.library.id) selected_lib = request.library - else: - # new request - selected_lib = None # if new request no user is selected initially, none of the libraries are # listed in the selectfield if not user: @@ -759,10 +438,11 @@ can_show, hidden_folder_ids = trans.app.security_agent.show_library_item( user, roles, library, actions_to_check ) if can_show: libraries[ library ] = hidden_folder_ids - # create the selectfield + # create data library selectbox with refresh on change enabled lib_id_list = ['new'] + [str(lib.id) for lib in libraries.keys()] lib_list = SelectField( 'library_id', refresh_on_change=True, refresh_on_change_values=lib_id_list ) - # first option + # fill up the options in the Library selectbox + # first option 'none' is the value for "Select one" option if lib_id == 'none': lib_list.add_option('Select one', 'none', selected=True) else: @@ -784,15 +464,23 @@ lib_widget = dict(label='Data library', widget=lib_list, helptext='Data library where the resultant dataset will be stored.') + # show the folder widget only if the user has selected a valid library above if selected_lib: - # when editing a request + # when editing a request, either the user has already selected a subfolder or not if request: if request.folder: current_fid = request.folder.id - else: - current_fid = request.library.root_folder.id + else: + # when a folder not yet associated with the request then the + # the current folder is set to the root_folder of the + # parent data library if present. + if request.library: + current_fid = request.library.root_folder.id + else: + current_fid = params.get( 'folder_id', 'none' ) else: current_fid = params.get( 'folder_id', 'none' ) + # create the folder selectbox folder_list = SelectField( 'folder_id') # first option if lib_id == 'none': @@ -828,9 +516,14 @@ ''' Validates the request entered by the user ''' + if not request.samples: + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + operation='show_request', + message='Please add one or more samples to this request before submitting.', + status='error', + id=trans.security.encode_id(request.id)) ) empty_fields = [] -# if not request.library: -# empty_fields.append('Library') # check rest of the fields of the form for index, field in enumerate(request.type.request_form.fields): if field['required'] == 'required' and request.values.content[index] in ['', None]: @@ -909,6 +602,350 @@ request.folder = folder request.flush() return request + + +# +#---- Request Editing ---------------------------------------------------------- +# + @web.expose + @web.require_admin + def edit(self, trans, **kwd): + params = util.Params( kwd ) + msg = util.restore_text( params.get( 'msg', '' ) ) + messagetype = params.get( 'messagetype', 'done' ) + try: + request = trans.app.model.Request.get(int(params.get('request_id', None))) + except: + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + status='error', + message="Invalid request ID", + **kwd) ) + if params.get('show', False) == 'True': + return self.__edit_request(trans, request.id, **kwd) + elif params.get('save_changes_request_button', False) == 'Save changes' \ + or params.get('edit_samples_button', False) == 'Edit samples': + request_type = trans.app.model.RequestType.get(int(params.select_request_type)) + if not util.restore_text(params.get('name', '')): + msg = 'Please enter the <b>Name</b> of the request' + kwd['messagetype'] = 'error' + kwd['msg'] = msg + kwd['show'] = 'True' + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='edit', + **kwd) ) + request = self.__save_request(trans, request, **kwd) + msg = 'The changes made to the request named %s has been saved' % request.name + if params.get('save_changes_request_button', False) == 'Save changes': + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + message=msg , + status='done') ) + elif params.get('edit_samples_button', False) == 'Edit samples': + new_kwd = {} + new_kwd['request_id'] = request.id + new_kwd['edit_samples_button'] = 'Edit samples' + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='show_request', + msg=msg , + messagetype='done', + **new_kwd) ) + elif params.get('refresh', False) == 'true': + return self.__edit_request(trans, request.id, **kwd) + @web.expose + @web.require_admin + def submit_request(self, trans, **kwd): + params = util.Params( kwd ) + try: + id = int(params.get('id', False)) + request = trans.app.model.Request.get(id) + except: + msg = "Invalid request ID" + log.warn( msg ) + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + status='error', + message=msg, + **kwd) ) + msg = self.__validate(trans, request) + if msg: + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='edit', + messagetype='error', + msg=msg, + request_id=request.id, + show='True') ) + # get the new state + new_state = request.type.states[0] + for s in request.samples: + event = trans.app.model.SampleEvent(s, new_state, 'Samples submitted to the system') + event.flush() + # change request's submitted field + request.state = request.states.SUBMITTED + request.flush() + kwd['id'] = trans.security.encode_id(request.id) + kwd['status'] = 'done' + kwd['message'] = 'The request <b>%s</b> has been submitted.' % request.name + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + show_filter=trans.app.model.Request.states.SUBMITTED, + **kwd) ) + def __update_samples(self, request, **kwd): + ''' + This method retrieves all the user entered sample information and + returns an list of all the samples and their field values + ''' + params = util.Params( kwd ) + current_samples = [] + for s in request.samples: + current_samples.append([s.name, s.values.content]) + index = len(request.samples) + while True: + if params.get( 'sample_%i_name' % index, '' ): + sample_index = index + sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) + sample_values = [] + for field_index in range(len(request.type.sample_form.fields)): + sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) + current_samples.append([sample_name, sample_values]) + index = index + 1 + else: + break + details = params.get( 'details', 'hide' ) + edit_mode = params.get( 'edit_mode', 'False' ) + return current_samples, details, edit_mode + def __copy_sample(self, current_samples): + copy_list = SelectField('copy_sample') + copy_list.add_option('None', -1, selected=True) + for i, s in enumerate(current_samples): + copy_list.add_option(s[0], i) + return copy_list + @web.expose + @web.require_login( "create/submit sequencing requests" ) + def show_request(self, trans, **kwd): + params = util.Params( kwd ) + msg = util.restore_text( params.get( 'msg', '' ) ) + messagetype = params.get( 'messagetype', 'done' ) + try: + request = trans.app.model.Request.get(int(params.get('request_id', None))) + except: + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + status='error', + message="Invalid request ID", + **kwd) ) + # get the user entered sample details + current_samples, details, edit_mode = self.__update_samples( request, **kwd ) + if params.get('import_samples_button', False) == 'Import samples': + try: + file_obj = params.get('file_data', '') + import csv + reader = csv.reader(file_obj.file) + for row in reader: + current_samples.append([row[0], row[1:]]) + return trans.fill_template( '/admin/requests/show_request.mako', + request=request, + request_details=self.request_details(trans, request.id), + current_samples=current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) + except: + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + status='error', + message='Error in importing samples file', + **kwd)) + elif params.get('add_sample_button', False) == 'Add New': + # add an empty or filled sample + # if the user has selected a sample no. to copy then copy the contents + # of the src sample to the new sample else an empty sample + src_sample_index = int(params.get( 'copy_sample', -1 )) + if src_sample_index == -1: + # empty sample + current_samples.append(['Sample_%i' % (len(current_samples)+1),['' for field in request.type.sample_form.fields]]) + else: + current_samples.append([current_samples[src_sample_index][0]+'_%i' % (len(current_samples)+1), + [val for val in current_samples[src_sample_index][1]]]) + return trans.fill_template( '/admin/requests/show_request.mako', + request=request, + request_details=self.request_details(trans, request.id), + current_samples=current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) + elif params.get('save_samples_button', False) == 'Save': + # check for duplicate sample names + msg = '' + for index in range(len(current_samples)-len(request.samples)): + sample_index = index + len(request.samples) + sample_name = current_samples[sample_index][0] + if not sample_name.strip(): + msg = 'Please enter the name of sample number %i' % sample_index + break + count = 0 + for i in range(len(current_samples)): + if sample_name == current_samples[i][0]: + count = count + 1 + if count > 1: + msg = "This request has <b>%i</b> samples with the name <b>%s</b>.\nSamples belonging to a request must have unique names." % (count, sample_name) + break + if msg: + return trans.fill_template( '/admin/requests/show_request.mako', + request=request, + request_details=self.request_details(trans, request.id), + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, edit_mode=edit_mode, + messagetype='error', msg=msg) + # save all the new/unsaved samples entered by the user + if edit_mode == 'False': + for index in range(len(current_samples)-len(request.samples)): + sample_index = index + len(request.samples) + sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) + sample_values = [] + for field_index in range(len(request.type.sample_form.fields)): + sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) + form_values = trans.app.model.FormValues(request.type.sample_form, sample_values) + form_values.flush() + s = trans.app.model.Sample(sample_name, '', request, form_values) + s.flush() + else: + for index in range(len(current_samples)): + sample_index = index + sample_name = current_samples[sample_index][0] + new_sample_name = util.restore_text( params.get( 'sample_%i_name' % sample_index, '' ) ) + sample_values = [] + for field_index in range(len(request.type.sample_form.fields)): + sample_values.append(util.restore_text( params.get( 'sample_%i_field_%i' % (sample_index, field_index), '' ) )) + sample = request.has_sample(sample_name) + if sample: + form_values = trans.app.model.FormValues.get(sample.values.id) + form_values.content = sample_values + form_values.flush() + sample.name = new_sample_name + sample.flush() + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + operation='show_request', + id=trans.security.encode_id(request.id)) ) + elif params.get('edit_samples_button', False) == 'Edit samples': + edit_mode = 'True' + return trans.fill_template( '/admin/requests/show_request.mako', + request=request, + request_details=self.request_details(trans, request.id), + current_samples=current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) + elif params.get('cancel_changes_button', False) == 'Cancel': + return trans.response.send_redirect( web.url_for( controller='requests_admin', + action='list', + operation='show_request', + id=trans.security.encode_id(request.id)) ) + + + @web.expose + @web.require_login( "create/submit sequencing requests" ) + def delete_sample(self, trans, **kwd): + params = util.Params( kwd ) + msg = util.restore_text( params.get( 'msg', '' ) ) + messagetype = params.get( 'messagetype', 'done' ) + request = trans.app.model.Request.get(int(params.get('request_id', 0))) + current_samples, details, edit_mode = self.__update_samples( request, **kwd ) + sample_index = int(params.get('sample_id', 0)) + sample_name = current_samples[sample_index][0] + s = request.has_sample(sample_name) + if s: + s.delete() + s.flush() + request.flush() + del current_samples[sample_index] + return trans.fill_template( '/admin/requests/show_request.mako', + request=request, + request_details=self.request_details(trans, request.id), + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) + + @web.expose + @web.require_login( "create/submit sequencing requests" ) + def toggle_request_details(self, trans, **kwd): + params = util.Params( kwd ) + msg = util.restore_text( params.get( 'msg', '' ) ) + messagetype = params.get( 'messagetype', 'done' ) + request = trans.app.model.Request.get(int(params.get('request_id', 0))) + current_samples, details, edit_mode = self.__update_samples( request, **kwd ) + return trans.fill_template( '/admin/requests/show_request.mako', + request=request, + request_details=self.request_details(trans, request.id), + current_samples = current_samples, + sample_copy=self.__copy_sample(current_samples), + details=details, + edit_mode=edit_mode) + @web.expose + @web.require_admin + def request_details(self, trans, id): + ''' + Shows the request details + ''' + request = trans.app.model.Request.get(id) + # list of widgets to be rendered on the request form + request_details = [] + # main details + request_details.append(dict(label='User', + value=str(request.user.email), + helptext='')) + request_details.append(dict(label='Description', + value=request.desc, + helptext='')) + request_details.append(dict(label='Type', + value=request.type.name, + helptext='')) + request_details.append(dict(label='State', + value=request.state, + helptext='')) + request_details.append(dict(label='Date created', + value=request.create_time, + helptext='')) + # library associated + if request.library: + value=request.library.name + else: + value = None + request_details.append(dict(label='Data library', + value=value, + helptext='Data library where the resultant dataset will be stored')) + # folder associated + if request.folder: + value = request.folder.name + else: + value = None + request_details.append( dict( label='Data library folder', + value=value, + helptext='Data library folder where the resultant dataset will be stored' ) ) + + # form fields + for index, field in enumerate(request.type.request_form.fields): + if field['required']: + req = 'Required' + else: + req = 'Optional' + if field['type'] == 'AddressField': + if request.values.content[index]: + request_details.append(dict(label=field['label'], + value=trans.app.model.UserAddress.get(int(request.values.content[index])).get_html(), + helptext=field['helptext']+' ('+req+')')) + else: + request_details.append(dict(label=field['label'], + value=None, + helptext=field['helptext']+' ('+req+')')) + else: + request_details.append(dict(label=field['label'], + value=request.values.content[index], + helptext=field['helptext']+' ('+req+')')) + return request_details @web.expose @web.require_admin def bar_codes(self, trans, **kwd): @@ -1086,7 +1123,9 @@ events_list=events_list, sample=sample, widgets=widgets, title=title) - # Request Type Stuff +## +#### Request Type Stuff ################################################### +## @web.expose @web.require_admin def manage_request_types( self, trans, **kwd ): diff -r b16a6d767e65 -r ca816e66ec69 lib/galaxy/web/framework/__init__.py --- a/lib/galaxy/web/framework/__init__.py Tue Oct 13 18:15:06 2009 -0400 +++ b/lib/galaxy/web/framework/__init__.py Wed Oct 14 02:22:11 2009 -0400 @@ -635,7 +635,7 @@ return None def request_types(self): - if self.app.model.RequestType.query().all(): + if self.app.model.RequestType.query().filter_by(deleted=False).all(): return True return False diff -r b16a6d767e65 -r ca816e66ec69 templates/admin/forms/edit_form.mako --- a/templates/admin/forms/edit_form.mako Tue Oct 13 18:15:06 2009 -0400 +++ b/templates/admin/forms/edit_form.mako Wed Oct 14 02:22:11 2009 -0400 @@ -36,16 +36,20 @@ <% options = field_attr[2] %> <div class="repeat-group-item"> <div class="form-row"> - <label> Options</label> - %for i, option in enumerate(options): - <b> ${i+1}</b> - ${option[1].get_html()} - <a class="action-button" href="${h.url_for( controller='forms', action='edit', form_id=form.id, select_box_options='remove', field_index=index, option_index=i )}">Remove</a><br> - %endfor + <label> Options</label> + %for i, option in enumerate(options): + <div class="form-row"> + <b> ${i+1}</b> + ${option[1].get_html()} + ##<a class="action-button" href="${h.url_for( controller='forms', action='edit', form_id=form.id, select_box_options='remove', field_index=index, option_index=i )}">Remove</a><br> + <input type="submit" name="removeoption_${index}_${i}" value="Remove"/> + </div> + %endfor + <input type="hidden" name="field_index" value="${index}"/> </div> </div> <div class="form-row"> - <a class="action-button" href="${h.url_for( controller='forms', action='edit', form_id=form.id, select_box_options='add', field_index=index )}">Add</a> + <input type="submit" name="addoption_${index}" value="Add"/> </div> %endif %endif @@ -81,7 +85,7 @@ <div class="toolForm"> <div class="toolFormTitle">Edit form definition "${form.name}"</div> - <form id="edit_form" name="edit_form" action="${h.url_for( controller='forms', action='edit', form_id=form.id, num_fields=len(form.fields) )}" method="post" > + <form id="edit_form" name="edit_form" action="${h.url_for( controller='forms', action='edit', form_id=form.id )}" method="post" > %for label, input in form_details: <div class="form-row"> <label>${label}</label> diff -r b16a6d767e65 -r ca816e66ec69 templates/admin/forms/show_form_read_only.mako --- a/templates/admin/forms/show_form_read_only.mako Tue Oct 13 18:15:06 2009 -0400 +++ b/templates/admin/forms/show_form_read_only.mako Wed Oct 14 02:22:11 2009 -0400 @@ -63,9 +63,19 @@ <div class="toolForm"> %if form.desc: - <div class="toolFormTitle">${form.name} - <i>${form.desc}</i></div> + <div class="toolFormTitle">${form.name} - <i>${form.desc}</i> + <a id="form-${form.id}-popup" class="popup-arrow" style="display: none;">▼</a> + <div popupmenu="form-${form.id}-popup"> + <a class="action-button" href="${h.url_for( action='edit', form_id=form.id, show_form=True )}">Edit</a> + </div> + </div> %else: - <div class="toolFormTitle">${form.name}</div> + <div class="toolFormTitle">${form.name} + <a id="form-${form.id}-popup" class="popup-arrow" style="display: none;">▼</a> + <div popupmenu="form-${form.id}-popup"> + <a class="action-button" href="${h.url_for( action='edit', form_id=form.id, show_form=True )}">Edit</a> + </div> + </div> %endif <form name="library" action="${h.url_for( controller='forms', action='manage' )}" method="post" > %if form.type == trans.app.model.FormDefinition.types.SAMPLE: @@ -77,58 +87,27 @@ %endfor %endif %else: - <table class = "grid"> - <tbody> - %for index, field in enumerate(form.fields): - <tr> - <td> - <div class="form-row"> - <label>${1+index}. Label</label> - <a>${field['label']}</a> - %if field['type'] == 'SelectField': - <a id="${field['label']}-popup" class="popup-arrow" style="display: none;">▼</a> - %for option in field['selectlist']: - <div popupmenu="${field['label']}-type-popup"> - <a class="action-button" href="" >${option}</a> - </div> - %endfor - %endif - </div> - </td> - <td> - <div class="form-row"> - <label>Help text </label> - %if not field['helptext']: - <a><i>No helptext</i></a> - %else: - <a>${field['helptext']}</a> - %endif - </div> - </td> - <td> - <div class="form-row"> - <label>Type:</label> - <a>${field['type']}</a> - %if field['type'] == 'SelectField': - <a id="fieldtype-popup" class="popup-arrow" style="display: none;">▼</a> - %for option in field['selectlist']: - <div popupmenu="type-popup"> - <a class="action-button" href="" >${option}</a> - </div> - %endfor - %endif - </div> - </td> - <td> - <div class="form-row"> - <label>Required?</label> - <a>${field['required']}</a> - </div> - </td> - </tr> - %endfor - </tbody> - </table> + %for index, field in enumerate(form.fields): + <div class="form-row"> + <label>${field['label']}</label> + %if field['helptext']: + <div class="toolParamHelp" style="clear: both;"> + <i>${field['helptext']}</i> + </div> + %endif + <div>${field['required']}</div> + <i>Type: </i> ${field['type']} + %if field['type'] == 'SelectField': + <div> + <div><i>Options:</i></div> + %for option in field['selectlist']: + <div>${option}</div> + %endfor + </div> + %endif + </div> + <div style="clear: both"></div> + %endfor %endif </form> </div> diff -r b16a6d767e65 -r ca816e66ec69 templates/admin/requests/show_request.mako --- a/templates/admin/requests/show_request.mako Tue Oct 13 18:15:06 2009 -0400 +++ b/templates/admin/requests/show_request.mako Wed Oct 14 02:22:11 2009 -0400 @@ -58,6 +58,8 @@ </select> %elif field['type'] == 'CheckboxField': <input type="checkbox" name="sample_${index}_field_${field_index}" value="Yes"/> + %elif field['type'] == 'NumberField': + <input type=int name="sample_${index}_field_${field_index}" value="${sample_values[field_index]}" size="7"/> %endif <div class="toolParamHelp" style="clear: both;"> <i>${'('+field['required']+')' }</i> @@ -91,35 +93,41 @@ </%def> <div class="toolForm"> - <div class="form-row"> - <a href="${h.url_for( controller='requests_admin', action='toggle_request_details', request_id=request.id )}">${details_state}</a> - </div> - %if details_state == "Hide request details": - %for index, rd in enumerate(request_details): + <div class="form-row"> + %if details == "show": + <a href="${h.url_for( controller='requests_admin', action='toggle_request_details', request_id=request.id, details="hide" )}">Hide request details</a> + </div> + %for index, rd in enumerate(request_details): + <div class="form-row"> + <label>${rd['label']}</label> + %if not rd['value']: + <i>None</i> + %else: + %if rd['label'] == 'Data library': + %if rd['value']: + <a href="${h.url_for( controller='library', action='browse_library', obj_id=request.library.id )}">${rd['value']}</a> + %else: + <i>None</i> + %endif + %else: + ${rd['value']} + %endif + %endif + </div> + <div style="clear: both"></div> + %endfor <div class="form-row"> - <label>${rd['label']}</label> - %if not rd['value']: - <i>None</i> - %else: - %if rd['label'] == 'Data library': - <a href="${h.url_for( controller='library_admin', action='browse_library', obj_id=request.library.id )}">${rd['value']}</a> - %else: - ${rd['value']} - %endif - %endif - </div> - <div style="clear: both"></div> - %endfor - <div class="form-row"> <ul class="manage-table-actions"> <li> <a class="action-button" href="${h.url_for( controller='requests_admin', action='edit', show=True, request_id=request.id)}"> <span>Edit request details</span></a> </li> </ul> + </div> </div> + %else: + <a href="${h.url_for( controller='requests_admin', action='toggle_request_details', request_id=request.id, details="show" )}">Show request details</a> %endif - </div> </div> <%def name="render_grid( grid_index, grid_name, fields_dict )"> @@ -151,13 +159,13 @@ request.refresh() %> %for sample_index, sample in enumerate(current_samples): - %if edit_mode: + %if edit_mode == 'True': <tr> <td>${sample_index+1}</td> ${render_sample_form( sample_index, sample[0], sample[1], grid_index, fields_dict)} <td> %if request.unsubmitted() and grid_index == 0: - <a class="action-button" href="${h.url_for( controller='requests', action='delete_sample', request_id=request.id, sample_id=sample_index)}"> + <a class="action-button" href="${h.url_for( controller='requests_admin', action='delete_sample', request_id=request.id, sample_id=sample_index)}"> <img src="${h.url_for('/static/images/delete_icon.png')}" /> <span></span></a> %endif @@ -173,7 +181,7 @@ %endif <td> %if request.unsubmitted() and grid_index == 0: - <a class="action-button" href="${h.url_for( controller='requests', action='delete_sample', request_id=request.id, sample_id=sample_index)}"> + <a class="action-button" href="${h.url_for( controller='requests_admin', action='delete_sample', request_id=request.id, sample_id=sample_index)}"> <img src="${h.url_for('/static/images/delete_icon.png')}" /> <span></span></a> %endif @@ -201,7 +209,7 @@ <label>There are no samples.</label> %endif </div> - %if not edit_mode: + %if edit_mode == 'False': <table class="grid"> <tbody> <tr> @@ -238,7 +246,7 @@ <div style="clear: both"></div> </div> <div class="form-row"> - %if edit_mode: + %if edit_mode == 'True': <input type="submit" name="save_samples_button" value="Save"/> <input type="submit" name="cancel_changes_button" value="Cancel"/> %elif request.unsubmitted(): diff -r b16a6d767e65 -r ca816e66ec69 templates/requests/show_request.mako --- a/templates/requests/show_request.mako Tue Oct 13 18:15:06 2009 -0400 +++ b/templates/requests/show_request.mako Wed Oct 14 02:22:11 2009 -0400 @@ -92,41 +92,43 @@ </%def> <div class="toolForm"> - <div class="form-row"> - <a href="${h.url_for( controller='requests', action='toggle_request_details', request_id=request.id )}">${details_state}</a> - </div> - %if details_state == "Hide request details": - %for index, rd in enumerate(request_details): - <div class="form-row"> - <label>${rd['label']}</label> - %if not rd['value']: - <i>None</i> - %else: - %if rd['label'] == 'Data library': - %if rd['value']: - <a href="${h.url_for( controller='library', action='browse_library', obj_id=request.library.id )}">${rd['value']}</a> + <div class="form-row"> + %if details == "show": + <a href="${h.url_for( controller='requests', action='toggle_request_details', request_id=request.id, details="hide" )}">Hide request details</a> + </div> + %for index, rd in enumerate(request_details): + <div class="form-row"> + <label>${rd['label']}</label> + %if not rd['value']: + <i>None</i> + %else: + %if rd['label'] == 'Data library': + %if rd['value']: + <a href="${h.url_for( controller='library', action='browse_library', obj_id=request.library.id )}">${rd['value']}</a> + %else: + <i>None</i> + %endif %else: - <i>None</i> + ${rd['value']} %endif - %else: - ${rd['value']} %endif - %endif - </div> - <div style="clear: both"></div> - %endfor - %if request.unsubmitted(): - <div class="form-row"> - <ul class="manage-table-actions"> - <li> - <a class="action-button" href="${h.url_for( controller='requests', action='edit', show=True, request_id=request.id)}"> - <span>Edit request details</span></a> - </li> - </ul> - </div> - %endif + </div> + <div style="clear: both"></div> + %endfor + %if request.unsubmitted(): + <div class="form-row"> + <ul class="manage-table-actions"> + <li> + <a class="action-button" href="${h.url_for( controller='requests', action='edit', show=True, request_id=request.id)}"> + <span>Edit request details</span></a> + </li> + </ul> + </div> + %endif + </div> + %else: + <a href="${h.url_for( controller='requests', action='toggle_request_details', request_id=request.id, details="show" )}">Show request details</a> %endif - </div> </div> <%def name="render_grid( grid_index, grid_name, fields_dict )"> @@ -158,7 +160,7 @@ request.refresh() %> %for sample_index, sample in enumerate(current_samples): - %if edit_mode: + %if edit_mode == 'True': <tr> <td>${sample_index+1}</td> ${render_sample_form( sample_index, sample[0], sample[1], grid_index, fields_dict)} @@ -194,7 +196,7 @@ <div class="toolForm"> ##<div class="toolFormTitle">Samples (${len(request.samples)})</div> - <form id="show_request" name="show_request" action="${h.url_for( controller='requests', action='show_request' )}" enctype="multipart/form-data" method="post" > + <form id="show_request" name="show_request" action="${h.url_for( controller='requests', action='show_request', edit_mode=edit_mode )}" enctype="multipart/form-data" method="post" > <div class="form-row"> %if current_samples: %if not request.type.sample_form.layout: @@ -211,7 +213,7 @@ %endif </div> - %if request.unsubmitted() and not edit_mode: + %if request.unsubmitted() and edit_mode == 'False': <table class="grid"> <tbody> <tr> @@ -251,7 +253,7 @@ </div> <div class="form-row"> <input type="submit" name="save_samples_button" value="Save"/> - %if edit_mode: + %if edit_mode == 'True': <input type="submit" name="cancel_changes_button" value="Cancel"/> %endif </div> diff -r b16a6d767e65 -r ca816e66ec69 templates/user/address.mako --- a/templates/user/address.mako Tue Oct 13 18:15:06 2009 -0400 +++ b/templates/user/address.mako Wed Oct 14 02:22:11 2009 -0400 @@ -46,7 +46,7 @@ <td> <div class="form-row"> <label>${address.desc}</label> - ${address.display()} + ${address.get_html()} </div> <div class="form-row"> <ul class="manage-table-actions"> diff -r b16a6d767e65 -r ca816e66ec69 test/base/twilltestcase.py --- a/test/base/twilltestcase.py Tue Oct 13 18:15:06 2009 -0400 +++ b/test/base/twilltestcase.py Wed Oct 14 02:22:11 2009 -0400 @@ -1059,8 +1059,15 @@ tc.fv( "1", field_name, field_contents ) tc.fv( "1", field_help_name, field_help_contents ) tc.submit( "save_changes_button" ) + if num_fields: check_str = "The form '%s' has been updated with the changes." % name - self.check_page_for_string( check_str ) + self.check_page_for_string( check_str ) + else: + self.home() + self.visit_url( "%s/forms/manage" % self.url ) + self.check_page_for_string( name ) + self.check_page_for_string( desc ) + self.check_page_for_string( formtype ) self.home() def edit_form( self, form_id, form_name, new_form_name="Form One's Name (Renamed)", new_form_desc="This is Form One's description (Re-described)"): """ @@ -1074,7 +1081,7 @@ tc.submit( "save_changes_button" ) self.check_page_for_string( "The form '%s' has been updated with the changes." % new_form_name ) self.home() - def form_add_field( self, form_id, form_name, field_index, fields): + def form_add_field( self, form_id, form_name, form_desc, form_type, field_index, fields): """ Add a new fields to the form definition """ @@ -1088,13 +1095,18 @@ tc.fv( "1", "field_helptext_%i" % index, field['desc'] ) tc.fv( "1", "field_type_%i" % index, field['type'] ) tc.fv( "1", "field_required_%i" % index, field['required'] ) -# if field['type'] == 'SelectField': -# for option_index, option in enumerate(field['selectlist']): -# self.visit_url( "%s/forms/edit?select_box_options=add&form_id=%i&field_index=%i" % \ -# (self.url, form_id, index)) -# #data = self.last_page() -# #file( "rc.html", 'wb' ).write(data) -# tc.fv( "1", "field_%i_option_%i" % (index, option_index), option ) + if field['type'] == 'SelectField': + options = '' + for option_index, option in enumerate(field['selectlist']): + url_str = "%s/forms/edit?description=%s&form_id=%i&form_type_selectbox=%s&addoption_%i=Add&name=%s&field_name_%i=%s&field_helptext_%i=%s&field_type_%i=%s" % \ + (self.url, form_desc.replace(" ", "+"), form_id, form_type.replace(" ", "+"), + index, form_name.replace(" ", "+"), index, field['name'].replace(" ", "+"), + index, field['desc'].replace(" ", "+"), index, field['type']) + self.visit_url( url_str + options ) + data = self.last_page() + file( "rc.html", 'wb' ).write(data) + tc.fv( "1", "field_%i_option_%i" % (index, option_index), option ) + options = options + "&field_%i_option_%i=%s" % (index, option_index, option) tc.submit( "save_changes_button" ) check_str = "The form '%s' has been updated with the changes." % form_name self.check_page_for_string( check_str ) @@ -1130,18 +1142,22 @@ self.check_page_for_string( "Request type <b>%s</b> has been created" % name ) def create_request( self, request_type_id, name, desc, library_id, folder_id, fields ): self.home() - self.visit_url( "%s/requests/new?create=True&select_request_type=%i" % (self.url, request_type_id) ) + self.visit_url( "%s/requests/new?create=True&select_request_type=%i&library_id=%i" % ( self.url, + request_type_id, + library_id ) ) self.check_page_for_string( 'Add a new request' ) tc.fv( "1", "name", name ) tc.fv( "1", "desc", desc ) tc.fv( "1", "library_id", str(library_id) ) - #tc.fv( "1", "folder_id", str(folder_id) ) + tc.fv( "1", "folder_id", str(folder_id) ) for index, field_value in enumerate(fields): tc.fv( "1", "field_%i" % index, field_value ) tc.submit( "create_request_button" ) def create_request_admin( self, request_type_id, user_id, name, desc, library_id, fields ): self.home() - self.visit_url( "%s/requests_admin/new?create=True&select_request_type=%i" % (self.url, request_type_id) ) + self.visit_url( "%s/requests_admin/new?create=True&select_request_type=%i&library_id=%i" % ( self.url, + request_type_id, + library_id ) ) self.check_page_for_string( 'Add a new request' ) tc.fv( "1", "select_user", str(user_id) ) tc.fv( "1", "name", name ) diff -r b16a6d767e65 -r ca816e66ec69 test/functional/test_forms_and_requests.py --- a/test/functional/test_forms_and_requests.py Tue Oct 13 18:15:06 2009 -0400 +++ b/test/functional/test_forms_and_requests.py Wed Oct 14 02:22:11 2009 -0400 @@ -42,12 +42,7 @@ name = form_one_name desc = "This is Form One's description" formtype = galaxy.model.FormDefinition.types.REQUEST - self.create_form( name=name, desc=desc, formtype=formtype ) - self.home() - self.visit_page( 'forms/manage' ) - self.check_page_for_string( name ) - self.check_page_for_string( desc ) - self.check_page_for_string( formtype ) + self.create_form( name=name, desc=desc, formtype=formtype, num_fields=0 ) # Get the form_definition object for later tests form_one = galaxy.model.FormDefinition.filter( and_( galaxy.model.FormDefinition.table.c.name==name, galaxy.model.FormDefinition.table.c.desc==desc, @@ -66,24 +61,21 @@ """Testing adding fields to a form definition""" fields = [dict(name='Test field name one', desc='Test field description one', - type='TextField', - required='required'), + type='SelectField', + required='optional', + selectlist=['option1', 'option2']), dict(name='Test field name two', desc='Test field description two', type='AddressField', - required='optional')] + required='optional'), + dict(name='Test field name three', + desc='Test field description three', + type='TextField', + required='required')] form_one = get_latest_form(form_one_name) - self.form_add_field(form_one.id, form_one.name, field_index=len(form_one.fields), fields=fields) + self.form_add_field(form_one.id, form_one.name, form_one.desc, form_one.type, field_index=len(form_one.fields), fields=fields) form_one_latest = get_latest_form(form_one_name) assert len(form_one_latest.fields) == len(form_one.fields)+len(fields) -#This following test has been commented out as it is causing: -#TwillException: multiple matches to "remove_button" -# def test_010_remove_form_fields( self ): -# """Testing removing fields from a form definition""" -# form_one = get_latest_form(form_one_name) -# self.form_remove_field( form_one.id, form_one.name, 'Test field name one' ) -# form_one_latest = get_latest_form(form_one_name) -# assert len(form_one_latest.fields) == len(form_one.fields)-1 def test_015_create_sample_form( self ): """Testing creating another form (for samples)""" global form_two_name @@ -186,10 +178,10 @@ self.logout() self.login( email='test1@bx.psu.edu' ) # set field values - fields = ['field one value', 'field two value', str(user_address.id)] + fields = ['option1', str(user_address.id), 'field three value'] # create the request request_name, request_desc = 'Request One', 'Request One Description' - self.create_request(request_type.id, request_name, request_desc, library_one.id, folder_one.id, fields) + self.create_request(request_type.id, request_name, request_desc, library_one.id, 'none', fields) self.check_page_for_string( request_name ) self.check_page_for_string( request_desc ) global request_one @@ -208,8 +200,9 @@ for field_value in fields: self.check_page_for_string( field_value ) # edit this request - fields = ['field one value (edited)', 'field two value (edited)', str(user_address.id)] - self.edit_request(request_one.id, request_one.name, request_one.name+' (Renamed)', request_one.desc+' (Re-described)', library_one.id, folder_one.id, fields) + fields = ['option2', str(user_address.id), 'field three value (edited)'] + self.edit_request(request_one.id, request_one.name, request_one.name+' (Renamed)', + request_one.desc+' (Re-described)', library_one.id, folder_one.id, fields) request_one.refresh() self.check_page_for_string( request_name+' (Renamed)' ) self.check_page_for_string( request_desc+' (Re-described)' ) @@ -261,8 +254,8 @@ self.login( email='test@bx.psu.edu' ) request_name = "RequestTwo" # simulate request creation - url_str = '%s/requests_admin/new?create=True&create_request_button=Save&select_request_type=%i&select_user=%i&name=%s&library_id=%i&folder_id=%i&refresh=True&field_1=%s&field_2=%i' \ - % ( self.url, request_type.id, regular_user.id, request_name, library_one.id, library_one.root_folder.id, "field_1_value", user_address.id ) + url_str = '%s/requests_admin/new?create=True&create_request_button=Save&select_request_type=%i&select_user=%i&name=%s&library_id=%i&folder_id=%i&refresh=True&field_2=%s&field_0=%s&field_1=%i' \ + % ( self.url, request_type.id, regular_user.id, request_name, library_one.id, library_one.root_folder.id, "field_2_value", 'option1', user_address.id ) self.home() self.visit_url( url_str ) self.check_page_for_string( "The new request named %s has been created" % request_name ) @@ -299,10 +292,3 @@ self.visit_url( '%s/requests_admin/list?show_filter=All' % self.url ) self.check_page_for_string( request_one.name ) self.check_page_for_string( request_two.name ) - - - - - - - \ No newline at end of file
participants (1)
-
Greg Von Kuster