コード例 #1
0
ファイル: views.py プロジェクト: nadoss/nems_db
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,
    )
コード例 #2
0
ファイル: views.py プロジェクト: nadoss/nems_db
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)
コード例 #3
0
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)