Beispiel #1
0
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,
    )
Beispiel #2
0
def delete_analysis():
    """Delete the selected analysis from the database."""

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

    success = False
    aSelected = request.args.get('aSelected')
    if len(aSelected) == 0:
        return jsonify(success=success)

    result = (
            session.query(Analysis)
            .filter(Analysis.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)
Beispiel #3
0
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()
    Users = Tables()['Users']

    saved_selections = request.args.get('stringed_selections')
    user_entry = (
            session.query(Users)
            .filter(Users.username == user.username)
            .first()
            )
    user_entry.selections = saved_selections
    session.commit()
    session.close()

    return jsonify(response='selections saved', null=False)
Beispiel #4
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)
Beispiel #5
0
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 Analysis for current analysis.

    """

    session = Session()
    db_tables = Tables()
    Batches = db_tables['Batches']
    sBatch = db_tables['sBatch']
    Analysis = db_tables['Analysis']

    # Only get the numerals for the selected batch, not the description.
    bSelected = request.args.get('bSelected')
    aSelected = request.args.get('aSelected')
    search = request.args.get('cellSearch')

    global _previous_update_cells
    previous_batch, previous_celllist = _previous_update_cells
    if bSelected == previous_batch:
        celllist = previous_celllist
    else:
        celllist = [
                i[0] for i in
                session.query(Batches.cellid)
                .filter(Batches.batch == bSelected[:3])
                .all()
                ]
        _previous_update_cells = (bSelected, celllist)

    batchname = (
            session.query(sBatch)
            .filter(sBatch.id == bSelected[:3])
            .first()
            )
    if batchname:
        batch = str(bSelected[:3] + ': ' + batchname.name)
    else:
        batch = bSelected
    analysis = (
            session.query(Analysis)
            .filter(Analysis.name == aSelected)
            .first()
            )
    # don't change batch association if batch is blank
    if analysis and bSelected:
        analysis.batch = batch

    session.commit()
    session.close()

    # remove pairs
    filtered_cellids = [c for c in celllist if '+' not in c]
    # remove siteids
    filtered_cellids = [c for c in filtered_cellids if '-' in c]
    # filter by search string
    filtered_cellids = simple_search(search, filtered_cellids)

    return jsonify(celllist=filtered_cellids)