Esempio n. 1
0
    def post(self):
        data_parser = reqparse.RequestParser()
        data_parser.add_argument('compPortfolios',
                                 action='append',
                                 required=True,
                                 help="You must provide a list of portfolios")
        data_parser.add_argument("end")
        data_parser.add_argument("start")
        data = data_parser.parse_args()

        start = data['start'][:10] if data["start"] else None
        end = data['end'][:10] if data["end"] else None
        portfolios = []
        for portfolio_name in data['compPortfolios']:
            portfolio = PortfolioModel.find_by_name(portfolio_name,
                                                    get_jwt_identity())
            if not portfolio:
                return {
                    "message":
                    "Portfolio by the name {} was not found".format(
                        portfolio_name)
                }, 404
            else:
                portfolios.append(portfolio)
        try:
            return create_comparison_tearsheet(portfolios, start, end)
        except Exception as e:
            traceback.print_exc()
            return {"message": "Oops, an error occurred on our end"}, 500
Esempio n. 2
0
    def put(self, name):
        portfolio = PortfolioModel.find_by_name(name, get_jwt_identity())
        if portfolio:
            # cache.delete_memoized(create_portfolio_tearsheet)
            try:
                data = self.get_data(additional_reqs={"name": str})
                original_create_date = portfolio.date_created
                portfolio.delete_from_db()
                portfolio = self.create_portfolio(data['name'], data)
                portfolio.date_created = original_create_date

            except ValueError as e:
                return {"message": str(e)}, 500
            except Exception as e:
                traceback.print_exc()
                return {
                    "message": "An error occurred when updating the portfolio"
                }, 500
        else:
            try:
                data = self.get_data()
                portfolio = self.create_portfolio(name, data)
            except:
                traceback.print_exc()
                return {
                    "message": "An error occurred creating the portfolio"
                }, 500

        portfolio.save_to_db()
        return portfolio.json(), 200
Esempio n. 3
0
 def get(self, name):
     portfolio = PortfolioModel.find_by_name(name, get_jwt_identity())
     if portfolio:
         return portfolio.json(), 200
     else:
         return {
             'message':
             "Could not fund a portfolio with name '{}'".format(name)
         }, 404
Esempio n. 4
0
    def get(self, id):
        """GET request that deals with requests that look for a portfolio by id"""
        
        # Request to the model
        portfolio = PortfolioModel.find_by_id(id)

        # if found
        if portfolio:
            return portfolio.json()

        # if not
        return {'message': 'Portfolio not found'}, 404
Esempio n. 5
0
    def get(self, id):
        """GET request returns the list of all portfolios belonging to a user"""
        
        # We find all portfolios that belong to a user
        Portfolio = PortfolioModel.find_portfolios_by_user(id)

        # if there is any we return them as json
        if Portfolio:
            return {'Portfolios': list(map(lambda x: x.json(), Portfolio))}, 201
        else:
            # If there is none
            return {'message': 'This user does not exist or does not have any portfolio'}, 404
Esempio n. 6
0
    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
Esempio n. 7
0
 def post(self, name):
     if PortfolioModel.find_by_name(name, get_jwt_identity()):
         return {
             'message':
             "A portfolio with name '{}' already exists.".format(name)
         }, 400
     try:
         data = self.get_data()
         portfolio = self.create_portfolio(name, data)
     except ValueError as e:
         return {"message": str(e)}, 500
     except:
         traceback.print_exc()
         return {
             "message": "An error occurred when inserting a portfolio"
         }, 500
     return portfolio.json(), 201
Esempio n. 8
0
    def delete(self, id):
        """DELETE request that deals with the deletion of book given its id"""

        # Look for the portfolio with a specific id
        Portfolio = PortfolioModel.find_by_id(id)

        # if exist
        if Portfolio:
            try:
                # we delete it
                Portfolio.delete_from_db()
                return {'message': 'Portfolio deleted'}
            except:
                # if error during deletion
                return {'message': 'A portfolio is linked to a many portfolio_book relations, so before deleting it you need to delete all these relations.'}
        else:
            # if doesn't exist
            return {'message' : 'Portfolio not found'}, 404
Esempio n. 9
0
 def post(self, portfolio_name):
     data_parser = reqparse.RequestParser()
     data_parser.add_argument("end")
     data_parser.add_argument("start")
     data = data_parser.parse_args()
     start = data['start'][:10] if data["start"] else None
     end = data['end'][:10] if data["end"] else None
     portfolio = PortfolioModel.find_by_name(portfolio_name,
                                             get_jwt_identity())
     if portfolio:
         try:
             return create_portfolio_tearsheet(portfolio, start, end), 200
         except Exception as e:
             traceback.print_exc()
             # return {"message": e}, 500
             return {"message": "Oops, an error occurred on our end"}, 500
     else:
         return {
             "message":
             "Portfolio by the name {} was not found".format(portfolio_name)
         }, 404
Esempio n. 10
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()
Esempio n. 11
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
Esempio n. 12
0
 def create_portfolio(cls, name, data):
     portfolio = PortfolioModel(name, get_jwt_identity(), data)
     portfolio.save_to_db()
     return portfolio
Esempio n. 13
0
 def delete(self, name):
     portfolio = PortfolioModel.find_by_name(name, get_jwt_identity())
     if portfolio:
         portfolio.delete_from_db()
     return {'message': 'Successfully deleted {}'.format(name)}, 200