def show_cuisine_single_recipe(cuisine_name, recipe_name): cuisine = Cuisine.get_or_none(name=cuisine_name) if cuisine: is_recipe = Recipe.get_or_none(name=recipe_name) if is_recipe: recipe = Recipe.select().where(Recipe.cuisine_id == cuisine.id, Recipe.name == recipe_name) if recipe: for r in recipe: result = { "name": r.name, "image": r.image, "difficulty": r.difficulty } return jsonify({"data": result}) else: return jsonify({ "erros": recipe_name + " is not found in " + cuisine_name + " cuisine." }) else: return jsonify({"errors": "Recipe not exists"}) else: return jsonify({"errors": cuisine_name + " cuisine is not exist"})
def show_recipe_step(cuisine_name, recipe_name, step_number): cuisine = Cuisine.get_or_none(name=cuisine_name) if cuisine: recipe = Recipe.get_or_none(name=recipe_name) if recipe: if recipe.cuisine_id == cuisine.id: step = Step.select().where(Step.recipe_id == recipe.id, Step.number == step_number) for s in step: result = { "step_number": s.number, "description": s.description } return jsonify({"data": result}) else: return jsonify({ "errors": recipe_name + " is not found in " + cuisine_name + " cuisine." }) else: return jsonify({"errors": "Recipe not exists"}) else: return jsonify({"errors": cuisine_name + " cusine is not exist"})
def delete_recipe_ingredient(cuisine_name, recipe_name): current_user = User.get_by_id(get_jwt_identity()) if current_user.is_admin: cuisine = Cuisine.get_or_none(name=cuisine_name) if cuisine: recipe = Recipe.get_or_none(name=recipe_name) if recipe: if recipe.cuisine_id == cuisine.id: name = request.form.get("name") delete = RecipeIngredient.delete().where( RecipeIngredient.name == name, RecipeIngredient.recipe_id == recipe.id) delete.execute() return jsonify({ "message": "Ingredient has been successfully deleted!" }) else: return jsonify({ "errors": recipe_name + " is not found in " + cuisine_name + " cuisine." }) else: return jsonify({"errors": "Recipe not exists"}) else: return jsonify({"errors": cuisine_name + " cusine is not exist"}) else: return 0
def delete_from_cart(user_id, recipe_id): user = User.get_or_none(User.id == user_id) recipe = Recipe.get_or_none(Recipe.id == recipe_id) if current_user.delete_from_cart(recipe): return redirect(url_for('users.view_cart', user_id=current_user.id)) else: return redirect(url_for('users.view_cart', user_id=current_user.id))
def edit_recipe(recipe_id): form = UpdateRecipeForm() that_recipe = Recipe.get_or_none(Recipe.id == recipe_id) recipe_owner = User.get_or_none(User.id == that_recipe.user_id) # cannot use form.validate on submit as it only captures form, hence the CKEditor field details will fail # if form.validate_on_submit(): result = request.form if recipe_owner != current_user: flash("Only owner of the recipe can amend the details", "danger") return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id)) else: if "picture" not in request.files: flash("No picture key in request.files", 'danger') return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id)) file = request.files['picture'] if file.filename == '': flash("Please select a file", 'danger') return redirect(url_for('users.update_user')) if file and allowed_file(file.filename): file.filename = secure_filename(file.filename) output = upload_file_to_s3(file, app.config['S3_BUCKET']) updated_recipe = Recipe.update( title=result['title'], description=result['content'], picture=output ).where(Recipe.user_id == recipe_owner.id) updated_recipe.execute() flash("Recipe updated successfully", "success") return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id)) return render_template('read_that_recipe.html', recipe=that_recipe, form=form)
def choose_ingredient(recipe_id): recipe = Recipe.get_or_none(Recipe.id == recipe_id) ingredients = RecipeIngredient.select().where( RecipeIngredient.recipe_id == recipe.id) return render_template('recipes/choose_ingredient.html', ingredients=ingredients, recipe=recipe)
def add_recipe_ingredient(cuisine_name, recipe_name): current_user = User.get_by_id(get_jwt_identity()) if current_user.is_admin: cuisine = Cuisine.get_or_none(name=cuisine_name) if cuisine: recipe = Recipe.get_or_none(name=recipe_name) if recipe: if recipe.cuisine_id == cuisine.id: quantity = request.form.get("quantity") name = request.form.get("name") recipe_ingredient = RecipeIngredient(name=name, quantity=quantity, recipe_id=recipe.id) recipe_ingredient.save() return jsonify({ "message": "Ingredient has been successfully created!" }) else: return jsonify({ "errors": recipe_name + " is not found in " + cuisine_name + " cuisine." }) else: return jsonify({"errors": "Recipe not exists"}) else: return jsonify({"errors": cuisine_name + " cusine is not exist"}) else: return 0
def user_delete_cart(recipe_id): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) recipe = Recipe.get_or_none(Recipe.id == recipe_id) if user.delete_from_cart(recipe): return jsonify({"message": "Successfully Deleted"}) else: return jsonify({"message": "Error occured"})
def recipe(recipe_id): recipe = Recipe.get_or_none(Recipe.id == recipe_id) return jsonify([{ "id": recipe.id, "recipe_name": recipe.recipe_name, "description": recipe.description, "meal_type": recipe.meal_type, "image_url": app.config.get('S3_LOCATION') + recipe.image_url }])
def delete_recipe_step(cuisine_name, recipe_name): user = User.get_by_id(get_jwt_identity()) cuisine = Cuisine.get_or_none(name=cuisine_name) if user.is_admin: if cuisine: recipe = Recipe.get_or_none(name=recipe_name) if recipe: step_number = request.form.get("step_number") delete = Step.delete().where(Step.number == step_number, Step.recipe_id == recipe.id) delete.execute() return jsonify( {"message": "Step has been successfully deleted."}) else: return jsonify({"errors": "Recipe not exists"}) else: return jsonify({"errors": cuisine_name + " cusine is not exist"}) else: return 0
def new_recipe_step(cuisine_name, recipe_name): user = User.get_by_id(get_jwt_identity()) cuisine = Cuisine.get_or_none(name=cuisine_name) if user.is_admin: if cuisine: recipe = Recipe.get_or_none(name=recipe_name) if recipe: step_number = request.form.get("step_number") step_description = request.form.get("step_description") new_step = Step(number=step_number, description=step_description, recipe_id=recipe.id) new_step.save() return jsonify({"message": "Step successfully created!"}) else: return jsonify({"errors": "Recipe not exists"}) else: return jsonify({"errors": cuisine_name + " cusine is not exist"}) else: return 0
def user_add_cart(recipe_id): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) ingredients = request.json["selectedIngredients"] recipe = Recipe.get_or_none(Recipe.id == recipe_id) subscription_recipes = Subscription_Recipe.select().where( Subscription_Recipe.user == user.id, Subscription_Recipe.created_at.between( fn.date_trunc('week', date.today()), date.today() + timedelta(days=1))) temp = Subscription_Recipe.select().where( Subscription_Recipe.user == user.id, Subscription_Recipe.created_at >= datetime.date.today(), Subscription_Recipe.is_checkedout == 0, Subscription_Recipe.recipe == recipe.id) if temp: return jsonify({"message": "Item is already in the cart"}) else: if len(subscription_recipes) >= (user.subscription.amount_of_meals): return jsonify({ "message": "You have reached the maximum amount of meals selected in a week" }) else: new_subscription_recipe = Subscription_Recipe( user=user.id, subscription=user.subscription.id, recipe=recipe.id) new_subscription_recipe.save() for ingredient in ingredients: user_recipe = Subscription_Recipe.select().where( Subscription_Recipe.recipe == recipe.id, Subscription_Recipe.user == user.id).order_by( Subscription_Recipe.created_at.desc()).get() order = Order(subscription_recipe=user_recipe.id, ingredient=ingredient) order.save() if order.save(): return jsonify({"message": "Successfully added to cart"}) else: return jsonify({"message": "Error occured"})
def show_recipe_all_step(cuisine_name, recipe_name): cuisine = Cuisine.get_or_none(name=cuisine_name) if cuisine: recipe = Recipe.get_or_none(name=recipe_name) if recipe: step_all = Step.select().where(Step.recipe_id == recipe.id) results = [] for step in step_all: step_data = { "step_number": step.number, "step_description": step.description } results.append(step_data) return jsonify({"data": results}) else: return jsonify({ "errors": recipe_name + " is not found in " + cuisine_name + " cuisine." }) else: return jsonify({"errors": cuisine_name + " cusine is not exist"})
def add_to_cart(recipe_id): recipe = Recipe.get_or_none(Recipe.id == recipe_id) ingredients = request.form.getlist('ingredients') subscription_recipe = Subscription_Recipe( user=current_user.id, subscription=current_user.subscription, recipe=recipe.id) subscription_recipe.save() user_recipe = Subscription_Recipe.select().where( Subscription_Recipe.recipe == recipe.id, Subscription_Recipe.user == current_user.id).order_by( Subscription_Recipe.created_at.desc()).get() for ingredient in ingredients: order = Order(subscription_recipe=user_recipe.id, ingredient=ingredient) order.save() if subscription_recipe.save() and order.save(): flash("Successfully added to cart", "success") return redirect(url_for('recipes.show')) else: flash("Failed to add to cart", "danger") return redirect(url_for('recipes.show'))
def delele_recipe(): id = get_jwt_identity() recipe_id = request.json.get('recipe_id', None) user = User.get_or_none(User.id == id) recipe = Recipe.get_or_none(Recipe.id == recipe_id) delete = Recipe.delete().where(Recipe.user_id == user.id, Recipe.id == recipe.id) delete.execute() user = User.get_or_none(User.id == id) recipes = Recipe.select().where(Recipe.user_id == id) # url_photo = recipe.photo my_recipes = Recipe.select().where(Recipe.user_id == id) recipesNum = len([c.id for c in my_recipes]) return jsonify({ "status": "success", "recipesNum": recipesNum, "recipes": [{ "id": recipe.id, "name": recipe.name, "photo": recipe.photo, "countrys": recipe.countrys, "hour": recipe.hour, "sec": recipe.sec, "directions": recipe.directions, "ingredients": recipe.ingredients, "time": recipe.time, "username": user.username, "user_id": user.id, "user_photo": user.photo } for recipe in recipes] }), 200
def upload_image(recipe_id): recipe = Recipe.get_or_none(Recipe.id == recipe_id) if "recipe_image" not in request.files: return "No recipe_image key in request.files" file = request.files["recipe_image"] if file.filename == "": return "Please select a file" if file: file_path = upload_to_s3(file, "recipe") recipe.image_url = file_path if recipe.save(): flash("Image Uploaded") return redirect(url_for("recipes.edit", recipe_id=recipe.id)) else: flash("An error occured") return redirect(url_for("recipes.edit", recipe_id=recipe.id)) else: flash("No file selected") return redirect(url_for("recipes.edit", recipe_id=recipe.id))
def show_recipe_ingredient(cuisine_name, recipe_name): cuisine = Cuisine.get_or_none(name=cuisine_name) if cuisine: recipe = Recipe.get_or_none(name=recipe_name) if recipe: if recipe.cuisine_id == cuisine.id: ingredient = RecipeIngredient.select().where( RecipeIngredient.recipe_id == recipe.id) results = [] for i in ingredient: ingredient_data = {"name": i.name, "quantity": i.quantity} results.append(ingredient_data) return jsonify({"data": results}) else: return jsonify({ "errors": recipe_name + " is not found in " + cuisine_name + " cuisine." }) else: return jsonify({"errors": "Recipe not exists"}) else: return jsonify({"errors": cuisine_name + " cusine is not exist"})
def read_recipe(recipe_id): form = UpdateRecipeForm() that_recipe = Recipe.get_or_none(Recipe.id == recipe_id) recipe_user_id = that_recipe.user_id return render_template('read_that_recipe.html', recipe=that_recipe, form=form, user=recipe_user_id)
def edit(recipe_id): recipe = Recipe.get_or_none(Recipe.id == recipe_id) return render_template('recipes/edit.html', recipe_id=recipe.id)