示例#1
0
def api_product_list():
    if request.method == "POST":
        err = ""
        product_id = request.args.get("product_id")
        product = None
        if product_id:
            product = dao.read_product_id(product_id=int(product_id))

        if request.method.lower() == "post":
            if product_id:  # Cap nhat
                data = dict(request.form.copy())
                data["product_id"] = product_id
                if dao.update_product(**data):
                    return redirect(url_for("product_list"))
            else:  # Them

                import json
                product = dao.add_product(**dict(json.loads(request.data)))
                if product:
                    return jsonify(product)

            err = "Something wrong!!! Please back later!"

        return jsonify({"error_message": err})

    kw = request.args.get("keyword")

    return jsonify(dao.read_products(keyword=kw))
示例#2
0
def add_or_update_product():

    err = ""
    product_id = request.args.get("product_id")
    product = None
    if product_id:
        product = dao.read_product_by_id(product_id=int(product_id))

    if request.method.lower() == "post":
        # name = request.form.get("name")
        # price = request.form.get("price", 0)
        # images = request.form.get("images")
        # description = request.form.get("description")
        # category_id = request.form.get("category_id", 0)
        # import pdb
        # pdb.set_trace()
        if product_id:  # Cap nhat
            data = dict(request.form.copy())
            data["product_id"] = product_id
            if dao.update_product(**data):
                return redirect(url_for("product_list"))
        else:  # Them
            if dao.add_product(**dict(request.form)):
                return redirect(url_for("product_list"))

        err = "Something wrong!!! Please back later!"

    return render_template("product-add.html",
                           categories=dao.read_categories(),
                           product=product,
                           err=err)
示例#3
0
def add_or_update_products():
    err = ""
    if request.method.lower() == "post":
        name = request.form.get("name")
        price = request.form.get("price", 0)
        images = request.form.get("images")
        description = request.form.get("description")
        category_id = request.form.get("category_id", 0)
        if (dao.add_product(name=name,
                            price=price,
                            images=images,
                            description=description,
                            category_id=category_id)):
            return redirect(url_for("list_products"))

        err = "Something wrong !!! Please back later!!!"

    return render_template("product-add.html",
                           categories=dao.read_categories(),
                           err=err)
示例#4
0
def add_or_update_product():
    err = ""
    product_id = request.args.get("product_id")
    product = None
    if product_id:
        product = dao.read_product_by_id(product_id=int(product_id))

    if request.method.lower() == "post":
        if product_id:
            data = dict(request.form.copy())
            data["product_id"] = product_id
            if dao.update_product(**data):
                return redirect(url_for("product_list"))
        else:
            if dao.add_product(**dict(request.form)):
                return redirect(url_for("product_list"))

        err = "Something wrong!!! Please back later!"
    return render_template("product-add.html",
                           categories=dao.read_categories(),
                           product=product,
                           err=err)