Example #1
0
    def create():
        product_form = ProductForm(request.form)
        categories_products = CategoryProduct()

        view_data = ProductController.__get_view_data()
        product_form = view_data["product_form"]
        categories_list = view_data["categories"]

        if not product_form.validate():
            flash("Tuotteen julkaisu epäonnistui", "error")
            return render("users/product_form.html",
                          product_form = product_form,
                          categories = categories_list)

        product = Product(
            product_form.name.data,
            product_form.price.data,
            product_form.quantity.data,
            current_user.id
        )

        if not product.save("product create:"):
            flash("Tuotteen julkaisu epäonnistui", "error")
            return render("users/product_form.html",
                          product_form = product_form,
                          categories = categories_list)

        if not categories_products.add_product_categories(product.id, product_form.categories.data):
            flash("Tuote julkaistu. Kategorioiden lisäyksessä tapahtui virhe", "error")
            return render("users/product_form.html",
                          product_form = product_form,
                          categories = categories_list)

        flash("Tuote julkaistu", "success")
        return redirect(url_for("product_list"))
Example #2
0
def create_product(product: Product):
    return product.save()