def start_server(host, port): app.register_blueprint(db_api.blueprint) api.add_resource(user_resources.UserListResource, ConfigReader.read_users_api_url()) api.add_resource(user_resources.UserResource, ConfigReader.read_user_api_url()) api.add_resource(bin_user_resources.BinUserResource, ConfigReader.read_get_bin_user_api_url()) DataBase.global_init("bd/name.bd") # UserController.UserController().create_test_user() # app.run(port=8080, host='127.0.0.1') # func_start() app.run(port=port, host=host)
def abort_if_user_not_found(email, user_id=None): session = DataBase.create_session() if user_id: user = session.query(User).filter(User.id == user_id).first() else: user = session.query(User).filter(User.email == email).first() if not user: abort(404, message=f"User {email} not found")
def get(self): if request.json: if 'email' in request.json: abort_if_user_not_found(request.json['email']) db_session = DataBase.create_session() email = request.json['email'] user = db_session.query(User).filter( User.email == email).first() return jsonify(user.to_dict()) elif 'id' in request.json: abort_if_user_not_found(email=None, user_id=request.json['id']) db_session = DataBase.create_session() user_id = request.json['id'] user = db_session.query(User).filter( User.id == user_id).first() return jsonify(user.to_dict()) else: return make_response(jsonify({'error': "No email in the request"}), 400)
def delete(self): if request.json and 'id' in request.json: user_id = request.json['id'] abort_if_user_not_found(email=None, user_id=user_id) session = DataBase.create_session() user = session.query(User).filter(User.id == user_id).first() session.delete(user) session.commit() return jsonify({'success': 'OK'}) else: return make_response(jsonify({'error': "No id in the request"}), 400)
def post(self): args = parser.parse_args() session = DataBase.create_session() user = User() user.name = args["name"] user.surname = args["surname"] user.email = args["email"] user.hashed_password = args["hashed_password"] user.salt = args["salt"] user.confirmed = args['confirmed'] user.money = args['money'] session.add(user) session.commit() return jsonify({'success': 'OK'})
def get(self): db_session = DataBase.create_session() if request.json: if 'email' in request.json: email = request.json['email'] abort_if_user_not_found(email) user = db_session.query(User).filter(User.email == email).first() return jsonify(ConverterObj.encode(user)) # return make_response(jsonify({'error': "No user in the system"})) elif 'id' in request.json: user_id = request.json['id'] abort_if_user_not_found(email=None, user_id=user_id) user = db_session.query(User).filter(User.id == user_id).first() return jsonify(ConverterObj.encode(user)) else: return make_response(jsonify({'error': "No email in the request"}), 400)
def post(self): if request.json and all(val in request.json for val in ("id", "change_properties")): abort_if_user_not_found(email=None, user_id=request.json['id']) db_session = DataBase.create_session() try: db_session.query(User).filter( User.id == request.json['id']).update( request.json['change_properties']) except exc.InvalidRequestError as e: abort(404, message=e) db_session.commit() return jsonify({'success': 'OK'}) else: return make_response(jsonify({'error': "No email in the request"}), 400)