def delete(self, id): recipe = RecipeModel.find_by_id(id=id) if recipe is None: return { "message": "recipe was not found", "code": 404, "success": False, }, 404 # delete from db try: recipe.delete_from_db() except: return { "message": "Error with the delete process", "code": 500, "success": False, }, 500 return { "message": "Recipe was deleted", "success": True, "code": 200, "result": recipe_schemas.dump(recipe) }
def find_possible_drinks(ingredient_id): possible_drinks_dict = [] possible_drinks = MixingModel.find_by_ingredient(ingredient_id) for row in iter(possible_drinks): drink = RecipeModel.find_by_id(row.recipe_id) possible_drinks_dict.append(drink.json()) return possible_drinks_dict
def get(self, rfid): user = UserModel.find_by_rfid(rfid) if user: current_order = int(user.current_order) recipe = RecipeModel.find_by_id(current_order) ingredients = RecipeModel.find_ingredients(current_order) durations = [0, 0, 0, 0] for ingredient in ingredients: durations[ingredient[0]['pump'] - 1] = ingredient[0]['portion'] timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') order = OrderModel(user.id, user.current_order, timestamp) order.save_to_db() return {"durations": durations} awaiting = UserCardRegistrationModel.awaiting_for_card if awaiting: if UserModel.find_by_username(awaiting): UserModel.update_card(awaiting, rfid) return {"durations": [0, 0, 0, 0]} else: return {"message": "No such user"} else: return {"durations": [0, 0, 0, 0]}
def post(self, id): # make sure the id belongs to a recipe recipe = RecipeModel.find_by_id(id) if recipe is None: return { "message": "Recipe was not found", "success": False, "code": 404 }, 404 # fetch file and save it files = request.files['recipe_cover'] files.filename = "recipe_" + str(id) + "_" + files.filename try: filename = images.save(files) except: return { "message": "Make sure you are uploading an image", "success": False, "code": 500 }, 500 # create new image record image = image_schemas.load({ "name": filename, "data": files.mimetype, "recipe_id": id }) # update the recipe with the filename recipe.image_name = files.filename try: image.save_to_db() recipe.update_to_db() except: return { "message": "There was an error while saving, please try again", "success": False, "code": True }, 500 return { "message": "Image uploaded", "success": True, "code": 201, "result": image_schemas.dump(image) }
def delete(self, recipe_id): data = NewRecipe.parser.parse_args() recipe = RecipeModel.find_by_id(recipe_id) if recipe: if recipe.chef_id == data['chef_id']: recipe.delete_from_db() return {'message': 'Recipe deleted'} else: return { 'message': 'You can not delete this recipe as you are not the chef.' }, 401 return {'message': 'Recipe not found'}, 404
def patch(self, id): recipe = RecipeModel.find_by_id(id) # check if the recipe exists if recipe is None: return { "message": "recipe was not found", "code": 404, "success": False }, 404 # fetch the arguments data = request.get_json() recipe.title = data["title"] recipe.description = data["description"] recipe.ingredients = json.dumps(data["ingredients"]) recipe.sections = json.dumps(data["sections"]) recipe.category = data["category"] recipe.time = data["time"] recipe.portions = data["portions"] # save the update data try: recipe.update_to_db() except: return { "message": "Error during update", "code": 500, "success": False }, 500 recipe = recipe_schemas.dump(recipe) recipe["ingredients"] = [{ 'name': r['name'], 'quantity': r['quantity'], 'measurement': r['measurement'] } for r in json.loads(recipe["ingredients"])] return { "message": "recipe successfully updated", "code": 200, "success": True, "result": recipe }, 200
def post(self, recipe_id, ingredient_id): data = RecipeIngredient.parser.parse_args() if not IngredientModel.find_by_id(ingredient_id): return {'message': "That ingredient does not exist"}, 404 if not RecipeModel.find_by_id(recipe_id): return {'message': "That recipe does not exist"}, 404 if RecipeIngredientModel.find_by_recipe_ingredient( recipe_id, ingredient_id): return {'message': "That recipe already contains that ingredient."} ri = RecipeIngredientModel(data['amount'], recipe_id, ingredient_id) print(ri.json()) try: ri.save_to_db() except: return { "message": "An error occurred inserting the recipe ingredient." }, 500 return ri.json(), 201
def put(self, recipe_id): data = NewRecipe.parser.parse_args() recipe = RecipeModel.find_by_id(recipe_id) if recipe: if recipe.chef_id == data['chef_id']: recipe.name = data['name'] recipe.recipe_type = data['recipe_type'] recipe.description = data['description'] recipe.steps = data['steps'] recipe.servings = data['servings'] recipe.prep_min = data['prep_min'] recipe.cook_min = data['cook_min'] else: return { 'message': 'You can not change this recipe as you are not the chef.' }, 401 else: recipe = RecipeModel(**data) recipe.save_to_db() return recipe.json(), 201
def get(self, recipe_id): recipe = RecipeModel.find_by_id(recipe_id) if recipe: return recipe.json() return {'message': 'Recipe not found'}, 404