예제 #1
0
def add_user(dict_to_insert):
    postgres = SESSION()
    try:
        postgres.add(UserInfo(**dict_to_insert))
        postgres.commit()
    except Exception as e:
        # logger
        postgres.rollback()
        print(e)
    finally:
        postgres.close()
예제 #2
0
def add_notification(user, container):
    postgres = SESSION()
    try:
        postgres.add(UserContainer(user_id=user, cont_id=container))
        postgres.commit()
    except Exception as e:
        # logger
        postgres.rollback()
        print(e)
    finally:
        postgres.close()
예제 #3
0
def add_state(dict_to_insert):
    postgres = SESSION()
    dict_to_insert['received_time'] = str(datetime.now())
    cont_id = dict_to_insert.get('cont_id')
    try:
        REDIS.hmset(cont_id, dict_to_insert)
        REDIS.expire(cont_id, 864000)  # key expires in 10days
        postgres.add(TelemetryState(**dict_to_insert))
        postgres.commit()
    except Exception as e:
        # logger
        postgres.rollback()
        print(e)
    finally:
        postgres.close()
예제 #4
0
def get_state(cont_id):
    postgres = SESSION()
    try:
        cont_info = REDIS.hgetall(cont_id)
        if cont_info == {}:
            cont_info = postgres.query(TelemetryState).filter(
                TelemetryState.cont_id == cont_id,
                TelemetryState.id == postgres.query(
                    func.max(TelemetryState.id))
            ).first()
            cont_info['received_time'] = datetime.strptime(
                cont_info['received_time'], '%Y-%m-%d %H:%M:%S.%f')
            REDIS.hmset(cont_info['cont_id'], cont_info)
            REDIS.expire(cont_id, 864000)  # key expires in 10days
    except Exception as e:
        # logger
        postgres.rollback()
        print(e)
    finally:
        postgres.close()
        return cont_info  # mb {} or None
예제 #5
0
def delete_from_notification(container):
    postgres = SESSION()
    postgres.query(UserContainer).filter(
        UserContainer.cont_id == container).delete()
    postgres.commit()
    postgres.close()
예제 #6
0
def autonotification_list():
    postgres = SESSION()
    container_list = (postgres.query(UserContainer).all())
    postgres.close()
    return container_list
예제 #7
0
def user_permission(nickname):
    postgres = SESSION()
    permission = postgres.query(UserInfo).filter(
        UserInfo.nickname == nickname).first()
    postgres.close()
    return permission.permission