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
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
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()
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