def create_recipe(request): if request.method == 'GET': form = RecipeForm() return render(request, 'create.html', {'form': form}) else: form = RecipeForm(request.POST) if form.is_valid(): form.save() return redirect('index') return render(request, 'create.html', {'form': form})
def update_recipe(recipe_id): if current_user.is_admin: recipe = Recipe.query.get_or_404(recipe_id) form = RecipeForm() form.poll.choices = [(poll.id, poll.name) for poll in Poll.query.order_by('date_created')] if form.validate_on_submit(): recipe.name = form.name.data recipe.description = form.description.data recipe.poll = Poll.query.filter_by(id=form.poll.data).first() db.session.commit() flash('The recipe has been updated!', 'success') return redirect(url_for('recipe', recipe_id=recipe.id)) elif request.method == 'GET': form.name.data = recipe.name form.description.data = recipe.description form.poll.data = recipe.poll.id return render_template('recipe_form.html', title='Update - ' + recipe.name + ' - Recipe', description='Fill in the details to be changed', legend='Update - ' + recipe.name + ' - Recipe', form=form) else: abort(403)
def post(self, recipe_id): form = RecipeForm(request.form) if not form.validate_on_submit(): save_form_to_session(request.form) return redirect(url_for("RecipeView:edit", id=self.recipe.id)) form.populate_obj(self.recipe) self.recipe.edit() self.recipe.reload() if turbo.can_stream(): return turbo.stream([ turbo.replace( self.template("_info", message="Upraveno", form=form), target="recipe-info", ), turbo.replace( self.template("_ingredient_table"), target="recipe-ingredient-table", ), ]) else: return redirect(url_for("RecipeView:edit", id=self.recipe.id))
def add_recipe(): if current_user.is_admin: form = RecipeForm() form.poll.choices = [(poll.id, poll.name) for poll in Poll.query.order_by('date_created')] if form.validate_on_submit(): recipe = Recipe(name=form.name.data, description=form.description.data, contributor_id=current_user.id, poll_id=form.poll.data) db.session.add(recipe) # adds to the database db.session.commit() # commits all the changes in the database flash('The recipe has been added', 'success') return redirect(url_for('add_recipe')) return render_template('recipe_form.html', title='Add Recipe', description='Fill in the details of the recipe', legend='Add a Recipe', form=form) else: abort(403)
def edit_recipe(recipe_name): recipe = Recipe.query.filter_by(name=recipe_name).first_or_404() if current_user.is_authenticated and current_user == recipe.author: form = form_ingredients_and_allergens( RecipeForm(obj=recipe, original_name=recipe.name)) if form.validate_on_submit() and form.validate_recipe_name: f = form.image.data filename = secure_filename(f.filename) file_path = os.path.join("app/static/img/recipes_images", filename) f.save(file_path) recipe.name = "non-existent" created_recipe = Recipe( name=form.name.data, short_description=form.short_description.data, content=form.content.data, cuisine=form.cuisine.data, category=form.category.data, time_to_prepare=form.time_to_prepare.data, serves_num_people=form.serves_num_people.data, cooking_time=form.cooking_time.data, image="static/img/recipes_images/" + filename, author=current_user, calories=form.calories.data, carbohydrates=form.carbohydrates.data, proteins=form.proteins.data, fats=form.fats.data, cholesterol=form.cholesterol.data) ingredients_in_recipe = [] for ingredient in form.ingredients.data: queried_ingredient = Ingredient.query.filter_by( id=ingredient).first() ingredients_in_recipe.append(queried_ingredient) created_recipe._ingredients = ingredients_in_recipe allergens_in_recipe = [] for allergen in form.allergens.data: queried_allergen = Allergen.query.filter_by( id=allergen).first() allergens_in_recipe.append(queried_allergen) created_recipe._allergens = allergens_in_recipe try: db.session.query(Recipe).filter( Recipe.id == recipe.id).delete() db.session.commit() db.session.add(created_recipe) db.session.commit() except: print("There was an error while saving changes.") flash("Congrats, you have edited a recipe!") return redirect(url_for('recipes_list')) else: flash("You need to login before you edit recipe.") return redirect(url_for('login')) return render_template("edit_recipe.html", title='Edit Recipe', form=form, recipe=recipe)
def new_recipe(): form = RecipeForm() if form.validate_on_submit(): recipe = Recipe() save_recipe(recipe, form) flash("Recipe {} added successfully".format(form.name.data)) return redirect("/index") return render_template("edit.html", title="Create recipe", form=form)
def edit_recipe(id): recipe = Recipe.query.filter_by(id=id).first_or_404() form = RecipeForm(obj=recipe) if form.validate_on_submit(): save_recipe(recipe, form) flash("Recipe {} saved succesfully".format(form.name.data)) return redirect("/recipe/{}".format(recipe.id)) return render_template("edit.html", title="Edit recipe", form=form)
def edit_recipe(request, pk): recipe = Recipe.objects.get(pk=pk) if request.method == 'GET': form = RecipeForm(instance=recipe) context = { 'recipe': recipe, 'form': form, } return render(request, 'edit.html', context) else: form = RecipeForm(request.POST, instance=recipe) if form.is_valid(): form.save() return redirect('index') context = { 'form': form, } return render(request, 'edit.html', context)
def add_recipe(): if current_user.is_authenticated: form = form_ingredients_and_allergens(RecipeForm(original_name="")) if form.validate_on_submit(): f = form.image.data print(form.image.data) filename = secure_filename(f.filename) file_path = os.path.join("app/static/img/recipes_images", filename) f.save(file_path) recipe = Recipe(name=form.name.data, short_description=form.short_description.data, content=form.content.data, cuisine=form.cuisine.data, category=form.category.data, time_to_prepare=form.time_to_prepare.data, serves_num_people=form.serves_num_people.data, cooking_time=form.cooking_time.data, image="static/img/recipes_images/" + filename, author=current_user, calories=form.calories.data, carbohydrates=form.carbohydrates.data, proteins=form.proteins.data, fats=form.fats.data, cholesterol=form.cholesterol.data) ingredients_in_recipe = [] for ingredient in form.ingredients.data: queried_ingredient = Ingredient.query.filter_by( id=ingredient).first() ingredients_in_recipe.append(queried_ingredient) recipe._ingredients = ingredients_in_recipe allergens_in_recipe = [] for allergen in form.allergens.data: queried_allergen = Allergen.query.filter_by( id=allergen).first() allergens_in_recipe.append(queried_allergen) recipe._allergens = allergens_in_recipe try: db.session.add(recipe) db.session.commit() except: print("There was a DB error while saving Recipe.") print("Recipe added") flash("Congrats, you have added a recipe!") return redirect(url_for('recipe', recipe_name=form.name.data)) else: print("Something has failed") else: flash("You need to login before you add recipe.") return redirect(url_for('login')) return render_template("add_recipe.html", title='Add Recipe', form=form)
def new_recipe(): recipe_form = RecipeForm() if recipe_form.validate_on_submit(): user = current_user recipe = Recipe(body=recipe_form.name.data, user_id=user.id) db.session.add(recipe) db.session.commit() flash('New recipe added!') return redirect(url_for('index')) return render_template('new_recipe.html', recipe_form=recipe_form)
def post(self): form = RecipeForm(request.form) if not form.validate_on_submit(): save_form_to_session(request.form) return redirect(url_for("RecipeView:new")) recipe = Recipe(author=current_user) recipe.fill(form) recipe.save() return redirect(url_for("RecipeView:edit", id=recipe.id))
def submit(): """submit a recipe""" form = RecipeForm() if form.validate_on_submit(): recipe = Recipe(title=form.title.data, ingredients=form.ingredients.data, instructions=form.instructions.data, author=current_user) db.session.add(recipe) db.session.commit() return redirect(f'/recipe/{recipe.id}') return render_template('submit.html', title="submit", form=form)
def new_recipe(): form = RecipeForm() if form.validate_on_submit(): recipe = Recipe(name=form.name.data, author=current_user, notes=form.notes.data) db.session.add(recipe) db.session.commit() flash('Receptet har sparats.') return redirect(url_for('recipe', recipe_id=recipe.id)) return render_template('new_recipe.html', form=form, headline='Nytt recept')
def edit_recipe(rid): target = Recipe.query.filter_by(id=rid).one_or_none() if (current_user.admin or target.author == current_user) or target is None: form = RecipeForm() form.submit_button.label = f"Edit {target.title}" if form.validate_on_submit(): target.title = form.title.data target.instructions = form.instructions.data target.ingredients = form.ingredients.data db.session.add(target) db.session.commit() flash(f'successfully edited {target.title}') return redirect(f'/recipe/{rid}') else: flash('you do not have permission to edit this recipe') redirect('/') form = RecipeForm(title=target.title, ingredients=target.ingredients, instructions=target.instructions) return render_template('submit.html', title=f'Editing {target.title}', form=form)
def new_recipe(): form = RecipeForm() if form.validate_on_submit(): recipe = Recipe(title=form.title.data, content=form.content.data, ingredients=form.ingredients.data, image_file_recipe=form.picture.data, author=current_user) db.session.add(recipe) db.session.commit() flash('Twój przepis został dodany!', 'success') return redirect(url_for('home')) return render_template('new_recipe.html', title='New Title', form=form, legend='Stwórz nowy przepis')
def add_recipe(): form = RecipeForm() if form.validate_on_submit(): recipe = Recipe(recipe_name=form.recipe_name.data, description=form.description.data, cook_time=form.cook_time.data, start_day_before=form.start_day_before.data, lunchbox=form.lunchbox.data, author=current_user) db.session.add(recipe) db.session.commit() flash('Your recipe has been saved') return redirect(url_for('home')) return render_template('add_recipe.html', title='Add your awesome recipe here', form=form)
def upload_image(): form = RecipeForm() if request.method == 'POST': if request.files: image = request.files["image"] if image.filename == '': flash('Nie wybrano pliku.') return redirect(request.url) image.save( os.path.join(app.config["IMAGE_UPLOADS"], image.filename)) print("imaged saved") return redirect(request.url) return render_template('upload_image.html')
def update_recipe(recipe_id): recipe = Recipe.query.get_or_404(recipe_id) if recipe.author != current_user: abort(403) form = RecipeForm() if form.validate_on_submit(): recipe.title = form.title.data recipe.content = form.content.data recipe.ingredients = form.ingredients.data db.session.commit() flash('Twój przepis jest zaktualizowany', 'success') elif request.method == 'GET': form.title.data = recipe.title form.content.data = recipe.content form.ingredients.data = recipe.ingredients return render_template('new_recipe.html', title='Update Recipe', form=form, legend='Edytuj przepis')
def edit_recipe(recipe_id=None): recipe = Recipe.query.get(int(recipe_id)) if not recipe.author == current_user: flash('Du kan bara redigera dina egna recept') return redirect(url_for('recipe', recipe_id=recipe_id)) form = RecipeForm() form.name.data = recipe.name form.notes.data = recipe.notes if form.validate_on_submit(): recipe.name = request.form.get('name', None) recipe.notes = request.form.get('notes', None) db.session.commit() flash('Receptet har redigerats.') return redirect(url_for('recipe', recipe_id=recipe_id)) return render_template('edit_recipe.html', title='Redigera', recipe=recipe, headline='Redigera recept', form=form)
def add_recipe(): form = RecipeForm() quantities = MeasurementQty.query.all() units = MeasurementUnit.query.order_by(MeasurementUnit.factor).all() ingredients = Ingredient.query.order_by(Ingredient.name).all() if form.validate_on_submit(): # Could choose to set default author to logged in user if none is supplied recipe = Recipe(name=form.recipe.data, description=form.description.data, author=current_user, servings=form.servings.data, comments=form.comments.data, source=form.source.data) # Tags # No implemented yet # Ingredients db.session.add(recipe) if form.ingredient.validate(form): for ingredient in form.ingredient.data: query = IngredientSet.add_set( quantity=ingredient['quantity'], unit=ingredient['unit'], ingredient=ingredient['ingredient']) if query is not None: recipe.add_ingredient(query) else: print('Set does not exist placeholder') # Steps if form.ingredient.validate(form): for step in form.step.data: step = RecipeStep(step_number=step['step_number'], step_text=step['step']) recipe.steps.append(step) db.session.commit() flash('Recipe added') return redirect(url_for('index')) return render_template('add_recipe.html', title='Add recipe', form=form, quantities=quantities, units=units, ingredients=ingredients)
def postrecipe(): form = RecipeForm() search_form = SearchForm() if form.validate_on_submit(): mongo.db.recipes.insert_one({ 'name': form.recipe_name.data, 'desc': form.recipe_desc.data, 'ingredients': form.ingredients.data, 'method': form.method.data, 'owner': current_user.username, 'tags': form.tags.data, 'image': form.image.data, 'likes': [] }) flash('Recipe added!', 'success') return redirect(url_for('index')) return render_template('add_recipe.html', form=form, search_form=search_form, title='Post Recipe')
def copy_recipe(recipe_id): original = Recipe.query.get(int(recipe_id)) form = RecipeForm() form.name.data = original.name form.notes.data = original.notes if form.validate_on_submit(): copy = Recipe(name=request.form.get('name', None), notes=request.form.get('notes', None), user_id=current_user.id, original_author_id=original.author.id, original_recipe_id=original.id) db.session.add(copy) db.session.commit() flash('Receptet har sparats') return redirect(url_for('recipe', recipe_id=copy.id)) return render_template('edit_recipe.html', title='Spara en kopia', headline='Spara en kopia', form=form, recipe=original)
def recipes(): form = RecipeForm() if form.validate_on_submit(): recipe = Recipe(name=form.name.data) db.session.add(recipe) db.session.commit() flash(_('Your recipe added!')) return redirect(url_for('recipe', name=recipe.name)) page = request.args.get('page', 1, type=int) recipes = Recipe.query.order_by(Recipe.name).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('recipes', page=recipes.next_num) \ if recipes.has_next else None prev_url = url_for('recipes', page=recipes.prev_num) \ if recipes.has_prev else None return render_template('recipes.html', title=_('Recipes'), form=form, recipes=recipes.items, next_url=next_url, prev_url=prev_url)
def editrecipe(id): form = RecipeForm() search_form = SearchForm() recipe = mongo.db.recipes.find_one({"_id": ObjectId(id)}) # stops user editing recipes not owned by finding recipe id in source code if current_user.username == recipe['owner'] or current_user.is_admin: if request.method == "POST": if form.validate_on_submit(): mongo.db.recipes.update_one({"_id": ObjectId(id)}, { "$set": { 'name': form.recipe_name.data, 'desc': form.recipe_desc.data, 'ingredients': form.ingredients.data, 'method': form.method.data, 'tags': form.tags.data, 'image': form.image.data, 'owner': current_user.username } }) flash('Recipe Updated', 'info') return redirect(url_for('index')) return render_template('add_recipe.html', form=form, search_form=search_form, title='Edit Recipe') elif request.method == "GET": # Populates recipe form with data from db form.recipe_name.data = recipe['name'] form.recipe_desc.data = recipe['desc'] for ingredient in recipe['ingredients']: form.ingredients.append_entry(data=ingredient) form.method.data = recipe['method'] form.image.data = recipe['image'] form.tags.data = ', '.join(map(str, recipe['tags'])) return render_template('add_recipe.html', form=form, search_form=search_form, title='Edit Recipe') flash('Action not allowed', 'warning') return redirect(url_for('index'))
def newRecipe(): form = RecipeForm(request.form) if request.method=="POST": uploadedfile = request.files['uploadedfile'] if uploadedfile and allowed_file(uploadedfile.filename): uploadedfilename = form.name.data + '_' + str(time.strftime("%Y-%m-%d-%H-%M-%S")) + "_" + secure_filename(uploadedfile.filename) filepath = os.path.join(os.getcwd() + '/app/static/recipeuploads/',uploadedfilename) uploadedfile.save(filepath) connection = engine.raw_connection() cursor = connection.cursor() cursor.callproc("AddRecipe", [str(form.name.data),str(form.recipetype.data),str(uploadedfilename),str(form.serving.data),str(form.preptime.data),str(time.strftime("%Y/%m/%d")),str(form.caloriecount.data)]) result = cursor.fetchall() cursor.close() connection.commit() firstconnection = engine.connect() result = firstconnection.execute("SELECT MAX(recipe_id) FROM recipe LIMIT 1;") for row in result: iidd = row['MAX(recipe_id)'] print iidd firstconnection.close() return redirect(url_for('recipes')) else: return render_template("recipe.html",form=form)
def addRecipe(): form = RecipeForm() if request.method == 'POST' and form.validate_on_submit(): title = form.title.data desc = form.desc.data prep = form.prep.data cal = form.cal.data date = datetime.today().strftime("%y-%m-%d") cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor) cursor.execute('Select RecID from recipes order by length(RecID) DESC,RecID Desc Limit 1') lastrid = cursor.fetchone()['RecID'].split('-')[1] cursor.execute('Select DescID from recipe_description order by length(DescID) DESC,DescID Desc Limit 1') lastdid = cursor.fetchone()['DescID'].split('-')[1] cursor.execute('Insert into recipe_description values (%s,%s)', ('DESC-' + str(int(lastdid)+1),desc)) cursor.execute('Insert into recipes values (%s,%s,%s,%s,%s,%s)', ('RE-' + str(int(lastrid)+1),title,cal,'DESC-' + str(int(lastdid)+1),date,prep)) mysql.connection.commit() flash('Recipe Added') print(cal,date) return redirect(url_for('recipe',id='RE-' + str(int(lastrid)+1))) return render_template('addRecipe.html',form=form)