diff --git a/kuhkal/routes.py b/kuhkal/routes.py index f3e90fd..94d319c 100644 --- a/kuhkal/routes.py +++ b/kuhkal/routes.py @@ -169,19 +169,49 @@ def edit_recipe_in_db(id): return redirect("/recipes") -@app.get("/calculate") -def calculator(): - data = {"recipes": Recipe.query.all()} - return render_template("pages/calculate.html", data=data) +@app.route("/calculate", methods=["GET", "POST"]) +def calculator_form(): + if request.method == "GET": + data = {"recipes": Recipe.query.all()} + return render_template("pages/calculate_form.html", data=data) + elif request.method == "POST": + ## breakpoint() + print("123") + #data = {"recipes": Recipe.query.all()} + req_data = request.form + print(req_data) + recipe_id = req_data["recipe_id"] + recipe = Recipe.query.get(recipe_id) + ingredients_inside = [] + ammount = req_data["ammount"] + for ingredient in recipe.ingredients: + ingr = Ingredient.query.get(ingredient.ingredient_id) + ingr_dict = { + "name": ingr.name, + "price": ingr.price, + "ammount": ingredient.ingredient_ammount, + "calculated_ammount": float(ingredient.ingredient_ammount) + * (int(ammount) / 10), + "calculated_price": float(ingr.price) * (int(ammount) / 10), + "id": ingr.id, + } + ingredients_inside.append(ingr_dict) + price = calculate_price(ingredients_inside) + data = {"ingredients": ingredients_inside, "price": price} + ##breakpoint() + return render_template("pages/calculate_results.html", data=data) -@app.post("/calculate") +#@app.post("/calculate") def calculate(): - req_data = request.get_json() - recipe_id = req_data["recipe_id"] + breakpoint() + req_data = request.form + ##recipe_id = req_data["recipe_id"] + recipe_id = int(req_data[0][1]) recipe = Recipe.query.get(recipe_id) ingredients_inside = [] - ammount = req_data["ammount"] + ##ammount = req_data["ammount"] + ammount = int(req_data[1][1]) for ingredient in recipe.ingredients: ingr = Ingredient.query.get(ingredient.ingredient_id) ingr_dict = { @@ -197,8 +227,8 @@ def calculate(): price = calculate_price(ingredients_inside) data = {"ingredients": ingredients_inside, "price": price} - return jsonify(data) - + #return jsonify(data) + return data def calculate_price(ingredients: list) -> float: price = 0 diff --git a/kuhkal/templates/includes/calculate_form.html b/kuhkal/templates/includes/calculate_form.html index bbf6b19..02c752a 100644 --- a/kuhkal/templates/includes/calculate_form.html +++ b/kuhkal/templates/includes/calculate_form.html @@ -1,11 +1,11 @@ -
+
- {% for recipe in data["recipes"] %} {% endfor %} - +
- +
diff --git a/kuhkal/templates/includes/calculate_results.html b/kuhkal/templates/includes/calculate_results.html index 1355a89..bc23302 100644 --- a/kuhkal/templates/includes/calculate_results.html +++ b/kuhkal/templates/includes/calculate_results.html @@ -6,13 +6,13 @@ Kolicina Cena - + {% endfor %} -

Cena: RSD

+

Cena: {data['price']}} RSD

diff --git a/kuhkal/templates/pages/calculate.html b/kuhkal/templates/pages/calculate.html index 8c09b5e..35627dc 100644 --- a/kuhkal/templates/pages/calculate.html +++ b/kuhkal/templates/pages/calculate.html @@ -1,24 +1,8 @@ {% extends "layouts/base.html" %} {% block content %} -
+
{% include "includes/calculate_form.html" %} {% include "includes/calculate_results.html" %} -
+
{% endblock content %} diff --git a/kuhkal/templates/pages/calculate_form.html b/kuhkal/templates/pages/calculate_form.html new file mode 100644 index 0000000..0c6fb29 --- /dev/null +++ b/kuhkal/templates/pages/calculate_form.html @@ -0,0 +1,5 @@ +{% extends "layouts/base.html" %} + +{% block content %} + {% include "includes/calculate_form.html" %} +{% endblock content %} diff --git a/kuhkal/templates/pages/calculate_results.html b/kuhkal/templates/pages/calculate_results.html new file mode 100644 index 0000000..6bf605e --- /dev/null +++ b/kuhkal/templates/pages/calculate_results.html @@ -0,0 +1,5 @@ +{% extends "layouts/base.html" %} + +{% block content %} + {% include "includes/calculate_results.html" %} +{% endblock content %}