Beispiel #1
0
    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()
Beispiel #2
0
    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