Exemple #1
0
    def delete(uid: str) -> Soundfile:
        """
		Delete and return soundfile.
		"""
        soundfile = SoundfileStore.read(uid)
        delete_file(soundfile.filename)
        return super(SoundfileStore, SoundfileStore).delete(soundfile)
Exemple #2
0
def delete_recipe(recipe_id):
    """
    Renders the recipe deletion form.
    :param recipe_id: ID of the recipe to be deleted.
    :return: The rendered pge.
    """

    recipe = Item.query.get_or_404(recipe_id)
    form = DeleteRecipeForm()

    if recipe.user_id != current_user.id:
        flask.flash(
            'You do not have permission to delete this recipe.', 'error')
        return flask.redirect(
            flask.url_for('recipe_detail', recipe_id=recipe_id))

    # Form-WTF implements CSRF using the Flask SECRET_KEY
    if form.validate_on_submit():
        models.delete_file(recipe.photo)
        db.session.delete(recipe)
        db.session.commit()

        flask.flash('Recipe {} was deleted.'.format(recipe.name))
        return flask.redirect(flask.url_for('index'))

    return flask.render_template(
        'form_delete_recipe.html', form=form, recipe=recipe, stats=get_stats())
Exemple #3
0
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())
Exemple #4
0
def delete_photo(recipe_id):
    """
    Deletes the photo from a recipe. User must have proper rights in order
    to complete the operation.
    :param recipe_id:
    :return: Redirects back to cuisine editing page or 404 if method not POST
    """

    if flask.request.method != 'POST':
        flask.abort(404)

    recipe = Item.query.get_or_404(recipe_id)

    if recipe.user_id != current_user.id:
        flask.flash(
            'You do not have permission to delete this recipe photo.', 'error')

        return flask.redirect(flask.url_for('recipe_detail', recipe_id=recipe_id))

    models.delete_file(recipe.photo)
    recipe.photo = None
    db.session.commit()

    return flask.redirect(flask.url_for('edit_recipe', recipe_id=recipe.id))
Exemple #5
0
 def tearDownClass(cls):
     models.delete_file(TEST_SQLITE_DB_FILE)