Пример #1
0
    def get(self, name):
        claims = get_jwt_claims()
        if not claims['is_admin']:
            return {'message': 'Admin privilege required.'}, 401

        food = FoodModel.find_by_name(name)
        if food:
            return food.json()
        return {'message': 'Food not found'}, 404
Пример #2
0
    def delete(self, name):
        claims = get_jwt_claims()
        if not claims['is_admin']:
            return {'message': 'Admin privilege required.'}, 401

        food = FoodModel.find_by_name(name)
        if food:
            food.delete_from_db()
            return {'message': 'Food deleted.'}
        return {'message': 'Food not found.'}, 404
Пример #3
0
    def put(self, name):
        claims = get_jwt_claims()
        if not claims['is_admin']:
            return {'message': 'Admin privilege required.'}, 401

        data = Food.parser.parse_args()

        food = FoodModel.find_by_name(name)

        if food:
            food.price = data['price']
        else:
            food = FoodModel(name, **data)

        food.save_to_db()

        return food.json()
Пример #4
0
    def post(self, name):
        claims = get_jwt_claims()
        if not claims['is_admin']:
            return {'message': 'Admin privilege required.'}, 401

        if FoodModel.find_by_name(name):
            return {
                'message': "A food with name '{}' already exists.".format(name)
            }, 400

        data = Food.parser.parse_args()
        print(data)
        food = FoodModel(name, **data)

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

        return food.json(), 201