Ejemplo n.º 1
0
def create():
    """
    Create user function
    """

    data = request.json["properties"]

    # validation of the received data
    if not validate_json(create_schema, data):
        return jsonify({"error": "Data is invalid"}), 400

    # search users with the same email address
    temp = db.select_rows(
        f"select * from account where email='{data['email']}'")

    if temp is not None:
        return jsonify({"error":
                        "User with this email addres already exists"}), 400

    db.insert_data(f"""
        insert into account (first_name, last_name, email, password) values (
            '{data["first_name"]}', 
            '{data["last_name"]}', 
            '{data["email"]}',  
            '{hashlib.md5(data["password"].encode()).hexdigest()}'
        )""")
    db.commit()

    response = {"result": "ok"}
    return jsonify(response), 400
Ejemplo n.º 2
0
def user_in_room():
    data = request.json
    user_id = data['id_user']
    is_in_room = data['is_in_room']
    code = data['code']
    print(data)

    if is_in_room is 1:
        db.insert_data(
            # user_status_id: 1 - admin, 2 - user
            f"""
                        insert into room_has_user (user_id, room_id, user_status_id)
                        values ({int(user_id)}, {int(code)}, 2)
                        """)
        db.commit()
    else:
        db.delete_rows(f"""
                        delete from room_has_user 
                        where (user_id = {int(user_id)}) and 
                                (room_id = {int(code)}) and 
                                (user_status_id = 2)
            """)
        db.commit()

    response = {"message": "ok"}
    return jsonify(response)
Ejemplo n.º 3
0
def create_room():
    """
    Create room function

    :return:
    """

    data = request.json

    # validation of the received data
    if not validate_json(data, room_create_schema):
        return jsonify({"error": "Data is invalid"}), 400

    code = rand_code()

    try:
        # add room to db
        db.insert_data(f"""
                    insert into room (id_room, name_room, note) values (
                       '{code}', 
                       '{data['name']}', 
                       '{data['description']}'
                    )""")
        db.commit()

        # add room owner
        # status 1-admin, 2-user
        db.insert_data(f"""
                    insert into room_has_user (user_id, room_id, user_status_id) values ( 
                           '{data['id_user']}', 
                           '{code}',
                           1 
                       )""")
        db.commit()
    except:
        from sys import exc_info
        print(exc_info()[0])
        return jsonify({"message": "Catch DB exception"}), 400

    response = {"result": "ok"}

    return jsonify(response), 200
Ejemplo n.º 4
0
def send_room_message(message):

    if message["data"]:
        send_on = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        db.insert_data(f"""
                insert into message(content, date_send, room_id, user_id) 
                values (
                    '{message["data"]}',
                    '{send_on}',
                    {int(message["room"])},
                    {int(message["user"])}
                )           
                
            """)
        db.commit()
        response = [{
            "user": get_user_by_id(message["user"]).title(),
            "data": message['data'],
            "send_on": send_on.split(' ')[1][:5]  # негарно виглядає але працює
        }]

        emit('response', response, room=message["room"])