def store_product():
    """Persists a new product entry in the database."""
    form = ProductForm(request.form)

    if not form.validate():
        return render_template('admin/edit-product.html', form=form)

    prod = Product.from_form(form)

    flash(message='Product created', category='success')

    url = url_for('frontend.product',
                  category_slug=prod.category.slug,
                  product_slug=prod.slug)
    return redirect(url)
def update_product(product_slug):
    """Saves changes to an existing product."""
    product = Product.by_slug(product_slug)
    form = ProductForm(request.form, product)

    if not form.validate():
        return render_template(
            'admin/edit-product.html', form=form, product=product)

    product = Product.from_form(form, product=product)

    flash(message='Product updated', category='success')

    url = url_for('frontend.product', category_slug=product.category.slug,
                  product_slug=product.slug)
    return redirect(url)