Beispiel #1
0
def get_user(id):
    user_info = User.query.get(id)
    temp_book = []
    temp_car = []
    if user_info:
        user_car = UserCar.query.filter_by(user_id=user_info.id).all()
        user_book = UserBook.query.filter_by(user_id=user_info.id).all()
        if user_car:
            for car in user_car:
                response = requests.get(
                    f"http://127.0.0.1:5002/car/{car.car_id}")
                if response.status_code == 200:
                    temp_car.append(response.json())
        if user_book:
            for book in user_book:
                response = requests.get(
                    f"http://127.0.0.1:5003/books/{book.book_id}")
                if response.status_code == 200:
                    temp_book.append(response.json())
        data = UserSchema().dump(user_info)
        data["car_info"] = temp_car
        data["book_info"] = temp_book
        data.pop("password")
        return jsonify(data), HTTPStatus.OK
        # return UserSchema(exclude=["password"]).jsonify(user_info),HTTPStatus.OK
    return jsonify(msg="user not found"), HTTPStatus.NOT_FOUND
Beispiel #2
0
def get_all_user():
    user_info = User.query.all()
    temp = []
    for user in user_info:
        user_car = UserCar.query.filter_by(user_id=user.id).all()
        user_book = UserBook.query.filter_by(user_id=user.id).all()
        data_schema = UserSchema().dump(user)
        data_schema.pop("password")
        data_schema["car_info"] = []
        data_schema["book_info"] = []
        if user_car:
            for car in user_car:
                response = requests.get(
                    f"http://127.0.0.1:5002/car/{car.car_id}")
                if response.status_code == 200:
                    data_schema["car_info"].append(response.json())
        if user_book:
            for book in user_book:
                response = requests.get(
                    f"http://127.0.0.1:5003/books/{book.book_id}")
                if response.status_code == 200:
                    data_schema["book_info"].append(response.json())
        temp.append(data_schema)
    return jsonify(temp), HTTPStatus.OK