def post(self): data = Foods.parser.parse_args() food = FoodModel(**data) try: food.saveToDb() except: return { "message": "An error occurred while creating the classroom." }, 500 return food.to_dict(), 201
def init_db(): print("Checking if Nutrients are initialized") nutrients = NutrientModel.query.all() if not nutrients: print("Initializing Nutrients") NutrientModel.initialize_nutrients() print("Checking if Foods are initialized") foods = FoodModel.query.all() if not foods: print("Initializing Foods") FoodModel.initialize_foods()
def put(self, _id, store): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required = False, help="Field name cannot be empty") parser.add_argument('quantity', type=int, required = False, help="Field name cannot be empty") parser.add_argument('calories', type=int, required = False, help="Field name cannot be empty") parser.add_argument('expiration', type=str, required = False, help="Field name cannot be empty") food = FoodModel.find_by_store_id(_id, store) if (food): payload = parser.parse_args() if payload.get('name') is not None: food.name = payload['name'] if payload.get('quantity') is not None: food.quantity = payload['quantity'] if payload.get('calories') is not None: food.calories = payload['calories'] if payload.get('expiration') is not None: food.expiration = datetime.datetime.strptime(payload['expiration'], '%m/%d/%Y') food.save() return { 'status': 'ok', 'message' : f'Food {_id} changed for store {store}' } else: return { 'status' : 'error', 'message' : f'Food {_id} not found in store {store}' }, 404
def put(self, _id): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin privilege required.'}, 401 parser = reqparse.RequestParser() parser.add_argument('food_id', type=list, required=True, location='json', help="every order need a list of food id!") data = Order.parser.parse_args() order = OrderModel.find_by_id(_id) if order: foods = [ FoodModel.find_by_id(food).json() for food in data['food_id'] ] total_price = sum([food['price'] for food in foods]) order.total_price = total_price order.updated_at = datetime.now() else: return {'message': 'Order not found'}, 404 try: order.save_to_db() except: return {"message": "An error occurred inserting the food."}, 500 return order.json(), 201
def get(self): foods = [food.json() for food in FoodModel.find_all()] results = {} for food in foods: if food['category'] not in results: results[food['category']] = [] results[food['category']].append(food) return {'categories': results}
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 Food(self, id): res = [] food = FoodModel.findById(id) if food is None: return {'message': 'Food not found.'}, 404 temp_food = food.to_dict() if temp_food: return temp_food res.append(temp_food) return {"result": res}
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 get(self, date): try: datetime.strptime(date, '%d-%m-%Y') except ValueError: return { 'message': "Your date format is incorect. Example: 25/03/2018" }, 400 result = {'food': FoodModel.get_food_by_date(date)} return result, 200
def get(self, _id, store): food = FoodModel.find_by_store_id(_id, store) if food: return food.json() else: return { 'status' : 'error', 'message' : f'Food {_id} not found in store {store}' }, 404
def put(self, id): data = Foods.parser.parse_args() food = FoodModel.findById(id) if food is None: return {'message': 'Food not found.'} else: food.name = data['name'] food.money = data['money'] food.saveToDb() return food.to_dict()
def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required = True, help="Field name cannot be empty") parser.add_argument('quantity', type=int, required = True, help="Field name cannot be empty") parser.add_argument('calories', type=int, required = True, help="Field name cannot be empty") parser.add_argument('expiration', type=str, required = True, help="Field name cannot be empty") parser.add_argument('store', type=int, required = True, help="Field name cannot be empty") payload = parser.parse_args() expiration = datetime.datetime.strptime(payload['expiration'], '%m/%d/%Y') food = FoodModel(payload['name'], payload['quantity'], payload['calories'], expiration, payload['store']) food.save() return { 'status' : 'ok', 'message' : 'Store created without issues', 'record' : food.json() }, 201
def delete(self, _id, store): food = FoodModel.find_by_store_id(_id, store) if food: food.delete() return { 'status': 'ok', 'message' : f'Food {_id} deleted for store {store}' } else: return { 'status' : 'error', 'message' : f'Food {_id} not found in store {store}' }, 404
def post(self): parser = reqparse.RequestParser() parser.add_argument('file',type=werkzeug.datastructures.FileStorage, location='files') payload = parser.parse_args() food_id = request.form.to_dict().get("food", "") if (payload['file'] == ""): return { 'status' : 'error', 'message' : 'File parameter cannot be empty' } elif (food_id == ""): return { 'status' : 'error', 'message' : 'Food_Id parameter cannot be empty' } food = FoodModel.find_by_id(food_id) if (food): photo = payload['file'] if (photo): file_extension = os.path.splitext(photo.filename)[1] filename = secure_filename(f'{uuid.uuid4()}{file_extension}') file_location = os.path.join(UPLOAD_FOLDER,filename) photo.save(file_location) food.photo = file_location food.save() return { 'status' : 'ok', 'message' : f'File {filename} uploaded' }, 201 else: return { 'status' : 'error', 'message' : f'Error processing the upload file' }, 403 else: return { 'status' : 'error', 'message' : f'Food {food_id} not found' }, 404
def get(self, food_id): food = FoodModel.find_by_id(food_id) if food: if food.photo: return send_from_directory(UPLOAD_FOLDER, os.path.basename(food.photo), as_attachment=True) else: return { 'status' : 'error', 'message' : f'Food {food_id} does not have a photo' }, 404 else: return { 'status' : 'error', 'message' : f'Food {food_id} not found' }, 404
def post(self): user_id = get_jwt_identity() data = Order.parser.parse_args() foods = [FoodModel.find_by_id(food).json() for food in data['food_id']] total_price = sum([food['price'] for food in foods]) status = 'submitted.' created_at = datetime.now() order = OrderModel(user_id, total_price, status, created_at, created_at) try: order.save_to_db() except: return {"message": "An error occurred inserting the food."}, 500 return order.json(), 201
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, food_name): food = FoodModel.find_by_food_name(food_name) if food: return { "message": "food food_name with '{}' already exists".format(food_name) }, 400, { 'Access-Control-Allow-Origin': '*' } data = Food.parser.parse_args() food = FoodModel(data['food_name'], data['food_calorie'], data['food_type'], data['food_cuisine'], data['food_image'], data['food_category'], data['food_description'], data['spice1'], data['spice2'], data['spice3'], data['spice4']) try: food.save_to_db() except: return {"message": "Error occured durig insertion"}, 500 return food.json(), 201
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
def put(self, food_name): data = Food.parser.parse_args() food = FoodModel.find_by_food_name(food_name) if food is None: food = FoodModel(data['food_name'], data['food_calorie'], data['food_type'], data['food_cuisine'], data['food_image'], data['food_category'], data['food_description'], data['spice1'], data['spice2'], data['spice3'], data['spice4']) else: food.food_name = data['food_name'] food.food_calorie = data['food_calorie'] food.food_type = data['food_type'] food.food_cuisine = data['food_cuisine'] food.food_image = data['food_image'] food.food_category = data['food_category'] food.food_description = data['food_description'] food.spice1 = data['spice1'] food.spice2 = data['spice2'] food.spice3 = data['spice3'] food.spice4 = data['spice4'] food.save_to_db() return food.json(), {'Access-Control-Allow-Origin': '*'}
def get(self, food_name): food = FoodModel.find_by_food_name(food_name) if food: # return food.json() return food.json() return {"message": "No food item found"}, 404
def delete(self, food_name): food = FoodModel.find_by_food_name(food_name) if food: food.delete_from_db() return {"message": "Successfully deleted {}".format(food_name)}
def get(): foods = FoodModel.get_all() if not foods: return {'message': strings.error_foods_not_found}, 404 return {'items': [food.json() for food in FoodModel.query.all()]}, 200
def get(self): foods = [food.json() for food in FoodModel.find_all()] return {'food': foods}, 200
def delete(self, id): food = FoodModel.findById(id) if food: food.deleteFromDb() return {'message': 'Food deleted.'} return {'message': 'Food not found.'}, 404
def get(food_id): food = FoodModel.find_by_id(food_id) if not food: return {'message': strings.error_food_not_found}, 404 return food.json(), 200