def info_template( self, trans, cntrller, library_id, response_action='library', obj_id=None, folder_id=None, ldda_id=None, **kwd ): # Only adding a new template to a library or folder is currently allowed. Editing an existing template is # a future enhancement. The response_action param is the name of the method to which this method will redirect # if a new template is being added to a library or folder. params = util.Params( kwd ) msg = util.restore_text( params.get( 'msg', '' ) ) messagetype = params.get( 'messagetype', 'done' ) if obj_id: library_item = trans.sa_session.query( trans.app.model.FormDefinition ).get( int( obj_id ) ) library_item_desc = 'information template' response_id = obj_id elif folder_id: library_item = trans.sa_session.query( trans.app.model.LibraryFolder ).get( int( folder_id ) ) library_item_desc = 'folder' response_id = folder_id elif ldda_id: library_item = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( int( ldda_id ) ) library_item_desc = 'library dataset' response_id = ldda_id else: library_item = trans.sa_session.query( trans.app.model.Library ).get( int( library_id ) ) library_item_desc = 'library' response_id = library_id forms = get_all_forms( trans, filter=dict( deleted=False ), form_type=trans.app.model.FormDefinition.types.LIBRARY_INFO_TEMPLATE ) if not forms: msg = "There are no forms on which to base the template, so create a form and " msg += "try again to add the information template to the %s." % library_item_desc trans.response.send_redirect( web.url_for( controller='forms', action='new', msg=msg, messagetype='done', form_type=trans.app.model.FormDefinition.types.LIBRARY_INFO_TEMPLATE ) ) if params.get( 'add_info_template_button', False ): form = trans.sa_session.query( trans.app.model.FormDefinition ).get( int( kwd[ 'form_id' ] ) ) #fields = list( copy.deepcopy( form.fields ) ) form_values = trans.app.model.FormValues( form, [] ) trans.sa_session.add( form_values ) trans.sa_session.flush() if folder_id: assoc = trans.app.model.LibraryFolderInfoAssociation( library_item, form, form_values ) elif ldda_id: assoc = trans.app.model.LibraryDatasetDatasetInfoAssociation( library_item, form, form_values ) else: assoc = trans.app.model.LibraryInfoAssociation( library_item, form, form_values ) trans.sa_session.add( assoc ) trans.sa_session.flush() msg = 'An information template based on the form "%s" has been added to this %s.' % ( form.name, library_item_desc ) trans.response.send_redirect( web.url_for( controller=cntrller, action=response_action, obj_id=response_id, library_id=library_id, msg=msg, message_type='done' ) ) return trans.fill_template( '/library/select_info_template.mako', controller=cntrller, library_item_name=library_item.name, library_item_desc=library_item_desc, library_id=library_id, folder_id=folder_id, ldda_id=ldda_id, obj_id=response_id, forms=forms, msg=msg, messagetype=messagetype )
def info_template(self, trans, cntrller, library_id, response_action='library', obj_id=None, folder_id=None, ldda_id=None, **kwd): # Only adding a new template to a library or folder is currently allowed. Editing an existing template is # a future enhancement. The response_action param is the name of the method to which this method will redirect # if a new template is being added to a library or folder. params = util.Params(kwd) msg = util.restore_text(params.get('msg', '')) messagetype = params.get('messagetype', 'done') if obj_id: library_item = trans.sa_session.query( trans.app.model.FormDefinition).get(int(obj_id)) library_item_desc = 'information template' response_id = obj_id elif folder_id: library_item = trans.sa_session.query( trans.app.model.LibraryFolder).get(int(folder_id)) library_item_desc = 'folder' response_id = folder_id elif ldda_id: library_item = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation).get( int(ldda_id)) library_item_desc = 'library dataset' response_id = ldda_id else: library_item = trans.sa_session.query(trans.app.model.Library).get( int(library_id)) library_item_desc = 'library' response_id = library_id forms = get_all_forms(trans, filter=dict(deleted=False), form_type=trans.app.model.FormDefinition.types. LIBRARY_INFO_TEMPLATE) if not forms: msg = "There are no forms on which to base the template, so create a form and " msg += "try again to add the information template to the %s." % library_item_desc trans.response.send_redirect( web.url_for(controller='forms', action='new', msg=msg, messagetype='done', form_type=trans.app.model.FormDefinition.types. LIBRARY_INFO_TEMPLATE)) if params.get('add_info_template_button', False): form = trans.sa_session.query(trans.app.model.FormDefinition).get( int(kwd['form_id'])) #fields = list( copy.deepcopy( form.fields ) ) form_values = trans.app.model.FormValues(form, []) trans.sa_session.add(form_values) trans.sa_session.flush() if folder_id: assoc = trans.app.model.LibraryFolderInfoAssociation( library_item, form, form_values) elif ldda_id: assoc = trans.app.model.LibraryDatasetDatasetInfoAssociation( library_item, form, form_values) else: assoc = trans.app.model.LibraryInfoAssociation( library_item, form, form_values) trans.sa_session.add(assoc) trans.sa_session.flush() msg = 'An information template based on the form "%s" has been added to this %s.' % ( form.name, library_item_desc) trans.response.send_redirect( web.url_for(controller=cntrller, action=response_action, obj_id=response_id, library_id=library_id, msg=msg, message_type='done')) return trans.fill_template('/library/select_info_template.mako', controller=cntrller, library_item_name=library_item.name, library_item_desc=library_item_desc, library_id=library_id, folder_id=folder_id, ldda_id=ldda_id, obj_id=response_id, forms=forms, msg=msg, messagetype=messagetype)