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,15 +169,17 @@ def edit_recipe_in_db(id):
return redirect("/recipes") return redirect("/recipes")
@app.get("/calculate") @app.route("/calculate", methods=["GET", "POST"])
def calculator(): def calculator_form():
if request.method == "GET":
data = {"recipes": Recipe.query.all()} data = {"recipes": Recipe.query.all()}
return render_template("pages/calculate.html", data=data) return render_template("pages/calculate_form.html", data=data)
elif request.method == "POST":
## breakpoint()
@app.post("/calculate") print("123")
def calculate(): #data = {"recipes": Recipe.query.all()}
req_data = request.get_json() req_data = request.form
print(req_data)
recipe_id = req_data["recipe_id"] recipe_id = req_data["recipe_id"]
recipe = Recipe.query.get(recipe_id) recipe = Recipe.query.get(recipe_id)
ingredients_inside = [] ingredients_inside = []
@ -196,9 +198,37 @@ def calculate():
ingredients_inside.append(ingr_dict) ingredients_inside.append(ingr_dict)
price = calculate_price(ingredients_inside) price = calculate_price(ingredients_inside)
data = {"ingredients": ingredients_inside, "price": price} data = {"ingredients": ingredients_inside, "price": price}
##breakpoint()
return render_template("pages/calculate_results.html", data=data)
return jsonify(data)
#@app.post("/calculate")
def calculate():
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 = int(req_data[1][1])
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}
#return jsonify(data)
return data
def calculate_price(ingredients: list) -> float: def calculate_price(ingredients: list) -> float:
price = 0 price = 0

View File

@ -1,11 +1,11 @@
<form @submit.prevent method="POST"> <form action="/calculate" method="POST">
<div class="form-group"> <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"] %} {% for recipe in data["recipes"] %}
<option value="{{recipe.id}}">{{recipe.name}}</option> <option value="{{recipe.id}}">{{recipe.name}}</option>
{% endfor %} {% endfor %}
</select> </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> </div>
<!--<button @click="getResults()" type="submit">Izracunaj</button> --> <button type="submit">Izracunaj</button>
</form> </form>

View File

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

View File

@ -1,23 +1,7 @@
{% extends "layouts/base.html" %} {% extends "layouts/base.html" %}
{% block content %} {% block content %}
<div x-data="{ results: {}, <div>
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);
}
} ">
{% include "includes/calculate_form.html" %} {% include "includes/calculate_form.html" %}
{% include "includes/calculate_results.html" %} {% include "includes/calculate_results.html" %}
</div> </div>

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 %}