Пример #1
0
def login():
    """
    Login user function
    """
    data = request.json["properties"]

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

    # search user by email
    user = db.select_rows(
        f"select * from account where email='{data['email']}'")[0]

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

    if user[-1] != hashlib.md5(data["password"].encode()).hexdigest():
        return jsonify({"error": "Incorrect password"}), 400

    response = {
        "result": "ok",
        "user": user_object(user),
    }
    return jsonify(response), 200
Пример #2
0
def join_room():
    """
    Join room function
    """

    data = request.json
    print(data)
    code = data["code"]
    user = data["user_id"]

    db_data = db.select_rows(f"select * from room where id_room = {code};")

    # **приклад запиту**
    # temp = db.select_rows(
    #     """
    #     select status.status_name from
    #     account inner join room_has_user on account.id_user = room_has_user.user_id
    #     inner join room on room_has_user.room_id = room.id_room
    #     inner join status on room_has_user.user_status_id = status.id_status
    #     where account.id_user = {тут маэ бути ід юзера};
    #     """
    # )

    if db_data is None:
        return jsonify({"error": "There is no room with this code"}), 404

    response = {"message": "ok", "room_data": db_data[0]}
    return jsonify(response), 200
Пример #3
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
Пример #4
0
def get_active_users_in_room(admin_id, code):
    if check_admin(admin_id=admin_id, code=code):
        # user_status_id: 1 - admin, 2 - user
        users = db.select_rows(f"""select * from room_has_user 
                    where (room_id = {code}) and 
                            (user_status_id = 2)""")
        return users
    return 0
Пример #5
0
def get_room(code):
    """
    Get room by code

    :param code: room code
    :return: room
    """
    room = db.select_rows(f"select * from room where id_room = {code}")
    return room
Пример #6
0
def get_user_rooms(id_user):
    """
    Get rooms by user id

    :param id_user:
    :return: rooms
    """
    rooms = []
    # user_status_id: 1 - admin, 2 - user
    user_room = db.select_rows(
        f"select * from room_has_user where user_id = {id_user} and user_status_id = 1"
    )

    if user_room is not None:
        message = 'Here is your rooms:'
        for room in user_room:
            rooms.extend(db.select_rows(
                f"select * from room where id_room = {room[1]}"
            ))
    else:
        message = 'You have no rooms! Do you want to create one?'
    print(rooms)
    return rooms, message
Пример #7
0
def build_profile():
    data = request.json

    try:
        user = db.select_rows(
            f"select * from account where id_user='******'id_user']}'")[0]
        print(user)
    except:
        from sys import exc_info
        print(exc_info()[0])
        return jsonify({"error": "Catch DB exception"}), 400

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

    response = {
        "result": "ok",
        "id_user": user[0],
        "user": user_object(user),
    }
    return jsonify(response), 200
Пример #8
0
def get_messages_story(room_id):
    """
    Get the last 50(u can change this value if u want) messages by room id.
    Messages sorted by send date
    """
    messages = db.select_rows(f"""
        select content, date_send, user_id from
        message inner join room on
        room.id_room = message.room_id
        where room.id_room = {int(room_id)}
        order by date_send desc limit 50;
    """)

    response = []
    if messages is not None:
        for item in messages:
            response.append({
                "user": get_user_by_id(item[2]).title(),
                "data": item[0],
                "send_on": item[1].strftime("%H:%M")
            })
    return response[::-1]
Пример #9
0
def is_join(room_id, user_id):
    temp = db.select_rows(f"""
        select * from  room_has_user 
        where room_id={int(room_id)} and user_id={int(user_id)} 
        """)
    return temp
Пример #10
0
def get_user_by_id(id):
    return " ".join(
        db.select_rows(
            f"select first_name, last_name from account where id_user={int(id)}"
        )[0])