def delete_analysis(): """Delete the selected analysis from the database.""" user = get_current_user() session = Session() NarfAnalysis = Tables()['NarfAnalysis'] success = False aSelected = request.args.get('aSelected') if len(aSelected) == 0: return jsonify(success=success) result = (session.query(NarfAnalysis).filter( NarfAnalysis.id == aSelected).first()) if result is None: return jsonify(success=success) if (result.public or (result.username == user.username) or (user.labgroup in result.labgroup)): success = True session.delete(result) session.commit() else: log.info("You do not have permission to delete this analysis.") return jsonify(success=success) session.close() return jsonify(success=success)
def register(): errors = '' form = RegistrationForm(request.form) if request.method == 'POST': if form.validate(): session = Session() NarfUsers = Tables()['NarfUsers'] new_user = NarfUsers( username=form.username.data, password=bcrypt.generate_password_hash(form.password.data), email=(form.email.data).encode('utf-8'), firstname=form.firstname.data, lastname=form.lastname.data, ) session.add(new_user) session.commit() session.close() return redirect(url_for('login')) else: log.info('form errors:') log.info(form.errors) return render_template( 'account_management/register.html', form=form, errors=form.errors, ) return render_template( 'account_management/register.html', form=form, errors=errors, )
def update_cells(): """Update the list of cells in the cell selector after a batch is selected (this will cascade from an analysis selection). Also updates current batch in NarfAnalysis for current analysis. """ session = Session() # Only get the numerals for the selected batch, not the description. bSelected = request.args.get('bSelected') aSelected = request.args.get('aSelected') celllist = [ i[0] for i in session.query(NarfBatches.cellid).filter( NarfBatches.batch == bSelected[:3]).all() ] batchname = (session.query(sBatch).filter( sBatch.id == bSelected[:3]).first()) if batchname: batch = str(bSelected[:3] + ': ' + batchname.name) else: batch = bSelected analysis = (session.query(NarfAnalysis).filter( NarfAnalysis.name == aSelected).first()) # don't change batch association if batch is blank if analysis and bSelected: analysis.batch = batch session.commit() session.close() return jsonify(celllist=celllist)
def set_saved_selections(): user = get_current_user() if not user.username: return jsonify( response="user not logged in, can't save selections", null=True, ) session = Session() saved_selections = request.args.get('stringed_selections') user_entry = (session.query(NarfUsers).filter( NarfUsers.username == user.username).first()) user_entry.selections = saved_selections session.commit() session.close() return jsonify(response='selections saved', null=False)
def edit_analysis(): """Take input from Analysis Editor modal and save it to the database. Button : Edit Analysis """ user = get_current_user() session = Session() NarfAnalysis = Tables()['NarfAnalysis'] modTime = datetime.datetime.now().replace(microsecond=0) eName = request.args.get('name') eId = request.args.get('id') eStatus = request.args.get('status') eTags = request.args.get('tags') eQuestion = request.args.get('question') eAnswer = request.args.get('answer') eLoad = request.args.get('load') eMod = request.args.get('mod') eFit = request.args.get('fit') eTree = json.dumps([eLoad, eMod, eFit]) if eId == '__none': checkExists = False else: checkExists = (session.query(NarfAnalysis).filter( NarfAnalysis.id == eId).first()) if checkExists: a = checkExists if (a.public or (user.labgroup in a.labgroup) or (a.username == user.username)): a.name = eName a.status = eStatus a.question = eQuestion a.answer = eAnswer a.tags = eTags try: a.lastmod = modTime except: a.lastmod = str(modTime) a.modeltree = eTree else: log.info("You do not have permission to modify this analysis.") return jsonify(success=("failed")) # If it doesn't exist, add new sql alchemy object with the # appropriate attributes, which should get assigned to a new id else: # TODO: Currently copies user's labgroup by default. # Is that the behavior we want? try: a = NarfAnalysis(name=eName, status=eStatus, question=eQuestion, answer=eAnswer, tags=eTags, batch='', lastmod=modTime, modeltree=eTree, username=user.username, labgroup=user.labgroup, public='0') except: a = NarfAnalysis(name=eName, status=eStatus, question=eQuestion, answer=eAnswer, tags=eTags, batch='', lastmod=str(modTime), modeltree=eTree, username=user.username, labgroup=user.labgroup, public='0') session.add(a) addedName = a.name session.commit() session.close() # After handling submissions, return user to main page so that it # refreshes with new analysis included in list return jsonify(success="Analysis %s saved successfully." % addedName)
def edit_analysis(): """Take input from Analysis Editor modal and save it to the database. Button : Edit Analysis """ user = get_current_user() session = Session() modTime = datetime.datetime.now().replace(microsecond=0) eName = request.args.get('name') eId = request.args.get('id') eStatus = request.args.get('status') eTags = request.args.get('tags') eQuestion = request.args.get('question') eAnswer = request.args.get('answer') eTree = request.args.get('tree') #TODO: add checks to require input inside form fields # or allow blank so that people can erase stuff? # Turned this off for now -- can re-enable when rule needs are more stable # Make sure the keyword combination is valid using nems.keyword_rules #try: # mf = ModelFinder(eTree) # for modelname in mf.modellist: # keyword_test_routine(modelname) #except Exception as e: # return jsonify(success='Analysis not saved: \n' + str(e)) if eId == '__none': checkExists = False else: checkExists = (session.query(NarfAnalysis).filter( NarfAnalysis.id == eId).first()) if checkExists: a = checkExists if (a.public or (user.labgroup in a.labgroup) or (a.username == user.username)): a.name = eName a.status = eStatus a.question = eQuestion a.answer = eAnswer a.tags = eTags try: a.lastmod = modTime except: a.lastmod = str(modTime) a.modeltree = eTree else: log.info("You do not have permission to modify this analysis.") return jsonify(success=("failed")) # If it doesn't exist, add new sql alchemy object with the # appropriate attributes, which should get assigned to a new id else: # TODO: Currently copies user's labgroup by default. # Is that the behavior we want? try: a = NarfAnalysis(name=eName, status=eStatus, question=eQuestion, answer=eAnswer, tags=eTags, batch='', lastmod=modTime, modeltree=eTree, username=user.username, labgroup=user.labgroup, public='0') except: a = NarfAnalysis(name=eName, status=eStatus, question=eQuestion, answer=eAnswer, tags=eTags, batch='', lastmod=str(modTime), modeltree=eTree, username=user.username, labgroup=user.labgroup, public='0') session.add(a) # For verifying correct logging - comment these out # when not needed for testing. #log.info("Added the following analysis to database:") #log.info("------------------") #log.info("name:"); log.info(a.name) #log.info("question:"); log.info(a.question) #log.info("answer:"); log.info(a.answer) #log.info("status:"); log.info(a.status) #log.info("tags:"); log.info(a.tags) #log.info("model tree:"); log.info(a.modeltree) #log.info("-----------------\n\n") addedName = a.name session.commit() session.close() # After handling submissions, return user to main page so that it # refreshes with new analysis included in list return jsonify(success="Analysis %s saved successfully." % addedName)