示例#1
0
文件: views.py 项目: LBHB/nems_db
def register():
    errors = ''
    form = RegistrationForm(request.form)
    if request.method == 'POST':
        if form.validate():
            session = Session()
            Users = Tables()['Users']
            new_user = Users(
                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
def edit_analysis():
    """Take input from Analysis Editor modal and save it to the database.

    Button : Edit Analysis

    """

    user = get_current_user()
    session = Session()
    Analysis = Tables()['Analysis']

    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(Analysis)
                .filter(Analysis.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 = Analysis(
                    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 = Analysis(
                    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()

    # Clear out cached analysis selection, otherwise the simple caching logic
    # thinks the analysis selection didn't change so it will load previous
    # modeltree
    global _previous_update_models
    _previous_update_models = ('', '', '', [])

    # 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)