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
def create_user(): data = request.get_json() try: user = UserSchema().load(data) user.save_db() except ValidationError as err: return jsonify(err.messages), 400 except Exception as err: return jsonify({"result": str(err)}), 400 return UserSchema(exclude=("password", )).jsonify(user)
def create_user(): user_info = request.get_json() schema = UserSchema() try: user = schema.load(user_info) user.save_db() except ValidationError as err: return jsonify(err.messages) return UserSchema().jsonify(user)
def login_user(): user_data = request.get_json() user = Users.query.filter_by(email=user_data.get("email")).first() if not user: return jsonify({'message': "User not found"}), 404 if verify_secret(user_data.get("password"), user.password): schema = UserSchema() user = schema.dump(user) access_token = create_access_token(identity=user.get("id")) refresh_token = create_refresh_token(identity=user.get("id")) user.update(access_token=access_token, refresh_token=refresh_token) return jsonify(user) return jsonify({'message': "User not found"}), 404
def get_users(): identity = get_jwt_identity() user_info = User.query.filter_by(email=identity).first() if user_info: return UserSchema( exclude=["password"]).jsonify(user_info), HTTPStatus.OK return jsonify(msg="Not found")
def update_user_id(id): result = Users.query.filter_by(id=id).first() if result: data = request.json data = UserUpdateSchema().load(data) result = result.update_db(**data) return UserSchema(exclude=("password", )).jsonify(result) return jsonify({"result": f"Id {id} wasn't found"}), 404
def update_user_info(id): user = User.query.get(id) if user: new_user = request.get_json() new_user = UpdateUserSchema().load(new_user) user.update_db(**new_user) return UserSchema().jsonify(user), HTTPStatus.OK return jsonify(msg="User not found"), HTTPStatus.NOT_FOUND
def get_all(): all_user = User.query.all() temp=[] for user in all_user: users_book = UserBooks.query.filter_by(user_id=user.id).all() schema = UserSchema() data_schema = schema.dump(user) data_schema["book_info"]=[] if users_book: for book in users_book: response = requests.get(f"http://127.0.0.1:5002/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
def create_user(): user_info = request.get_json() user = User.query.filter_by(email=user_info.get("email")).first() if user: return jsonify(msg="exists"), HTTPStatus.BAD_REQUEST try: user = UserSchema().load(user_info) response = requests.get( f"http://127.0.0.1:5002/car/{user_info.get('car_id')}") response_book = requests.get( f"http://127.0.0.1:5003/books/{user_info.get('book_id')}") if user: user.generate_password() user.save_db() if response.status_code == 200: user_car = UserCar() user_car.user_id = user.id user_car.car_id = response.json().get('id') user_car.save_db() user_book = UserBook() user_book.user_id = user.id user_book.book_id = response_book.json().get('id') user_book.save_db() else: return jsonify(msg="Not found"), HTTPStatus.NOT_FOUND except ValidationError as err: return jsonify(err.messages), HTTPStatus.BAD_REQUEST return UserSchema(exclude=["password"]).jsonify(user), HTTPStatus.OK
def update_users(): identity = get_jwt_identity() user = User.query.filter_by(email=identity).first() if user: new_user = request.get_json() user_data = UpdateUserSchema().load(new_user) user.update_db(**user_data) return UserSchema().jsonify(new_user), HTTPStatus.OK return jsonify(msg="Not found"), HTTPStatus.NOT_FOUND
def update_user(): identity = get_jwt_identity() user_info = User.query.filter_by(email=identity).first() if user_info: new_user = request.get_json() new_user = UpdateUserSchema().load(new_user) user_info.update_db(**new_user) return UserSchema().jsonify(user_info), HTTPStatus.OK return jsonify(msg="Not found"), HTTPStatus.OK
def get_id(id): user = User.query.filter_by(id=id).first() if user: user_books = UserBooks.query.filter_by(user_id = user.id).all() if user_books: temp = [] for i in user_books: response = requests.get(f"http://127.0.0.1:5002/books/{i.book_id}") if response.status_code == 200: temp.append(response.json()) userschema = UserSchema() data = userschema.dump(user) data['books_info']=temp return jsonify(data),HTTPStatus.OK return UserSchema(exclude=["password"]).jsonify(user),HTTPStatus.OK return jsonify(msg="User not found"),HTTPStatus.NOT_FOUND
def user_login(): user_info = request.get_json() user: User = User.query.filter_by(email=user_info.get("email")).first() if user: if user.chech_password(user_info.get("password")): access_token = create_access_token(identity=user.email) user_schema = UserSchema().dump(user) user_schema["access_token"] = access_token return jsonify(user_schema), HTTPStatus.OK return jsonify(msg="Incorrect email or password"), HTTPStatus.NOT_FOUND
def get_login(): user = request.get_json() user_data: User = User.query.filter_by(email=user.get("email")).first() if user_data: if user_data.check_password(user.get("password")): access_token = create_access_token(identity=user_data.email) refresh_token = create_refresh_token(identity=user_data.email) user_schema = UserSchema().dump(user_data) user_schema["access_token"] = access_token user_schema["refresh_token"] = refresh_token return jsonify(user_schema), HTTPStatus.OK return jsonify(msg="Incorrect email or password"), HTTPStatus.BAD_REQUEST
def register(): user_info = request.get_json() user = User.query.filter_by(email = user_info.get("email")).first() if user: return jsonify(msg="User exsist"),HTTPStatus.BAD_REQUEST try: user = UserSchema().load(user_info) response = requests.get(f"http://127.0.0.1:5002/books/{user_info.get('book_id')}") if user: user.set_password() user = user.save_db() if response.status_code == 200: user_books = UserBooks() user_books.user_id = user.id user_books.book_id = response.json().get('id') user_books.save_db() else: return jsonify(msg="Not found!"),HTTPStatus.NOT_FOUND except ValidationError as err: return jsonify(err.messages),HTTPStatus.BAD_REQUEST # user.set_password() # user.save_db() # user = User(**user) # db.session.add(user) # db.session.commit() return UserSchema(exclude=["password"]).jsonify(user),HTTPStatus.OK
def create_user(): user_data = request.get_json() user = User.query.filter_by(email=user_data.get("email")).first() if user: return jsonify(msg="User exsist"), HTTPStatus.BAD_REQUEST try: user = UserSchema().load(user_data) user.set_password() user.save_db() except ValidationError as err: return jsonify(err.messages), HTTPStatus.BAD_REQUEST return UserSchema().jsonify(user), HTTPStatus.OK
def update_user(id): user_info = request.json user = Users.query.get(id) if user: try: schema = UpdateSchema() user_info = schema.load(user_info) user = user.update(**user_info) return UserSchema(load_only=['password']).jsonify(user) except ValidationError as err: return jsonify(err.messages) return jsonify({'message': "User not found"}), 404
def update(id): user = User.query.filter_by(id=id).first() if user: user_info = request.get_json() users = UpdateSchema().load(user_info) user.update_db(**users) # if user_info.get("name"): # user.name = user_info.get("name") # if user_info.get("surname"): # user.surname = user_info.get("surname") # db.session.add(user) # db.session.commit() return UserSchema().jsonify(user_info),HTTPStatus.OK return jsonify(msg="User not found"),HTTPStatus.NOT_FOUND
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
def get_user_id(id): data = Users.query.filter_by(id=id).first() if data: return UserSchema(exclude=("password", )).jsonify(data) return jsonify({"result": f"Id {id} wasn't found"}), 404
def get_users_results(): results = Users.query.all() return UserSchema(exclude=("password", )).jsonify(results, many=True)
def get_user_info(): id = get_jwt_identity() user = Users.query.get(id) if user: return UserSchema(exclude=("password", )).jsonify(user) return jsonify({"message": f"User wasn't found"}), 404
def get_users(): users = Users.query.all() return UserSchema(load_only=['password']).jsonify(users, many=True)
def get_user(): identity = get_jwt_identity() user_info = User.query.filter_by(email=identity).first() return UserSchema().jsonify(user_info), HTTPStatus.OK
def get_user(id): user = Users.query.filter_by(id=id).first() if user: return UserSchema().jsonify(user) return jsonify({'message': "User not found"}), 404