back
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
moerp 2024-04-04 12:25:51 +02:00
parent 888f8717b5
commit 3e6d743fd4

View File

@ -24,35 +24,28 @@ app.logger.handlers = logging.getLogger('gunicorn.error').handlers
datapw = 'dasrtwegdffgtewrt4335wferg' datapw = 'dasrtwegdffgtewrt4335wferg'
def calculate_nutrition(food, weight, portions): def calculate_nutrition(food, weight):
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:
# Teilen des Gewichts durch die Anzahl der Portionen und Aufrunden # Runden und Berechnen der Nährwerte basierend auf dem Gewicht
weight_per_portion = ceil(weight / portions) kcal, ew, fett, kh, bst, ca = result
nutrition_values = [
kcal, ew, fett, kh, bst, ca = [value * weight_per_portion / 100 for value in result] round(kcal * weight / 100), # kcal gerundet auf ganze Zahl
return { round(ew * weight / 100, 1), # EW gerundet auf eine Dezimalstelle
"weight_per_portion": weight_per_portion, round(fett * weight / 100, 1), # Fett gerundet auf eine Dezimalstelle
"nutrition_values": { round(kh * weight / 100, 1), # KH gerundet auf eine Dezimalstelle
"kcal": round(kcal), round(bst * weight / 100, 1), # BST gerundet auf eine Dezimalstelle
"ew": round(ew, 1), round(ca * weight / 100) # CA gerundet auf ganze Zahl
"fett": round(fett, 1), ]
"kh": round(kh, 1), return nutrition_values
"bst": round(bst, 1),
"ca": round(ca)
}
}
else: else:
return None return None
finally: finally: