Exemple #1
0
def edit(recipe_id):
    """Recipe edit page.

    Displays the form to edit a recipe.
    This page can be accessed only by logged in users.

    Args:
        recipe_id: The recipe id to be edit
    """

    try:
        recipe = db.session.query(Recipe).filter_by(id=recipe_id).one()
    except:
        flash('Oopss! The recipe does not exists!', 'danger')
        return redirect(url_for('recipes.index'))

    form = RecipeForm(obj=recipe)

    if request.method == 'GET':
        pass
    elif recipe.user.id != current_user.id:
        flash('Oopss! You are not allowed to edit this recipe!', 'danger')
    elif form.validate_on_submit():
        recipe.user = current_user
        recipe.category = form.category.data
        recipe.title = form.title.data
        recipe.description = form.description.data
        recipe.ingredients = form.ingredients.data
        recipe.directions = form.directions.data
        recipe.prep_time = form.prep_time.data
        recipe.cook_time = form.cook_time.data
        recipe.servings = form.servings.data

        db.session.add(recipe)

        imagedata = form.image1.data
        if imagedata and imagedata.filename:
            filename = upload_recipe_image(imagedata)
            # save new entry into database
            image = RecipeImage(recipe, filename)
            db.session.add(image)

        # save database changes
        db.session.commit()
        flash('Hooray! Your recipe was successfully edited!', 'success')
        return redirect(
            url_for('recipes.view', recipe_id=recipe.id, slug=recipe.slug)
        )
    else:
        flash('Oopss! There are some issues to fix here...', 'danger')

    return render_template(
        'recipes/edit.html',
        form=form, recipe_id=recipe.id, images=recipe.images
    )
Exemple #2
0
def edit(recipe_id):
    """Recipe edit page.

    Displays the form to edit a recipe.
    This page can be accessed only by logged in users.

    Args:
        recipe_id: The recipe id to be edit
    """

    try:
        recipe = db.session.query(Recipe).filter_by(id=recipe_id).one()
    except:
        flash('Oopss! The recipe does not exists!', 'danger')
        return redirect(url_for('recipes.index'))

    form = RecipeForm(obj=recipe)

    if request.method == 'GET':
        pass
    elif recipe.user.id != current_user.id:
        flash('Oopss! You are not allowed to edit this recipe!', 'danger')
    elif form.validate_on_submit():
        recipe.user = current_user
        recipe.category = form.category.data
        recipe.title = form.title.data
        recipe.description = form.description.data
        recipe.ingredients = form.ingredients.data
        recipe.directions = form.directions.data
        recipe.prep_time = form.prep_time.data
        recipe.cook_time = form.cook_time.data
        recipe.servings = form.servings.data

        db.session.add(recipe)

        imagedata = form.image1.data
        if imagedata and imagedata.filename:
            filename = upload_recipe_image(imagedata)
            # save new entry into database
            image = RecipeImage(recipe, filename)
            db.session.add(image)

        # save database changes
        db.session.commit()
        flash('Hooray! Your recipe was successfully edited!', 'success')
        return redirect(
            url_for('recipes.view', recipe_id=recipe.id, slug=recipe.slug))
    else:
        flash('Oopss! There are some issues to fix here...', 'danger')

    return render_template('recipes/edit.html',
                           form=form,
                           recipe_id=recipe.id,
                           images=recipe.images)
Exemple #3
0
def new():
    """Add recipe page.

    Displays the form to add a new recipe.
    If the form is successfully submitted an success message is shown.
    This page can be accessed only by logged in users.
    """

    form = RecipeForm()

    if request.method == 'GET':
        pass
    elif form.validate_on_submit():
        recipe = Recipe(
            current_user, form.category.data, form.title.data,
            form.description.data, form.ingredients.data, form.directions.data,
            form.prep_time.data, form.cook_time.data, form.servings.data
        )
        db.session.add(recipe)

        for imagedata in [
            form.image1.data, form.image2.data, form.image3.data
        ]:
            if not imagedata.filename:
                continue
            filename = upload_recipe_image(imagedata)
            # save new entry into database
            image = RecipeImage(recipe, filename)
            db.session.add(image)

        # save database changes
        db.session.commit()
        flash('Your recipe was successfully added!', 'success')
        return redirect(
            url_for('recipes.view', recipe_id=recipe.id, slug=recipe.slug)
        )
    else:
        print form.errors.items()
        flash('Oopss! There are some issues to fix here...', 'danger')

    return render_template('recipes/new.html', form=form)
Exemple #4
0
def new():
    """Add recipe page.

    Displays the form to add a new recipe.
    If the form is successfully submitted an success message is shown.
    This page can be accessed only by logged in users.
    """

    form = RecipeForm()

    if request.method == 'GET':
        pass
    elif form.validate_on_submit():
        recipe = Recipe(current_user, form.category.data, form.title.data,
                        form.description.data, form.ingredients.data,
                        form.directions.data, form.prep_time.data,
                        form.cook_time.data, form.servings.data)
        db.session.add(recipe)

        for imagedata in [
                form.image1.data, form.image2.data, form.image3.data
        ]:
            if not imagedata.filename:
                continue
            filename = upload_recipe_image(imagedata)
            # save new entry into database
            image = RecipeImage(recipe, filename)
            db.session.add(image)

        # save database changes
        db.session.commit()
        flash('Your recipe was successfully added!', 'success')
        return redirect(
            url_for('recipes.view', recipe_id=recipe.id, slug=recipe.slug))
    else:
        print form.errors.items()
        flash('Oopss! There are some issues to fix here...', 'danger')

    return render_template('recipes/new.html', form=form)