Example #1
0
File: app.py Project: LouisK130/oii
def init_database(dbengine, session):
    """initialize database"""
    Base.metadata.create_all(dbengine)
    session.expire_all()
    # init roles and test users
    # this should go somewhere else later
    for role in ['Admin','Instrument','Time Series', 'API']:
        if not session.query(Role).filter_by(name=role).count():
            r = Role(name=role)
            session.add(r)
            session.commit()
    if not session.query(User).filter_by(email='*****@*****.**').count():
        u = User(
            first_name='Test', last_name='Admin',
            email='*****@*****.**', username='******',
            password=user_manager.hash_password('12345678'),
            is_enabled=True)
        r = session.query(Role).filter_by(name='Admin').first()
        u.roles.append(r)
        session.add(u)
        session.commit()
    if not session.query(User).filter_by(email='*****@*****.**').count():
        u = User(
            first_name='Test', last_name='User',
            email='*****@*****.**', username='******',
            password=user_manager.hash_password('12345678'),
            is_enabled=True)
        session.add(u)
        session.commit()
Example #2
0
def promote_user(instid):
    user = session.query(User).filter_by(id=instid).first()
    adminRole = session.query(Role).filter_by(name='Admin').first()
    if not user:
        return "User not found", 404
    if not adminRole:
        return "Admin role not found", 404
    user.roles = [adminRole]
    session.commit()
    return "user promoted", 200
Example #3
0
def demote_user(instid):
    user = session.query(User).filter_by(id=instid).first()
    if not user:
        return "User not found", 404
    user.roles = []
    session.commit()
    return "user demoted", 200
Example #4
0
def delete_user(instid):
    user = session.query(User).filter_by(id=instid).first()
    if not user:
        return "User not found", 404
    session.delete(user)
    session.commit()
    return "user deleted", 200
Example #5
0
def set_password(instid):
    data = json.loads(request.data)
    user = session.query(User).filter_by(id=instid).first()
    if not user:
        return "User not found", 404
    # should eventually perform check in password complexity
    if data.has_key('password') and data['password']:
        user.password = user_manager.hash_password(data['password'])
        session.commit()
        return "password updated for user %s" % user.email, 200
    else:
        return "missing password", 400
Example #6
0
def patch_user(instid):
    data = json.loads(request.data)
    user = session.query(User).filter_by(id=instid).first()
    if not user:
        return "User not found", 404
    for k,v in data.items():
        try:
            if k not in ['id','password','roles']:
                setattr(user,k,v)
        except AttributeError:
            pass
    session.commit()
    return "user patched", 200
Example #7
0
def genkey(instid):
    data = json.loads(request.data)
    user = session.query(User).filter_by(id=instid).first()
    if not user:
        return "User not found", 404
    # should eventually perform check in password complexity
    if data.has_key('name') and data['name']:
        key = APIKey()
        key.name = data['name']
        key.user = user
        token = maketoken()
        key.token = token #encrypt later
        session.add(key)
        session.commit()
        return json.dumps({'token':token})
    else:
        return "missing key name", 400
Example #8
0
File: app.py Project: LouisK130/oii
def serve_timeseries(ts_label=None, pid=None):
    template = {
        'static': STATIC,
        'time_series': ts_label,
        'all_metrics': METRICS
    }
    if pid is not None:
        template['pid'] = pid
    # fetch time series information
    all_series = []
    url_root = get_url_root()
    for ts in session.query(TimeSeries).filter(TimeSeries.enabled).order_by(TimeSeries.label):
        if ts_label is None: # no time series specified
            return redirect(os.path.join(url_root, ts.label), code=302)
        if ts.label == ts_label:
            template['page_title'] = html.fromstring(ts.description).text_content()
            template['title'] = ts.description
        all_series.append((ts.label, ts.description))
    template['all_series'] = all_series
    template['base_url'] = url_root
    return template_response('timeseries.html', **template)
Example #9
0
File: app.py Project: LouisK130/oii
def parsed2files(parsed):
    b = session.query(Bin).filter(and_(Bin.lid==parsed['lid'],Bin.ts_label==parsed['ts_label'])).first()
    if b is None:
        abort(404)
    return b.files