Beispiel #1
0
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument("price",
                            type=float,
                            required=True,
                            help="This field is required")
        parser.add_argument("name",
                            type=str,
                            required=True,
                            help="This field is required")
        parser.add_argument("date",
                            type=str,
                            required=True,
                            help="This field is required")
        parser.add_argument("category",
                            type=str,
                            help="This field is optional")
        data = parser.parse_args()
        expense = ExpenseModel.user_find_by_name_and_date(
            current_identity, data['name'],
            datetime.strptime(data['date'], "%Y-%m-%d").date())
        if not expense:
            return {"message": "The expense was not found"}, 400
        expense.price = data['price']
        expense.category = data['category']
        try:
            expense.save_to_db()
        except:
            return {'message': "An error occured while updating expense"}, 500

        return {
            'message': "Expense successfully updated",
            'expense': expense.json()
        }
Beispiel #2
0
    def put(self, name):
        data = Expense.parser.parse_args()

        expense = ExpenseModel.find_by_name(name)

        if expense:
            expense.price = data['price']
        else:
            expense = ExpenseModel(name, **data)

        expense.save_to_db()

        return expense.json()
Beispiel #3
0
    def post(self, name):
        if ExpenseModel.find_by_name(name):
            return {'message': "An expense with name '{}' already exists.".format(name)}, 400

        data = Expense.parser.parse_args()

        expense = ExpenseModel(name, **data)

        try:
            expense.save_to_db()
        except:
            return {"message": "An error occurred inserting the expense."}, 500

        return expense.json(), 201
Beispiel #4
0
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("date",
                            type=str,
                            required=True,
                            help="This field is required")
        parser.add_argument("name",
                            type=str,
                            required=True,
                            help="This field is required")

        data = parser.parse_args()

        expense = ExpenseModel.user_find_by_name_and_date(
            current_identity, data['name'],
            datetime.strptime(data['date'], "%Y-%m-%d").date())
        if expense:
            expense.delete_from_db()
            return {"message": "expense successfully deleted"}
        return {"message": "expense not found in the database"}, 400
Beispiel #5
0
    def post(self):
        data = self.parser.parse_args()
        expense = ExpenseModel.user_find_by_name_and_date(
            current_identity, data['name'], date.today())
        if expense:
            return {
                "message":
                "Expense already exists for this date, modify the already existing expense"
            }, 400
        expense = ExpenseModel(date=date.today(),
                               user_id=current_identity.id,
                               **data)
        print(expense.json())
        print(current_identity.id)
        try:
            expense.save_to_db()
        except:
            return {
                'message': "An error occured while creating the expense"
            }, 500

        return {"message": "Expense successfully created"}, 201
Beispiel #6
0
 def delete(self, name):
     expense = ExpenseModel.find_by_name(name)
     if expense:
         expense.delete_from_db()
         return {'message': 'Item deleted.'}
     return {'message': 'Item not found.'}, 404
Beispiel #7
0
 def get(self, name):
     expense = ExpenseModel.find_by_name(name)
     if expense:
         return expense.json()
     return {'message': 'Item not found'}, 404