def put(self, id): """PUT request that deals with the edition or a creation of a portfolio with at a certain id""" # Parse the application/json data data = Portfolio.parser.parse_args() # Call the model portfolio = PortfolioModel.find_by_id(id) # if already exists we update if portfolio: portfolio.name = data['name'] else: # if doesn't we create portfolio = PortfolioModel(**data) # save and commit to database portfolio.save_to_db() # return the object as json return portfolio.json()
def post(self): """POST request that creates a new portfolio provided the correct data""" # if already exists if PortfolioModel.find_by_name(data['name']): return {'message': "An portfolio with name '{}' already exists.".format(data['name'])}, 400 # Parse the application/json data data = Portfolio.parser.parse_args() # We create the portfolio portfolio = PortfolioModel(**data) # we try to save and commit try: portfolio.save_to_db() except: # in case of error return {"message": "An error occurred inserting the portfolio."}, 500 # return a json return portfolio.json(), 201
def post(cls): data = cls.user_parser.parse_args() if UserModel.find_by_email(data['email']): return {'message': 'This email address already exists'}, 400 data['password'] = generate_password_hash(data['password']) new_user = UserModel( **data ) # data['email'], data['password'], user parser insures it only has 2 new_user.save_to_db() # create the default portfolios for the user for portfolio_meta in DEFAULT_PORTFOLIOS: portfolio = PortfolioModel(portfolio_meta["name"], new_user.id, portfolio_meta) portfolio.save_to_db() access_token = create_access_token(identity=new_user.id, fresh=True, expires_delta=timedelta(hours=3)) refresh_token = create_refresh_token(new_user.id) return { 'access_token': access_token, 'refresh_token': refresh_token, }, 200
def create_portfolio(cls, name, data): portfolio = PortfolioModel(name, get_jwt_identity(), data) portfolio.save_to_db() return portfolio