Obrisan javascript

This commit is contained in:
rglst 2023-07-13 17:00:26 +02:00
parent 2ec9506216
commit d90825a818
6 changed files with 62 additions and 38 deletions

View File

@ -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

View File

@ -1,11 +1,11 @@
<form @submit.prevent method="POST">
<form action="/calculate" method="POST">
<div class="form-group">
<select class="form-control" x-model.number="recipe_id" name="recipe_id">
<select class="form-control" name="recipe_id">
{% for recipe in data["recipes"] %}
<option value="{{recipe.id}}">{{recipe.name}}</option>
{% endfor %}
</select>
<input @input.debounce.300="getResults()" x-model.number="ammount" type="number" step="1" name="ammount" placeholder="Koliko obroka">
<input type="number" step="1" name="ammount" placeholder="Koliko obroka">
</div>
<!--<button @click="getResults()" type="submit">Izracunaj</button> -->
<button type="submit">Izracunaj</button>
</form>

View File

@ -6,13 +6,13 @@
<th>Kolicina</th>
<th>Cena</th>
</thead>
<template x-for="ingredient in results['ingredients']">
{% for ingredient in data['ingredients'] %}
<tr>
<td x-text="ingredient['name']"></td>
<td x-text="ingredient['calculated_ammount']"></td>
<td x-text="ingredient['calculated_price'] + ' RSD'"></td>
<td>{{ingredient['name']}}</td>
<td>{{ingredient['calculated_ammount']}}</td>
<td>{{ingredient['calculated_price']}} RSD</td>
</tr>
</template>
{% endfor %}
</table>
<p>Cena: <b x-text="results['price']"></b> RSD</p>
<p>Cena: <b> {data['price']}}</b> RSD</p>
</div>

View File

@ -1,24 +1,8 @@
{% extends "layouts/base.html" %}
{% block content %}
<div x-data="{ results: {},
recipe_id: 1,
ammount:40,
async getResults() {
this.results = await (await fetch('https://solidarna.cyberdeck.xyz/calculate', {
method:'POST',
body: JSON.stringify({
recipe_id: this.recipe_id,
ammount: this.ammount
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})).json();
console.log(this.results);
}
} ">
<div>
{% include "includes/calculate_form.html" %}
{% include "includes/calculate_results.html" %}
</div>
</div>
{% endblock content %}

View File

@ -0,0 +1,5 @@
{% extends "layouts/base.html" %}
{% block content %}
{% include "includes/calculate_form.html" %}
{% endblock content %}

View File

@ -0,0 +1,5 @@
{% extends "layouts/base.html" %}
{% block content %}
{% include "includes/calculate_results.html" %}
{% endblock content %}