Example #1
0
def setnewpassword(pid='-1', hash=''):
    session_init(session, db)

    if request.method == 'POST':

        password = request.form['password']
        password2 = request.form['password2']
        hash = request.form['hash']
        email = request.form['email']

        if len(password) < 7:
            error = 'password must be at least 7 characters long'
        elif password != password2:
            error = 'passwords must match.'
        elif not db.person_test_confirmhash(email, hash):
            error = 'password reset was not requested correctly.'
        else:
            db.set_person_password(email, password)
            hash = db.person_unconfirm(email)
            db.person_confirm(email, hash)
            flash("Your password has been reset.")
            error = None
            return render_template('login.html', error=error)

        return render_template('newpassword_form.html',
                               hash=hash,
                               email=email,
                               error=error)

    return render_template('password_reset_request.html')
Example #2
0
def list_facilities():
    session_init(session, db)
    error = None
    facilities = db.filtered_query('facility')
    return render_template('facilities_list.html',
                           error=error,
                           facilities=facilities)
Example #3
0
def all():
    session_init(session, db)
    spectra = []
    dbspectra = db.get_spectra()
    for s in dbspectra:
        edge = db.get_edge(s.edge_id)
        elem_sym = db.get_element(s.element_z).symbol
        elem = s.element_z
        person = db.get_person(s.person_id)

        bl_id, bl_desc = beamline_for_spectrum(db, s)

        spectra.append({
            'id': s.id,
            'name': s.name,
            'element': elem,
            'edge': edge.name,
            'person_email': person.email,
            'person_name': person.name,
            'elem_sym': elem_sym,
            'rating': get_rating(s),
            'beamline_desc': bl_desc,
            'beamline_id': bl_id,
        })

    return render_template('ptable.html',
                           nspectra=len(dbspectra),
                           elem='All Elements',
                           spectra=spectra)
Example #4
0
def user():
    # show the user profile for that user
    session_init(session, db)
    error = None
    if request.method == 'POST':
        email = request.form['email']
        name = request.form['name']
        affiliation = request.form['affiliation']
        person = db.get_person(email)

        ptab = db.tables['person']
        kws = {}
        if name != person.name:
            kws['name'] = name
        if affiliation != person.affiliation:
            kws['affiliation'] = affiliation
        if len(kws) > 1:
            ptab.update(whereclause="email='%s'" % email).execute(**kws)

    elif 'username' not in session:
        error = 'Not logged in'
        email, name, affiliation = '', '', ''
    else:
        email = session['username']
        person = db.get_person(email)
        name = person.name
        affiliation = person.affiliation

    return render_template('userprofile.html',
                           error=error,
                           email=email,
                           name=name,
                           affiliation=affiliation)
def submit_sample_edits():
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit sample'
        return render_template('ptable.html', error=error)
    sid = 0
    if request.method == 'POST':
        sid  = request.form['sample_id']
        pid  = request.form['person_id']
        name = request.form['name']
        notes = request.form['notes']
        prep = request.form['preparation']
        formula = request.form['formula']
        source = request.form['material_source']
        # xtal_format = request.form['xtal_format']
        # xtal_data   = request.form['xtal_data']

        db.update('sample', int(sid),
                  person=pid,
                  name=name,
                  notes=notes,
                  formula=formula,
                  material_source=source,
                  preparation=prep)
        time.sleep(0.25)
    return redirect(url_for('sample', sid=sid, error=error))
def suites(stid=None):
    session_init(session, db)
    suites = []
    if stid is None:
        for st in db.filtered_query('suite'):
            name, notes, person_id = st.name, st.notes, st.person_id
            person_email = db.get_person(person_id).email
            spectra = spectra_for_suite(db, st.id)

            is_owner = (int(session['person_id']) == int(st.person_id))
            suites.append({'id': st.id, 'name': name, 'notes': notes,
                           'person_email': person_email,
                           'rating': get_rating(st),
                           'suite_owner': is_owner,
                           'nspectra': len(spectra), 'spectra': spectra})

    else:
        st = db.filtered_query('suite', id=stid)[0]
        name, notes, person_id = st.name, st.notes, st.person_id
        person_email = db.get_person(person_id).email
        spectra = spectra_for_suite(db, stid)

        is_owner = (int(session['person_id']) == int(st.person_id))
        suites.append({'id': stid, 'name': name, 'notes': notes,
                       'person_email': person_email,
                       'rating': get_rating(st),
                       'suite_owner': is_owner,
                       'nspectra': len(spectra), 'spectra': spectra})
    return render_template('suites.html', nsuites=len(suites), suites=suites)
Example #7
0
def submit_suite_rating(spid=None):
    session_init(session, db)
    error = None

    if session['username'] is None:
        error = 'must be logged in to rate suite'
        return redirect(url_for('suite', spid=spid, error=error))

    if request.method == 'POST':
        score_is_valid = False
        try:
            score = float(request.form['score'])
            vscore = valid_score(score)
            score_is_valid = ((int(score) == vscore)
                              and (abs(score - int(score)) < 1.e-3))
        except:
            pass

        review = request.form['review']
        stname = request.form['suite_name']
        stid = int(request.form['suite'])
        pid = int(request.form['person'])

        if score_is_valid:
            db.set_suite_rating(pid, stid, vscore, comments=review)
            return redirect(url_for('suites', spid=spid))
        else:
            error = 'score must be an integer:  0, 1, 2, 3, 4, or 5'
            return render_template('ratesuite.html',
                                   error=error,
                                   suite_id=stid,
                                   suite_name=stname,
                                   person_id=pid,
                                   score=score,
                                   review=multilne_text(review))
Example #8
0
def add_facility():
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to add a facility'
        return redirect(url_for('beamlines', error=error))

    if request.method == 'POST':
        fac_name = request.form['facility_name']
        _facnames = [s.name for s in db.filtered_query('facility')]
        try:
            fac_name = unique_name(fac_name,
                                   _facnames,
                                   msg='facility',
                                   maxcount=5)
        except:
            error = 'a facility named %s exists'
        db.add_beamline(fac_name)
        time.sleep(1)
        return redirect(url_for('list_facilities', error=error))
    else:
        facilities = []
        for r in db.filtered_query('facility'):
            facilities.append({'id': r.id, 'name': r.name})
        return render_template('add_facility.html',
                               error=error,
                               facilities=facilities)
Example #9
0
def rate_suite(stid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to rate suite'
        return redirect(url_for('suites', stid=stid, error=error))

    for st in db.filtered_query('suite'):
        if st.id == stid:
            stname, notes, person_id = st.name, st.notes, st.person_id

    pid = session['person_id']
    score = 3
    review = '<review>'
    for _s, _r, _d, _p in suite_ratings(db, stid):
        if _p == pid:
            score = _s
            review = _r

    return render_template('ratesuite.html',
                           error=error,
                           suite_id=stid,
                           suite_name=stname,
                           person_id=pid,
                           score=score,
                           review=review)
def setnewpassword(pid='-1', hash=''):
    session_init(session, db)

    if request.method == 'POST':

        password = request.form['password']
        password2 = request.form['password2']
        hash      = request.form['hash']
        email     = request.form['email']

        if len(password) < 7:
            error = 'password must be at least 7 characters long'
        elif password != password2:
            error = 'passwords must match.'
        elif not db.person_test_confirmhash(email, hash):
            error = 'password reset was not requested correctly.'
        else:
            db.set_person_password(email, password)
            hash = db.person_unconfirm(email)
            db.person_confirm(email, hash)
            flash("Your password has been reset.")
            error = None
            return render_template('login.html', error=error)

        return render_template('newpassword_form.html', hash=hash,
                               email=email, error=error)

    return render_template('password_reset_request.html')
Example #11
0
def showsuite_rating(stid=None):
    session_init(session, db)

    for st in db.filtered_query('suite'):
        if st.id == stid:
            stname, notes, _pid = st.name, st.notes, st.person_id
    if stname is None:
        error = 'Could not find Suite #%i' % stid
        return render_template('ptable.html', error=error)

    ratings = []
    for score, review, dtime, pid in suite_ratings(db, stid):
        person = db.get_person(pid)
        ratings.append({
            'score': score,
            'review': multiline_text(review),
            'date': fmttime(dtime),
            'person_email': person.email,
            'person_name': person.name,
            'person_affil': person.affiliation
        })

    return render_template('show_suite_ratings.html',
                           ratings=ratings,
                           suite_notes=notes,
                           suite_name=stname)
Example #12
0
def rawfile(spid, fname):
    session_init(session, db)
    s = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable.html', error=error)
    return Response(s.filetext, mimetype='text/plain')
def upload():
    session_init(session, db)
    if session['username'] is None:
        error='must be logged in to submit a spectrum'
        return redirect(url_for('search', error=error))
    return render_template('upload.html',
                           person_id=session['person_id'])
def rate_spectrum(spid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to rate spectrum'
        return redirect(url_for('spectrum', spid=spid, error=error))

    pid = session['person_id']
    s  = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable', error=error)

    opts = parse_spectrum(s, db)
    score = 3
    review = '<review>'
    for _s, _r, _d, _p in spectrum_ratings(db, spid):
        if int(_p) == int(pid):
            score = _s
            review =  _r
    spid = s.id
    spname = s.name

    return render_template('ratespectrum.html', error=error,
                           spectrum_id=spid, spectrum_name=spname,
                           person_id=pid, score=score, review=review)
def user():
    # show the user profile for that user
    session_init(session, db)
    error = None
    if request.method == 'POST':
        email = request.form['email']
        name  = request.form['name']
        affiliation = request.form['affiliation']
        person = db.get_person(email)

        ptab = db.tables['person']
        kws = {}
        if name != person.name:
            kws['name'] = name
        if affiliation != person.affiliation:
            kws['affiliation'] = affiliation
        if len(kws) > 1:
            ptab.update(whereclause="email='%s'" % email).execute(**kws)

    elif 'username' not in session:
        error = 'Not logged in'
        email, name, affiliation = '', '', ''
    else:
        email = session['username']
        person = db.get_person(email)
        name = person.name
        affiliation = person.affiliation

    return render_template('userprofile.html', error=error, email=email,
                           name=name, affiliation=affiliation)
def submit_suite_rating(spid=None):
    session_init(session, db)
    error=None

    if session['username'] is None:
        error='must be logged in to rate suite'
        return redirect(url_for('suite', spid=spid, error=error))

    if request.method == 'POST':
        score_is_valid = False
        try:
            score = float(request.form['score'])
            vscore = valid_score(score)
            score_is_valid = ((int(score) == vscore) and
                              (abs(score-int(score)) < 1.e-3))
        except:
            pass

        review = request.form['review']
        stname = request.form['suite_name']
        stid   = int(request.form['suite'])
        pid    = int(request.form['person'])

        if score_is_valid:
            db.set_suite_rating(pid, stid, vscore, comments=review)
            return redirect(url_for('suites', spid=spid))
        else:
            error='score must be an integer:  0, 1, 2, 3, 4, or 5'
            return render_template('ratesuite.html', error=error,
                                   suite_id=stid, suite_name=stname,
                                   person_id=pid, score=score,
                                   review=multilne_text(review))
Example #17
0
def rate_spectrum(spid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to rate spectrum'
        return redirect(url_for('spectrum', spid=spid, error=error))

    pid = session['person_id']
    s = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable', error=error)

    opts = parse_spectrum(s, db)
    score = 3
    review = '<review>'
    for _s, _r, _d, _p in spectrum_ratings(db, spid):
        if int(_p) == int(pid):
            score = _s
            review = _r
    spid = s.id
    spname = s.name

    return render_template('ratespectrum.html',
                           error=error,
                           spectrum_id=spid,
                           spectrum_name=spname,
                           person_id=pid,
                           score=score,
                           review=review)
Example #18
0
def submit_sample_edits():
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to edit sample'
        return render_template('ptable.html', error=error)
    sid = 0
    if request.method == 'POST':
        sid = request.form['sample_id']
        pid = request.form['person_id']
        name = request.form['name']
        notes = request.form['notes']
        prep = request.form['preparation']
        formula = request.form['formula']
        source = request.form['material_source']
        # xtal_format = request.form['xtal_format']
        # xtal_data   = request.form['xtal_data']

        db.update('sample',
                  int(sid),
                  person=pid,
                  name=name,
                  notes=notes,
                  formula=formula,
                  material_source=source,
                  preparation=prep)
        time.sleep(0.25)
    return redirect(url_for('sample', sid=sid, error=error))
def all():
    session_init(session, db)
    spectra = []
    dbspectra = db.get_spectra()
    for s in dbspectra:
        edge     = db.get_edge(s.edge_id)
        elem_sym = db.get_element(s.element_z).symbol
        elem     = s.element_z
        person   = db.get_person(s.person_id)

        bl_id, bl_desc = beamline_for_spectrum(db, s)

        spectra.append({'id': s.id,
                        'name': s.name,
                        'element': elem,
                        'edge': edge.name,
                        'person_email': person.email,
                        'person_name': person.name,
                        'elem_sym': elem_sym,
                        'rating': get_rating(s),
                        'beamline_desc': bl_desc,
                        'beamline_id': bl_id,
                        })

    return render_template('ptable.html', nspectra=len(dbspectra),
                           elem='All Elements', spectra=spectra)
def rawfile(spid, fname):
    session_init(session, db)
    s  = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable.html', error=error)
    return Response(s.filetext, mimetype='text/plain')
def add_beamline():
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to add a beamline'
        return redirect(url_for('beamlines', error=error))

    if request.method == 'POST':
        bl_name = request.form['beamline_name']
        notes = request.form['notes']
        fac_id = int(request.form['fac_id'])
        source = request.form['xray_source']

        _blnames = [s.name for s in db.filtered_query('beamline')]
        try:
            bl_name = unique_name(bl_name, _blnames,
                                  msg='beamline', maxcount=5)
        except:
            error = 'a beamline named %s exists'

        db.add_beamline(bl_name, notes=notes, xray_source=source,
                        facility_id=fac_id)

        time.sleep(1)
        return redirect(url_for('beamlines', error=error))
    else:
        facilities = []
        for r in db.filtered_query('facility'):
            facilities.append({'id': r.id, 'name': r.name})
        return render_template('add_beamline.html', error=error,
                               facilities=facilities)
Example #22
0
def sample(sid=None):
    session_init(session, db)
    samples = []
    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('sample.html', sid=sid, **opts)
Example #23
0
def add_citation(spid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to add citation for spectrum'
        return redirect(url_for('ptable', error=error))

    return render_template('add_citation.html', spid=spid)
def sample(sid=None):
    session_init(session, db)
    samples = []
    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('sample.html', sid=sid, **opts)
def add_citation(spid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to add citation for spectrum'
        return redirect(url_for('ptable',  error=error))

    return render_template('add_citation.html', spid=spid)
Example #26
0
def newpassword(pid='-1', hash=''):
    session_init(session, db)
    if pid > 0 and len(hash) > 8:
        pid = int(pid)
        person = db.get_person(pid, key='id')
        email = person.email
        return render_template('newpassword_form.html', hash=hash, email=email)
    return render_template('password_reset_request.html')
def newpassword(pid='-1', hash=''):
    session_init(session, db)
    if pid > 0 and len(hash) > 8:
        pid = int(pid)
        person = db.get_person(pid, key='id')
        email = person.email
        return render_template('newpassword_form.html',
                               hash=hash, email=email)
    return render_template('password_reset_request.html')
def edit_sample(sid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit sample'
        return render_template('ptable.html', error=error)

    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('editsample.html', sid=sid, **opts)
Example #29
0
def edit_sample(sid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to edit sample'
        return render_template('ptable.html', error=error)

    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('editsample.html', sid=sid, **opts)
def beamline(blid=None):
    session_init(session, db)
    beamlines = []
    for _bldat in get_beamline_list(db):
        if _bldat['id'] == "%i" % blid:
            bldat = _bldat
            break

    spectra = spectra_for_beamline(db, blid)
    opts = {'nspectra': len(spectra), 'spectra': spectra}
    opts.update(bldat)
    beamlines.append(opts)

    return render_template('beamlines.html',
                           nbeamlines=len(beamlines), beamlines=beamlines)
def password_reset_request():
    session_init(session, db)
    error = None
    if request.method == 'POST':
        email = request.form['email']
        person = db.get_person(email)
        # log in existing
        if person is None:
            error = "No account with email '%s' exists" % email
        else:
            hash = db.person_unconfirm(email)
            send_confirm_email(person, hash, style='reset')
            return render_template('password_reset_response.html',
                                   email=email, error=error)

    return render_template('password_reset_request.html', error=error)
Example #32
0
def beamline(blid=None):
    session_init(session, db)
    beamlines = []
    for _bldat in get_beamline_list(db):
        if _bldat['id'] == "%i" % blid:
            bldat = _bldat
            break

    spectra = spectra_for_beamline(db, blid)
    opts = {'nspectra': len(spectra), 'spectra': spectra}
    opts.update(bldat)
    beamlines.append(opts)

    return render_template('beamlines.html',
                           nbeamlines=len(beamlines),
                           beamlines=beamlines)
def delete_suite(stid, ask=1):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to delete a suite'
        return redirect(url_for('suites', error=error))

    suite_name = db.filtered_query('suite', id=stid)[0].name
    if ask != 0:
        return render_template('confirm_del_suite.html', stid=stid,
                               suite_name=suite_name)

    else:
        db.del_suite(stid)
        time.sleep(1)
        flash('Deleted suite %s' % s_name)
    return redirect(url_for('suites', error=error))
Example #34
0
def submit_upload():
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to submit a spectrum'
        return redirect(url_for('search', error=error))

    if request.method == 'POST':
        pid = request.form['person']
        pemail = db.get_person(int(pid)).email
        file = request.files['file']
        s = None
        file_ok = False
        if file and allowed_file(file.filename):
            fname = secure_filename(file.filename)
            fullpath = os.path.abspath(
                os.path.join(app.config['UPLOAD_FOLDER'], fname))
            try:
                file.save(fullpath)
                file_ok = True
            except IOError:
                pass

            if file_ok:
                time.sleep(1.0)
                db.add_xdifile(fullpath, person=pemail, create_sample=True)
                time.sleep(1.0)
                db.session.commit()

            s = db.get_spectra()[-1]
            if s is None:
                error = 'Could not find Spectrum #%i' % s.id
                return render_template('upload.html', error=error)

        if s is None:
            error = "File '%s' not found or not suppported type" % (
                file.filename)
            return render_template('upload.html', error=error)

        try:
            opts = parse_spectrum(s, db)
        except:
            error = "Could not read spectrum from '%s'" % (file.filename)
            return render_template('upload.html', error=error)
        return redirect(url_for('spectrum', spid=s.id, error=error))
    return render_template('upload.html', error='upload error')
Example #35
0
def password_reset_request():
    session_init(session, db)
    error = None
    if request.method == 'POST':
        email = request.form['email']
        person = db.get_person(email)
        # log in existing
        if person is None:
            error = "No account with email '%s' exists" % email
        else:
            hash = db.person_unconfirm(email)
            send_confirm_email(person, hash, style='reset')
            return render_template('password_reset_response.html',
                                   email=email,
                                   error=error)

    return render_template('password_reset_request.html', error=error)
def submit_upload():
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to submit a spectrum'
        return redirect(url_for('search', error=error))

    if request.method == 'POST':
        pid    = request.form['person']
        pemail = db.get_person(int(pid)).email
        file = request.files['file']
        s = None
        file_ok = False
        if file and allowed_file(file.filename):
            fname = secure_filename(file.filename)
            fullpath = os.path.abspath(os.path.join(
                app.config['UPLOAD_FOLDER'], fname))
            try:
                file.save(fullpath)
                file_ok = True
            except IOError:
                pass

            if file_ok:
                time.sleep(1.0)
                db.add_xdifile(fullpath, person=pemail, create_sample=True)
                time.sleep(1.0)
                db.session.commit()

            s  = db.get_spectra()[-1]
            if s is None:
                error = 'Could not find Spectrum #%i' % s.id
                return render_template('upload.html', error=error)

        if s is None:
            error = "File '%s' not found or not suppported type" %  (file.filename)
            return render_template('upload.html', error=error)

        try:
            opts = parse_spectrum(s, db)
        except:
            error = "Could not read spectrum from '%s'" % (file.filename)
            return render_template('upload.html', error=error)
        return redirect(url_for('spectrum', spid=s.id, error=error))
    return render_template('upload.html', error='upload error')
Example #37
0
def delete_suite(stid, ask=1):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to delete a suite'
        return redirect(url_for('suites', error=error))

    suite_name = db.filtered_query('suite', id=stid)[0].name
    if ask != 0:
        return render_template('confirm_del_suite.html',
                               stid=stid,
                               suite_name=suite_name)

    else:
        db.del_suite(stid)
        time.sleep(1)
        flash('Deleted suite %s' % s_name)
    return redirect(url_for('suites', error=error))
def delete_spectrum(spid, ask=1):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to delete a spectrum'
        return redirect(url_for('search', error=error))

    s_name = db.filtered_query('spectrum', id=spid)[0].name
    if ask != 0:
        return render_template('confirm_delete_spectrum.html',
                               spectrum_id=spid,
                               spectrum_name=s_name)

    else:
        db.del_spectrum(spid)
        time.sleep(1)
        flash('Deleted spectrum %s' % s_name)
    return redirect(url_for('search', error=error))
Example #39
0
def delete_spectrum(spid, ask=1):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to delete a spectrum'
        return redirect(url_for('search', error=error))

    s_name = db.filtered_query('spectrum', id=spid)[0].name
    if ask != 0:
        return render_template('confirm_delete_spectrum.html',
                               spectrum_id=spid,
                               spectrum_name=s_name)

    else:
        db.del_spectrum(spid)
        time.sleep(1)
        flash('Deleted spectrum %s' % s_name)
    return redirect(url_for('search', error=error))
def add_spectrum_to_suite(spid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to add spectrum to suite'
        return redirect(url_for('spectrum', spid=spid, error=error))

    s  = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable', error=error)

    suites = []
    for st in db.filtered_query('suite'):
        suites.append({'id': st.id, 'name': st.name})

    return render_template('add_spectrum_to_suite.html', error=error,
                           spectrum_id=spid, spectrum_name=s.name,
                           person_id=session['person_id'], suites=suites)
def edit_suite(stid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit suite'
        return render_template('ptable.html', error=error)

    st = db.filtered_query('suite', id=stid)[0]
    name, notes, person_id = st.name, st.notes, st.person_id
    person_email = db.get_person(person_id).email
    spectra = spectra_for_suite(db, stid)

    is_owner = (int(session['person_id']) == int(st.person_id))
    opts = {'id': stid, 'name': name, 'notes': notes,
            'person_email': person_email,
            'rating': get_rating(st),
            'suite_owner': is_owner,
            'nspectra': len(spectra), 'spectra': spectra}
    return render_template('edit_suite.html', **opts)
Example #42
0
def search(elem=None, orderby=None, reverse=0):
    session_init(session, db)
    dbspectra = []
    if orderby is None: orderby = 'id'
    if elem is not None:
        try:
            dbspectra = db.get_spectra(element=elem, orderby=orderby)
        except:
            pass

    reverse = int(reverse)
    if reverse:
        dbspectra.reverse()
        reverse = 0
    else:
        reverse = 1

    spectra = []
    for s in dbspectra:
        edge = db.get_edge(s.edge_id)
        elem_sym = db.get_element(s.element_z).symbol
        person = db.get_person(s.person_id)
        bl_id, bl_desc = beamline_for_spectrum(db, s)

        spectra.append({
            'id': s.id,
            'name': s.name,
            'element': elem,
            'edge': edge.name,
            'person_email': person.email,
            'person_name': person.name,
            'elem_sym': elem_sym,
            'rating': get_rating(s),
            'beamline_desc': bl_desc,
            'beamline_id': bl_id,
        })

    return render_template('ptable.html',
                           nspectra=len(dbspectra),
                           elem=elem,
                           spectra=spectra,
                           reverse=reverse)
def submit_suite_edits():
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit suite'
        return render_template('ptable.html', error=error)
    if request.method == 'POST':
        stid  = int(request.form['suite'])
        db.update('suite', stid,
                  name=request.form['name'],
                  notes=request.form['comments'])

        for spec in spectra_for_suite(db, stid):
            spid = int(spec['spectrum_id'])
            key = 'spec_%i' % spid
            if key not in request.form:
                db.remove_spectrum_from_suite(stid, spid)
        time.sleep(0.25)

    return redirect(url_for('suites', stid=stid, error=error))
def submit_spectrum_edits():
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit spectrum'
        return render_template('ptable.html', error=error)
    spid = 0
    if request.method == 'POST':
        spid  = int(request.form['spectrum'])
        db.update('spectrum', int(spid),
                  name=request.form['name'],
                  comments=request.form['comments'],
                  d_spacing=float(request.form['d_spacing']),
                  edge_id= int(request.form['edge']),
                  beamline_id= int(request.form['beamline']),
                  sample_id= int(request.form['sample']),
                  energy_units_id=int(request.form['energy_units']))

        time.sleep(0.25)

    return redirect(url_for('spectrum', spid=spid, error=error))
def beamlines(blid=None, orderby='id', reverse=0):
    session_init(session, db)
    beamlines = []

    for bldat in get_beamline_list(db, orderby=orderby):
        blid = bldat['id']
        spectra = spectra_for_beamline(db, blid)
        opts = {'nspectra': len(spectra), 'spectra': spectra}
        opts.update(bldat)
        beamlines.append(opts)

    reverse = int(reverse)
    if reverse:
        beamlines.reverse()
        reverse = 0
    else:
        reverse = 1

    return render_template('beamlines.html',
                           nbeamlines=len(beamlines),
                           beamlines=beamlines, reverse=reverse)
def showspectrum_rating(spid=None):
    session_init(session, db)
    s  = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable.html', error=error)

    opts = parse_spectrum(s, db)
    ratings = []
    for score, review, dtime, pid in spectrum_ratings(db, spid):
        person = db.get_person(pid)
        ratings.append({'score': score,
                        'review': multiline_text(review),
                        'date': fmttime(dtime),
                        'person_email': person.email,
                        'person_name': person.name,
                        'person_affil': person.affiliation})

    return render_template('show_spectrum_ratings.html',
                           ratings=ratings,
                           spectrum_name=opts['spectrum_name'])
def edit_spectrum(spid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit spectrum'
        return render_template('ptable.html',
                               error=error)

    s  = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable.html', error=error)

    opts = parse_spectrum(s, db)
    return render_template('editspectrum.html', error=error,
                           elems=get_element_list(db),
                           eunits=get_energy_units_list(db),
                           edges=get_edge_list(db),
                           beamlines=get_beamline_list(db),
                           samples=get_sample_list(db),
                           **opts)
Example #48
0
def submit_suite_edits():
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to edit suite'
        return render_template('ptable.html', error=error)
    if request.method == 'POST':
        stid = int(request.form['suite'])
        db.update('suite',
                  stid,
                  name=request.form['name'],
                  notes=request.form['comments'])

        for spec in spectra_for_suite(db, stid):
            spid = int(spec['spectrum_id'])
            key = 'spec_%i' % spid
            if key not in request.form:
                db.remove_spectrum_from_suite(stid, spid)
        time.sleep(0.25)

    return redirect(url_for('suites', stid=stid, error=error))
Example #49
0
def edit_spectrum(spid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to edit spectrum'
        return render_template('ptable.html', error=error)

    s = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable.html', error=error)

    opts = parse_spectrum(s, db)
    return render_template('editspectrum.html',
                           error=error,
                           elems=get_element_list(db),
                           eunits=get_energy_units_list(db),
                           edges=get_edge_list(db),
                           beamlines=get_beamline_list(db),
                           samples=get_sample_list(db),
                           **opts)
Example #50
0
def beamlines(blid=None, orderby='id', reverse=0):
    session_init(session, db)
    beamlines = []

    for bldat in get_beamline_list(db, orderby=orderby):
        blid = bldat['id']
        spectra = spectra_for_beamline(db, blid)
        opts = {'nspectra': len(spectra), 'spectra': spectra}
        opts.update(bldat)
        beamlines.append(opts)

    reverse = int(reverse)
    if reverse:
        beamlines.reverse()
        reverse = 0
    else:
        reverse = 1

    return render_template('beamlines.html',
                           nbeamlines=len(beamlines),
                           beamlines=beamlines,
                           reverse=reverse)
def showsuite_rating(stid=None):
    session_init(session, db)

    for st in db.filtered_query('suite'):
        if st.id == stid:
            stname, notes, _pid = st.name, st.notes, st.person_id
    if stname is None:
        error = 'Could not find Suite #%i' % stid
        return render_template('ptable.html', error=error)

    ratings = []
    for score, review, dtime, pid in suite_ratings(db, stid):
        person = db.get_person(pid)
        ratings.append({'score': score, 'review': multiline_text(review),
                        'date': fmttime(dtime),
                        'person_email': person.email,
                        'person_name': person.name,
                        'person_affil': person.affiliation})

    return render_template('show_suite_ratings.html',
                           ratings=ratings, suite_notes=notes,
                           suite_name=stname)
Example #52
0
def suites(stid=None):
    session_init(session, db)
    suites = []
    if stid is None:
        for st in db.filtered_query('suite'):
            name, notes, person_id = st.name, st.notes, st.person_id
            person_email = db.get_person(person_id).email
            spectra = spectra_for_suite(db, st.id)

            is_owner = (int(session['person_id']) == int(st.person_id))
            suites.append({
                'id': st.id,
                'name': name,
                'notes': notes,
                'person_email': person_email,
                'rating': get_rating(st),
                'suite_owner': is_owner,
                'nspectra': len(spectra),
                'spectra': spectra
            })

    else:
        st = db.filtered_query('suite', id=stid)[0]
        name, notes, person_id = st.name, st.notes, st.person_id
        person_email = db.get_person(person_id).email
        spectra = spectra_for_suite(db, stid)

        is_owner = (int(session['person_id']) == int(st.person_id))
        suites.append({
            'id': stid,
            'name': name,
            'notes': notes,
            'person_email': person_email,
            'rating': get_rating(st),
            'suite_owner': is_owner,
            'nspectra': len(spectra),
            'spectra': spectra
        })
    return render_template('suites.html', nsuites=len(suites), suites=suites)
Example #53
0
def create_account():
    session_init(session, db)
    error = None
    if request.method == 'POST':
        email = request.form['email']
        name = request.form['name']
        affiliation = request.form['affiliation']
        password = request.form['password']
        password2 = request.form['password2']
        person = db.get_person(email)
        # log in existing
        if person is not None:
            error = "Account with email '%s' exists" % email
        else:
            if len(name) < 4:
                error = 'Must give a valid name (at least 4 characters)'
            elif '@' not in email or '.' not in email or len(email) < 10:
                error = 'Must give a valid email address'
            elif len(password) < 7:
                error = 'password must be at least 7 characters long'
            elif password != password2:
                error = 'passwords must match.'
            else:
                db.add_person(name,
                              email,
                              password=password,
                              affiliation=affiliation)
                hash = db.person_unconfirm(email)
                if LOCAL_ONLY:
                    db.person_confirm(email, hash)
                else:
                    ## send email here!!
                    person = db.get_person(email)
                    send_confirm_email(person, hash, style='new')
                    return render_template('confirmation_email_sent.html',
                                           email=email,
                                           error=error)

    return render_template('create_account.html', error=error)
Example #54
0
def add_spectrum_to_suite(spid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to add spectrum to suite'
        return redirect(url_for('spectrum', spid=spid, error=error))

    s = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable', error=error)

    suites = []
    for st in db.filtered_query('suite'):
        suites.append({'id': st.id, 'name': st.name})

    return render_template('add_spectrum_to_suite.html',
                           error=error,
                           spectrum_id=spid,
                           spectrum_name=s.name,
                           person_id=session['person_id'],
                           suites=suites)
Example #55
0
def confirmaccount(pid='-1', hash=''):
    session_init(session, db)
    error = 'Could not confirm an account with that information'
    if pid > 0 and len(hash) > 8:
        pid = int(pid)
        person = db.get_person(pid, key='id')
        if person is None:
            error = 'Could not locate account'
        elif person.confirmed == 'true':
            error = 'The account for %s is already confirmed' % person.email
        else:
            confirmed = db.person_confirm(person.email, hash)
            if not confirmed:
                error = 'Confirmation key incorrect for %s' % person.email
            else:
                flash('''Congratulations, %s, your account is confirmed.
                You can now log in!''' % person.name)
                # notify
                notify_account_creation(person)

                return render_template('login.html')
    return render_template('confirmation_email_sent.html', error=error)
Example #56
0
def login():
    session_init(session, db)
    error = None
    session['username'] = None
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']
        person = db.get_person(email)
        session['person_id'] = "-1"
        if person is None:
            error = 'Invalid email'
        elif not db.test_person_confirmed(email):
            error = 'Account not confirmed'
        elif not db.test_person_password(email, password):
            error = 'Invalid password'
        else:
            session['person_id'] = "%i" % person.id
            session['username'] = request.form['email']
    if session['username'] is not None:
        return redirect(url_for('search'))
    else:
        return render_template('login.html', error=error)
def login():
    session_init(session, db)
    error = None
    session['username'] = None
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']
        person = db.get_person(email)
        session['person_id'] = "-1"
        if person is None:
            error = 'Invalid email'
        elif not db.test_person_confirmed(email):
            error = 'Account not confirmed'
        elif not db.test_person_password(email, password):
            error = 'Invalid password'
        else:
            session['person_id'] = "%i" % person.id
            session['username'] = request.form['email']
    if session['username'] is not None:
        return redirect(url_for('search'))
    else:
        return render_template('login.html', error=error)
def search(elem=None, orderby=None, reverse=0):
    session_init(session, db)
    dbspectra = []
    if orderby is None: orderby = 'id'
    if elem is not None:
        try:
            dbspectra = db.get_spectra(element=elem, orderby=orderby)
        except:
            pass

    reverse = int(reverse)
    if reverse:
        dbspectra.reverse()
        reverse = 0
    else:
        reverse = 1

    spectra = []
    for s in dbspectra:
        edge     = db.get_edge(s.edge_id)
        elem_sym = db.get_element(s.element_z).symbol
        person   = db.get_person(s.person_id)
        bl_id, bl_desc = beamline_for_spectrum(db, s)

        spectra.append({'id': s.id,
                        'name': s.name,
                        'element': elem,
                        'edge': edge.name,
                        'person_email': person.email,
                        'person_name': person.name,
                        'elem_sym': elem_sym,
                        'rating': get_rating(s),
                        'beamline_desc': bl_desc,
                        'beamline_id': bl_id,
                        })

    return render_template('ptable.html', nspectra=len(dbspectra),
                           elem=elem, spectra=spectra,
                           reverse=reverse)
def rate_suite(stid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to rate suite'
        return redirect(url_for('suites', stid=stid, error=error))

    for st in db.filtered_query('suite'):
        if st.id == stid:
            stname, notes, person_id = st.name, st.notes, st.person_id

    pid = session['person_id']
    score = 3
    review = '<review>'
    for _s, _r, _d, _p in suite_ratings(db, stid):
        if _p == pid:
            score = _s
            review =  _r

    return render_template('ratesuite.html', error=error,
                           suite_id=stid, suite_name=stname,
                           person_id=pid, score=score, review=review)
Example #60
0
def submit_spectrum_edits():
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to edit spectrum'
        return render_template('ptable.html', error=error)
    spid = 0
    if request.method == 'POST':
        spid = int(request.form['spectrum'])
        db.update('spectrum',
                  int(spid),
                  name=request.form['name'],
                  comments=request.form['comments'],
                  d_spacing=float(request.form['d_spacing']),
                  edge_id=int(request.form['edge']),
                  beamline_id=int(request.form['beamline']),
                  sample_id=int(request.form['sample']),
                  energy_units_id=int(request.form['energy_units']))

        time.sleep(0.25)

    return redirect(url_for('spectrum', spid=spid, error=error))