def recipe_detail(recipe_id): """ Renders a recipe detail page for the recipe matching recipe_id. :param recipe_id: ID of the recipe to display. :return: The rendered page. """ recipe = Item.query.get_or_404(recipe_id) photo = models.load_image_base64(recipe.photo) return flask.render_template( 'recipe_detail.html', recipe=recipe, photo=photo, stats=get_stats())
def recipe_photo(recipe_id): """ Renders the photo linked to the recipe with ID recipe_id in an HTML document. :param recipe_id: ID of the recipe to display the photo from. :return: The rendered page, or 404 error if the recipe doesn't exist or it doesn't have a photo. """ recipe = Item.query.get_or_404(recipe_id) if not recipe.photo: flask.abort(404) photo = models.load_image_base64(recipe.photo) return flask.render_template("photo.html", photo=photo, stats=get_stats())
def edit_recipe(recipe_id): """ Renders the recipe editing form. :param recipe_id: ID of the recipe to edit. :return: The rendered page. """ recipe = Item.query.get_or_404(recipe_id) form = EditRecipeForm(obj=recipe) form.cuisine.choices = [(c.id, c.name) for c in Cuisine.query.order_by('name')] if recipe.user_id != current_user.id: flask.flash('You do not have permission to edit this recipe.', 'error') return flask.redirect(flask.url_for('recipe_detail', recipe_id=recipe_id)) if form.validate_on_submit(): try: # Save photo in disk if one was uploaded. filepath = save_photo(form) # Delete current photo models.delete_file(recipe.photo) except OSError as e: flask.flash("Something went wrong. Please contact support.") return flask.redirect(flask.url_for('edit_recipe')) # Update recipe info with data from the form recipe.name = form.name.data recipe.description = form.description.data recipe.cuisine_id = form.cuisine.data recipe.updated_at = datetime.now().replace(microsecond=0) if filepath: recipe.photo = filepath db.session.commit() flask.flash('Record updated successfully!') return flask.redirect(flask.url_for('recipe_detail', recipe_id=recipe_id)) # Set current value if rendering form form.cuisine.data = recipe.cuisine_id photo = models.load_image_base64(recipe.photo) return flask.render_template('form_edit_recipe.html', form=form, recipe=recipe, photo=photo, stats=get_stats())