def recipes_create(): form = RecipeForm(request.form) form.recipe_id = -1 # Checking that the form passes validations if not form.validate(): return render_template("recipes/new.html", form=form) # Adding the new recipe name = form.name.data.strip() name = (name[0].upper() + name[1:]) newRecipe = Recipe(name) newRecipe.instruction = form.instruction.data newRecipe.preptime = form.preptime.data newRecipe.account_id = current_user.id # Separating and adding tags tagsString = form.tags.data.strip() tags = tagsString.split(',') Tag.add_tags(tags, newRecipe) # Commiting changes db.session().add(newRecipe) db.session().commit() # Ingredients need recipe ID, # so they are added only after the recipe is added addedRecipe = Recipe.query.filter(Recipe.name == newRecipe.name).first() ingredients = form.ingredients.data.splitlines() Ingredient.add_ingredients(ingredients, addedRecipe) return redirect(url_for("recipes_index"))
def recipes_index(): recipeList = Recipe.find_recipes_with_ingredients() recipeCount = Recipe.get_recipe_count() return render_template("recipes/list.html", has_ingredients=recipeList, recipeCount=recipeCount)
def recipes_statistics(): return render_template( "recipes/statistics.html", recipe_count=Recipe.get_recipe_count(), most_used_ing_name=Recipe.get_most_used_ingredient()[0].get("name"), most_used_ing_count=Recipe.get_most_used_ingredient()[0].get("uses"))
def recipes_ingredients(recipeId): if not recipeAuthorization(recipeId): return redirect(url_for("recipes_show")) if request.method == "GET": return render_template("/recipes/ingredients.html", form=IngredientForm(), ingredients=Recipe.list_recipes_ingredients(recipeId), recipeId=recipeId) form = IngredientForm(request.form) if not form.validate(): return render_template("/recipes/ingredients.html", form=form, ingredients=Recipe.list_recipes_ingredients(recipeId), recipeId=recipeId) try: itemId = Item.get_matching_item(form.name.data).get("id") ingredient = Ingredient(form.amount.data, form.name.data, recipeId, itemId) except: item = Item(Item.name_to_lexeme(form.name.data)) db.session().add(item) db.session().flush() ingredient = Ingredient(form.amount.data, form.name.data, recipeId, item.id) db.session().add(ingredient) db.session().commit() return redirect(url_for("recipes_ingredients", recipeId=recipeId))
def recipes_create(): if request.method == "GET": form = NewRecipeForm() form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.ingredients.append_entry({}) return render_template("recipes/new.html", form=form) form = NewRecipeForm(request.form) if not form.validate(): return render_template("recipes/new.html", form=form) recipe = Recipe(form.header.data, form.category.data, form.description.data, form.directions.data) recipe.account_id = current_user.id db.session().add(recipe) db.session().flush() for ingredientForm in form.ingredients.data: if ingredientForm['ingredientName']: ingredient = Ingredient.query.filter_by( name=ingredientForm['ingredientName'].lower()).first() if not ingredient: ingredient = Ingredient( ingredientForm['ingredientName'].lower()) db.session().add(ingredient) db.session().flush() recipeIngredient = RecipeIngredient( ingredientForm['ingredientAmount'], ingredientForm['ingredientUnit']) recipeIngredient.recipe_id = recipe.id recipeIngredient.ingredient_id = ingredient.id db.session().add(recipeIngredient) db.session().commit() return redirect(url_for("recipes_index", user_id=recipe.account_id))
def recipe_create(): form = RecipeForm(request.form) r = Recipe(form.name.data, form.description.data) r.creator = current_user.id if not form.validate(): return render_template("recipes/new.html", form=form) db.session().add(r) db.session().commit() return redirect(url_for("recipes_index"))
def recipes_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/new.html", form=form) r = Recipe(form.name.data, form.text.data) r.difficult = form.difficult.data r.event = form.event.data r.account_id = current_user.id db.session().add(r) db.session().commit() return redirect(url_for("recipes_list"))
def recipes_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/new.html", form=form) r = Recipe(form.name.data) r.favourite = form.favourite.data r.account_id = current_user.id r.description = cleanstr(form.description.data) db.session().add(r) db.session().commit() return redirect(url_for("recipes_index"))
def recipes_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/new.html", form=form) recipe = Recipe(form.name.data) recipe.description = form.description.data recipe.content = form.content.data recipe.account_id = current_user.id db.session().add(recipe) db.session().commit() return redirect(url_for("recipes_index"))
def recipe_create(): form = RecipeForm(request.form) ingredients = Ingredient.query.all() form.ingredients.choices = [(ingredient.ingredientid, ingredient.name) for ingredient in ingredients] r = Recipe(form.name.data, form.method.data, current_user.id) ingredient_ids = form.ingredients.data if not form.validate(): return render_template("recipes/new.html", form=form) db.session().add(r) db.session().commit() db.session.refresh(r) for id in ingredient_ids: i = Ingredient.query.get(id) r.recipeingredient.append(i) db.session().commit() return redirect(url_for("recipe_index"))
def recipes_by_categories(category_id): recipes = Recipe.get_recipes_by_category(category_id) votes = Vote.query.all() votecount = dict() votedOn = [] comments = Comment.query.all() commentcount = dict() category = Category.query.filter_by(id=category_id).first() for vote in votes: if not vote.recipe_id in votecount: votecount[vote.recipe_id] = 1 else: votecount[vote.recipe_id] += 1 for comment in comments: if not comment.recipe_id in commentcount: commentcount[comment.recipe_id] = 1 else: commentcount[comment.recipe_id] += 1 if current_user.is_authenticated: votedOnQuery = Vote.query.filter( Vote.account_id == current_user.id).all() for vote in votedOnQuery: votedOn.append(vote.recipe_id) users = User.query.all() return render_template("recipes/list.html", recipes=recipes, users=users, votes=votecount, votedOn=votedOn, commentcount=commentcount, category=category)
def recipes_ingredient_statistics(): ingredientCount = Recipe.count_ingredient_recipe() ingredients = Ingredient.count_ingredient_usage() return render_template("stats.html", ingredientCount=ingredientCount, ingredients=ingredients)
def recipe_edit(recipe_id): # POST is not accepted if current user is not the creator of the recipe # or an administrator recipe = Recipe.query.get(recipe_id) if ((recipe.account_id is not current_user.get_id()) and (current_user.get_role() != 'admin')): return abort(401) form = RecipeEditForm(request.form) # If form does not pass validations, # a new faulty form is created to be shown along with error messages, # but never put to the database if not form.validate(): faultyRecipe = Recipe(request.form['name']) faultyRecipe.id = recipe_id faultyRecipe.instruction = request.form['instruction'] faultyRecipe.preptime = request.form.get("preptime") faultyIngredients = request.form.get("ingredients") faultyTags = request.form.get("tags") return render_template("recipes/edit.html", recipe=faultyRecipe, form=form, tags=faultyTags, ingredients=faultyIngredients) # Fetching and editing the recipe changedRecipe = Recipe.query.get(recipe_id) name = request.form.get("name").strip() name = name[0].upper() + name[1:] changedRecipe.name = name changedRecipe.instruction = request.form.get("instruction") changedRecipe.preptime = request.form.get("preptime") # Add tags for the recipe tags = form.tags.data.split(',') Tag.add_tags(tags, changedRecipe) db.session().commit() ingredients = request.form.get("ingredients").splitlines() Ingredient.add_ingredients(ingredients, changedRecipe) return redirect(url_for("recipes_index"))
def recipe(recipeId): recipe = Recipe.query.get(recipeId) ingredients = Recipe.list_recipes_ingredients(recipeId) return render_template("/recipes/recipe.html", recipe=recipe, ingredients=ingredients, authorizedToDelete=recipeAuthorization(recipeId))
def go_to_user_info(user_id): user = User.query.get(user_id) recipeSum = Recipe.count_my_recipes(user_id) return render_template("user/userinfo.html", user=user, recipeSum=recipeSum)
def recipes_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/new.html", form=form) name = form.name.data duration = form.duration.data instructions = form.instructions.data recipe = Recipe(name, duration, instructions) recipe.account_id = current_user.id recipe.tags = get_tags(form) db.session().add(recipe) db.session().commit() return redirect(url_for("recipes_index"))
def go_to_statistics(): recipesPerUser = Recipe.list_how_many_recipes_per_user() ingredientsPerRecipe = Ingredient.list_ingredients_per_recipe() return render_template("statistics/statistics.html", recipesPerUser=recipesPerUser, ingredientsPerRecipe=ingredientsPerRecipe)
def recipes_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/new.html", form=form) r = db.session.query(Recipe).filter( Recipe.name == form.name.data).one_or_none() if r is not None: flash("Recipe you are trying to add already exists", "warning") elif form.name.data.__len__() < 2: flash("Recipe name has to be longer than 1 character", "warning") else: flash("Recipe has been added", "success") t = Recipe(form.name.data) t.account_id = current_user.id db.session().add(t) ingredients_string = form.ingredientString.data ingredients = [x.strip() for x in ingredients_string.split(',')] for ingredient in ingredients: x = db.session.query(Ingredient).filter( Ingredient.name == ingredient).one_or_none() if not x: if ingredient.__len__() < 2: flash("Ingredient name has to be longer than 1 character", "warning") else: x = Ingredient(ingredient) x.account_id = current_user.id db.session().add(x) t.recipeIngredients.append(x) try: db.session().commit() except Exception as e: print(str(e)) return redirect(url_for("recipes_create"))
def recipe_info(recipe_id): recipe = Recipe.query.get(recipe_id) tags = Recipe.find_recipe_tags(recipe) recipeCreator = User.query.filter_by(id=recipe.account_id).first() ingredients = Ingredient.query.filter_by(recipe_id=recipe_id) return render_template("recipes/recipe.html", recipe=recipe, recipeCreator=recipeCreator, tags=tags, ingredients=ingredients)
def recipes_create(): if request.method == "GET": return render_template("recipes/new.html", form=RecipeForm()) form = RecipeForm(request.form) if not form.validate(): return render_template("/recipes/new.html", form=form) url = uploadImage(form.image.name) r = Recipe(form.name.data, form.instructions.data, url) r.account_id = current_user.id db.session().add(r) db.session().flush() recipeId = r.id db.session().commit() return redirect(url_for("recipes_ingredients", recipeId=recipeId))
def recipe_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/newrecipe.html", form=form) newrecipe = Recipe(form.name.data, form.ingredients.data, form.recipetext.data, form.tips.data) newrecipe.account_id = current_user.id db.session().add(newrecipe) db.session.commit() for category in form.categories.data: newrecipecategory = RecipeCategory(newrecipe.id, category.id) db.session.add(newrecipecategory) db.session.commit() return redirect(url_for("recipes_index"))
def recipes_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/form.html", form=form, form_action=url_for("recipes_create"), button_text="Create recipe") recipe = Recipe(form.name.data.strip()) recipe.servings = form.servings.data recipe.instructions = form.instructions.data recipe.account_id = current_user.id recipe.account_name = current_user.name ingredients = form.ingredients.data db.session().add(recipe) db.session().flush() new = create_ingredients(ingredients, recipe.id) db.session().commit() return redirect(url_for("recipes_view", recipe_id=recipe.id))
def recipe_form(): if request.method == "GET": return render_template("/recipes/create.html", form = RecipeForm()) form = RecipeForm(request.form) if not form.validate: return render_template("/recipes/create.html", form = form) r = Recipe(form.name.data, form.instructions.data, form.diet.data, form.timeInMinutes.data) db.session().add(r) db.session().commit() return redirect(url_for("recipes_index"))
def recipes_create(): form = RecipeForm(request.form) if not form.validate(): return render_template("recipes/new.html", form=form) nimi = form.nimi.data annoksia = form.annoksia.data aika = form.aika.data ohje = form.ohje.data r = Recipe(nimi, annoksia, aika, ohje) db.session().add(r) db.session().commit() return redirect(url_for("recipes_index"))
def recipes_index(): form = SearchForm() form.min_rating.data = request.args.get("min_rating") form.recipe_name.data = request.args.get("recipe_name") form.tag_name.data = request.args.get("tag_name") if not form.validate(): return redirect(url_for("recipes_index")) recipes = None min_rating = form.min_rating.data recipe_name = request.args.get("recipe_name") tag_name = request.args.get("tag_name") recipes = Recipe.filter_recipes(min_rating, recipe_name, tag_name) return render_template("recipes/list.html", recipes=recipes, form=SearchForm())
def add_tags(tags, recipe): prev_tags = Recipe.find_recipe_tags(recipe) for tag in prev_tags: rmtag = Tag.query.filter(Tag.id == tag[0]).first() recipe.tags.remove(rmtag) for tag in tags: if tag == "": break # Format tags to obey following rules: # no leading or trailing whitespace # capitalized, otherwise lowercase tag = tag.strip().lower().capitalize() tagExists = Tag.query.filter(Tag.name == tag).first() if tagExists: recipe.tags.append(tagExists) else: newTag = Tag(tag) recipe.tags.append(newTag)
def recipes_search(): account_id = "" if current_user.is_authenticated: account_id = current_user.id if request.method == 'POST': results = Recipe.search_by_term( recipe=request.form.get("recipe"), ingredient=request.form.get("ingredient"), category=request.form.get("category"), term=request.form.get("search")) return render_template("recipes/search.html", results=results, account_id=account_id) else: return render_template("recipes/search.html", recipes=Recipe.query.all(), account_id=account_id)
def recipes_create(): form = RecipeForm().new() if request.method == "GET": return render_template("recipes/new.html", form=form) if request.method == "POST" and form.validate(): try: r = Recipe(form.name.data, form.instruction.data) r.account.append(current_user) for data in form.ingredients.data: ingredient = Ingredient.query.filter_by(name=data).first() r.ingredient.append(ingredient) db.session().add(r) db.session().commit() return redirect(url_for("recipes_index")) except: flash("could not connect to database") return redirect(url_for("error_page", message="Error connecting to database")) return render_template("recipes/new.html", form=form)
def recipe_editform(recipe_id): fetched_recipe = Recipe.query.get(recipe_id) fetched_tags = Recipe.find_recipe_tags(fetched_recipe) joined_tags = "" tags_length = len(fetched_tags) for i in range(tags_length): joined_tags += fetched_tags[i][1] if i < tags_length - 1: joined_tags += ', ' fetched_ingredients = Ingredient.find_recipe_ingredients(fetched_recipe) joined_ingredients = "" for ingredient in fetched_ingredients: joined_ingredients += ingredient.line + "\n" return render_template("recipes/edit.html", recipe=fetched_recipe, form=RecipeEditForm(), tags=joined_tags, ingredients=joined_ingredients)
def recipe_view(recipe_id): recipe = Recipe.query.get(recipe_id) if not recipe: return redirect(url_for("recipes_index")) review = None if current_user.is_authenticated: review = Review.query.filter_by(account_id=current_user.id, recipe_id=recipe_id).first() form = ReviewForm() if review: form.rating.data = review.rating rating = Recipe.get_average_rating(recipe_id) return render_template("recipes/view.html", recipe=recipe, form=form, rating=rating, review_exists=review is not None)