portion option
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
2024-04-03 22:06:15 +02:00
parent 11469a2865
commit 204e2e4dd7
2 changed files with 49 additions and 16 deletions

View File

@ -6,6 +6,7 @@ import os
import json import json
import psycopg2 import psycopg2
import logging import logging
from math import ceil
app = Flask(__name__) app = Flask(__name__)
app.config.update({ app.config.update({
@ -23,28 +24,35 @@ app.logger.handlers = logging.getLogger('gunicorn.error').handlers
datapw = 'dasrtwegdffgtewrt4335wferg' datapw = 'dasrtwegdffgtewrt4335wferg'
def calculate_nutrition(food, weight): def calculate_nutrition(food, weight, portions):
try: try:
conn = psycopg2.connect() conn = psycopg2.connect(
host=os.environ.get('PGHOST', 'localhost'),
database=os.environ.get('PGDATABASE', 'csafdb'),
user=os.environ.get('PGUSER', 'user'),
password=os.environ.get('PGPASSWORD', 'password')
)
with conn.cursor() as cursor: with conn.cursor() as cursor:
# Abfrage der Nährwertdaten aus der Datenbank
cursor.execute('SELECT kcal, EW, Fett, KH, BST, CA FROM nutrition_table WHERE name = %s', (food,)) cursor.execute('SELECT kcal, EW, Fett, KH, BST, CA FROM nutrition_table WHERE name = %s', (food,))
result = cursor.fetchone() result = cursor.fetchone()
if result: if result:
# Runden und Berechnen der Nährwerte basierend auf dem Gewicht # Teilen des Gewichts durch die Anzahl der Portionen und Aufrunden
kcal, ew, fett, kh, bst, ca = result weight_per_portion = ceil(weight / portions)
nutrition_values = [
round(kcal * weight / 100), # kcal gerundet auf ganze Zahl kcal, ew, fett, kh, bst, ca = [value * weight_per_portion / 100 for value in result]
round(ew * weight / 100, 1), # EW gerundet auf eine Dezimalstelle return {
round(fett * weight / 100, 1), # Fett gerundet auf eine Dezimalstelle "weight_per_portion": weight_per_portion,
round(kh * weight / 100, 1), # KH gerundet auf eine Dezimalstelle "nutrition_values": {
round(bst * weight / 100, 1), # BST gerundet auf eine Dezimalstelle "kcal": round(kcal),
round(ca * weight / 100) # CA gerundet auf ganze Zahl "ew": round(ew, 1),
] "fett": round(fett, 1),
return nutrition_values "kh": round(kh, 1),
"bst": round(bst, 1),
"ca": round(ca)
}
}
else: else:
return None return None
finally: finally:

View File

@ -111,11 +111,34 @@
document.getElementById('submit-button').disabled = !(food && weight); document.getElementById('submit-button').disabled = !(food && weight);
} }
function addProduct() {
const foodInput = document.getElementById('my_combobox');
const weightInput = document.getElementById('weight');
const portionsInput = document.getElementById('portions');
const food = foodInput.value;
let weight = parseFloat(weightInput.value);
const portions = Math.max(parseInt(portionsInput.value, 10), 1); // Stellt sicher, dass mindestens 1 Portion angegeben wird
// Teilen des Gewichts durch die Anzahl der Portionen und Aufrunden
weight = Math.ceil(weight / portions);
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
.then(response => response.json())
.then(data => {
// Fügen Sie die verarbeitete Logik hier hinzu, wie zuvor
})
.catch(error => console.error('Fehler:', error));
}
function addProduct() { function addProduct() {
const foodInput = document.getElementById('my_combobox'); const foodInput = document.getElementById('my_combobox');
const weightInput = document.getElementById('weight'); const weightInput = document.getElementById('weight');
const food = foodInput.value; const food = foodInput.value;
const weight = weightInput.value; let weight = parseFloat(weightInput.value);
const portions = Math.max(parseInt(portionsInput.value, 10), 1); // Stellt sicher, dass mindestens 1 Portion angegeben wird
// Teilen des Gewichts durch die Anzahl der Portionen und Aufrunden
weight = Math.ceil(weight / portions);
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`) fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
@ -195,6 +218,8 @@ function updateTotalNutrition() {
</datalist> </datalist>
<input type="number" id="weight" name="weight" placeholder="Gramm" oninput="updateButtonState()"> <input type="number" id="weight" name="weight" placeholder="Gramm" oninput="updateButtonState()">
<button type="submit" id="submit-button" disabled>Hinzufügen</button> <button type="submit" id="submit-button" disabled>Hinzufügen</button>
<input type="number" id="portions" name="portions" placeholder="Portionen" min="1" value="1">
</form> </form>
<table id="nutrition-table"> <table id="nutrition-table">