Esempio n. 1
0
def edit(id):
    product = select_row_from_table_by_id("products", id)
    images = []
    try:
        product = product[0]
    except IndexError:
        abort(404)

    form = ProductForm()
    if request.method == 'GET':
        form.prepopulate_data(product)
        images = get_product_images(id)

    if form.validate_on_submit():
        data = form.data.copy()
        data.pop("images", None)
        try:
            update("products", {"id": id}, data)
        except IntegrityError:
            form.name.errors = ('Product with given name already exists',)
            render_template('products/create.html', form=form)
        images = request.files.getlist("images")
        if images:
            for image in images:
                if image.filename:
                    save_image(image, product.id)
            return redirect(url_for('product.edit', id=id))

    return render_template('products/edit.html', form=form, images=images)
Esempio n. 2
0
def delete(id):
    product = select_row_from_table_by_id("products", id)
    if product is not None:
        run_custom_query("""SELECT delete_product({})""".format(id), fetch=False)
        flash(
            "Succesfully deleted product {}".format(product[0].name),
            "success"
        )
    else:
        flash("Product with ID={} does not exist".format(id), "danger")

    return redirect(url_for('product.list'))