Ejemplo n.º 1
0
def get_to_split(user_id):
    session = Session()

    access = session.query(tables.Access).filter(tables.Access.user_id == user_id).all()

    foreign_ids = []
    for ac in access:
        foreign_ids.append(ac.access)

    orders = session.query(tables.Orders).filter(tables.Orders.user_id.in_(foreign_ids)).filter(
        tables.Orders.hide == 0).filter(tables.Orders.status == 1).all()

    debs = session.query(tables.Debs).distinct().filter(or_(tables.Debs.user_id == user_id, tables.Debs.foreign_id == user_id)).all()
    session.close()

    splited = []
    for deb in debs:
        splited.append(deb.order_id)

    out = []
    for order in orders:
        if order.order_id not in splited:
            out.append(order)

    return out
Ejemplo n.º 2
0
def change_settings(user_id, value):
    session = Session()
    try:
        session.query(tables.Settings).filter(
            tables.Settings.user_id == user_id).update({'lite': value})
        session.commit()
    except Exception as e:
        session.rollback()
        raise e
    finally:
        session.close()
Ejemplo n.º 3
0
def update_username(user_id, user_name):
    session = Session()
    try:
        session.query(tables.User).filter(
            tables.User.user_id == user_id).update({'user_name': user_name})
        session.commit()
    except Exception as e:
        session.rollback()
        raise e
    finally:
        session.close()
Ejemplo n.º 4
0
def hide_all(user_id):
    session = Session()

    access = session.query(tables.Access).filter(tables.Access.user_id == user_id).all()

    foreign_ids = []
    for ac in access:
        foreign_ids.append(ac.access)

    session.query(tables.Orders).filter(tables.Orders.user_id.in_(foreign_ids)).update({'hide': 1})
    session.commit()
    session.close()
Ejemplo n.º 5
0
def order_price_up(order_id, price):
    session = Session()
    try:
        session.query(tables.Orders).filter(
            tables.Orders.order_id == order_id).update(
            {'price': price})
        session.commit()
    except Exception as e:
        session.rollback()
        raise e
    finally:
        session.close()
Ejemplo n.º 6
0
def order_up(order_id, text):
    session = Session()
    try:
        session.query(tables.Orders).filter(
            tables.Orders.order_id == order_id).update(
            {'text': text})
        session.commit()
    except Exception as e:
        session.rollback()
        raise e
    finally:
        session.close()
Ejemplo n.º 7
0
def update_order_status(order_id, status, date):
    session = Session()
    try:
        session.query(tables.Orders).filter(
            tables.Orders.order_id == order_id).update(
            {'status': status,
             'date': date})
        session.commit()
    except Exception as e:
        session.rollback()
        raise e
    finally:
        session.close()
Ejemplo n.º 8
0
def get_orders(user_id):
    session = Session()

    access = session.query(tables.Access).filter(tables.Access.user_id == user_id).all()

    foreign_ids = []
    for ac in access:
        foreign_ids.append(ac.access)

    orders = session.query(tables.Orders).filter(tables.Orders.user_id.in_(foreign_ids)).filter(tables.Orders.hide == 0).all()

    session.close()

    return orders
Ejemplo n.º 9
0
    def callback_handler(device_id):
        """
        Receives the json formatted callback message from a given sigfox device.
        It stores the information into the database.

        Args:
             device_id (str): the SigFox device id that has triggered the callback

        Returns:
            json: The device_id as a response with HTTP 200 OK.
        """
        json_msg = request.json

        db_session = Session()

        # Append message to 'sigfox_messages' table
        db_msg = SigFoxMessages()
        db_msg.device_id = json_msg["device_id"]
        db_msg.seq_num = json_msg["seq_num"]
        db_msg.weight = json_msg["weight"]
        db_session.add(db_msg)

        # Create 'latest_sigfox_messages' table entry or update it if existing for the current device_id
        if db_session.query(LatestSigFoxMessages).filter(
                LatestSigFoxMessages.device_id == device_id).count() == 0:
            latest_db_msg = LatestSigFoxMessages()
            latest_db_msg.device_id = json_msg["device_id"]
            latest_db_msg.seq_num = json_msg["seq_num"]
            latest_db_msg.weight = json_msg["weight"]
            db_session.add(latest_db_msg)
        else:
            entry_to_update = db_session.query(LatestSigFoxMessages).filter(
                LatestSigFoxMessages.device_id == device_id).one()
            entry_to_update.seq_num = json_msg["seq_num"]
            entry_to_update.weight = json_msg["weight"]

        db_session.commit()
        db_session.close()

        # Notify the Angular2 client that the database entry has been updated
        socket_io.emit('weight_update', {
            'device_id': json_msg["device_id"],
            'seq_num': json_msg["seq_num"],
            'weight': json_msg["weight"]
        },
                       namespace='/')
        return jsonify({"device_id": device_id})
Ejemplo n.º 10
0
def get_users():
    session = Session()

    users = session.query(tables.User).all()

    session.close()

    return users
Ejemplo n.º 11
0
def find_debs(user_id):
    session = Session()

    debs = session.query(tables.Debs).filter(
        or_(tables.Debs.user_id == user_id,
            tables.Debs.foreign_id == user_id)).all()
    session.close()
    return debs
Ejemplo n.º 12
0
def is_user_exist(user_id):
    session = Session()
    user = session.query(
        tables.User).filter(tables.User.user_id == user_id).first()
    session.close()
    if user:
        return user
    else:
        return False
Ejemplo n.º 13
0
def is_user_exist_by_username(user_name):
    session = Session()
    user = session.query(
        tables.User).filter(tables.User.user_name == user_name).first()
    session.close()
    if user:
        return user
    else:
        return False
Ejemplo n.º 14
0
def check_access(user_id, foreign_id):
    session = Session()
    access = session.query(
        tables.Access).filter(tables.Access.user_id == user_id).filter(
            tables.Access.access == foreign_id).first()
    session.close()
    if access:
        return access
    else:
        return False
    def get_latest_sigfox_messages():
        """
        Returns a 200 OK with the 'latest_sigfox_messages' table content (JSON formatted)

        Returns:
            json: A json representation of the content of table sigfox_messages with HTTP 200 OK.
        """
        db_session = Session()
        qryresult = db_session.query(LatestSigFoxMessages).all()

        resp_json = jsonify(json_list=[i.serialize for i in qryresult])
        db_session.close()

        return resp_json
Ejemplo n.º 16
0
def get_access(user_id):
    session = Session()

    access1 = session.query(
        tables.Access).filter(tables.Access.user_id == user_id).all()
    access2 = session.query(
        tables.Access).filter(tables.Access.access == user_id).all()

    ids = []
    for ac in access1:
        if user_id != ac.access:
            ids.append(ac.access)

    for ac in access2:
        if user_id != ac.user_id:
            ids.append(ac.user_id)

    users = session.query(tables.User).filter(
        tables.User.user_id.in_(ids)).all()

    session.close()

    return users
    def get_latest_weight(device_id):
        """
        Returns a HTTP 200 OK with the latest weight for the given device (JSON formatted)

        Returns:
            json: A json representation of the latest device stored weight with HTTP 200 OK.
        """
        db_session = Session()
        qryresult = db_session.query(LatestSigFoxMessages).filter(
            LatestSigFoxMessages.device_id == device_id).one()
        weight = qryresult.weight

        resp_json = jsonify({"weight": weight})
        db_session.close()

        return resp_json
    def get_sigfox_messages_for_device(device_id):
        """
        Returns a 200 OK with the 'sigfox_messages' table content for a given device (JSON formatted)

        Args:
             device_id (str): the SigFox device id that has triggered the callback

        Returns:
            json: A json representation of the content of table sigfox_messages (for one device) with HTTP 200 OK.
        """
        db_session = Session()
        qryresult = db_session.query(SigFoxMessages).filter(
            SigFoxMessages.device_id == device_id)

        resp_json = jsonify(json_list=[i.serialize for i in qryresult])
        db_session.close()

        return resp_json
Ejemplo n.º 19
0
def create_order(user_id, text):
    session = Session()

    user = session.query(tables.User).filter(
        tables.User.user_id == user_id).first()

    date = create_date()
    order = tables.Orders(user_id=user_id,
                          user_name=user.user_name,
                          text=text,
                          created=date)

    try:
        session.add(order)
        session.commit()
    except Exception as e:
        session.rollback()
        raise e
    finally:
        session.close()
Ejemplo n.º 20
0
def delete_user(user_id):
    session = Session()
    session.query(tables.User).filter(tables.User.user_id == user_id).delete()
    session.commit()
    session.close()
Ejemplo n.º 21
0
def delete_deb(deb_id):
    session = Session()
    session.query(tables.Debs).filter(tables.Debs.deb_id == deb_id).delete()
    session.commit()
    session.close()
Ejemplo n.º 22
0
def hide_order(order_id):
    session = Session()
    session.query(tables.Orders).filter(tables.Orders.order_id == order_id).update({'hide': 1})
    session.commit()
    session.close()
Ejemplo n.º 23
0
def order_status(order_id):
    session = Session()
    order = session.query(tables.Orders).filter(tables.Orders.order_id == order_id).first()
    session.close()
    return order
Ejemplo n.º 24
0
def user_settings(user_id):
    session = Session()
    settings = session.query(
        tables.Settings).filter(tables.Settings.user_id == user_id).first()
    session.close()
    return settings
Ejemplo n.º 25
0
def get_words(types):
    session = Session()
    words = session.query(Word).filter(Word.type.in_(types)).all()
    session.close()
    return words
Ejemplo n.º 26
0
def delete_user_debs(user_id, foreign_id):
    session = Session()
    session.query(tables.Debs).filter(tables.Debs.user_id == user_id).filter(
        tables.Debs.foreign_id == foreign_id).delete()
    session.commit()
    session.close()
Ejemplo n.º 27
0
def delete_access(access_id):
    session = Session()
    session.query(
        tables.Access).filter(tables.Access.access_id == access_id).delete()
    session.commit()
    session.close()