181 lines
5.0 KiB
Python
181 lines
5.0 KiB
Python
from flask import Flask, request, render_template, jsonify, redirect, url_for, g
|
|
import sqlite3
|
|
from flask_oidc import OpenIDConnect
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
import os
|
|
import json
|
|
|
|
app = Flask(__name__)
|
|
app.config.update({
|
|
'SECRET_KEY': os.environ['SECRET'],
|
|
'DEBUG': False,
|
|
'OIDC_CLIENT_SECRETS': json.loads(os.environ['CLIENT_SECRETS']),
|
|
'OIDC_ID_TOKEN_COOKIE_SECURE': False,
|
|
'OIDC_USER_INFO_ENABLED': True,
|
|
'OIDC_OPENID_REALM': 'hottis',
|
|
'OIDC_SCOPES': ['openid', 'email', 'profile']
|
|
})
|
|
|
|
oidc = OpenIDConnect(app)
|
|
|
|
|
|
# Datenbankverbindung konfigurieren
|
|
def get_db_connection():
|
|
conn = sqlite3.connect('nutrition.db') # 'nutrition.db' ist der Name der Datenbankdatei
|
|
conn.row_factory = sqlite3.Row # Ermöglicht den Zugriff auf Daten durch Spaltennamen
|
|
return conn
|
|
|
|
|
|
#def init_db():
|
|
# conn = get_db_connection()
|
|
# cursor = conn.cursor()
|
|
#
|
|
# # Erstellen der Tabelle
|
|
# cursor.execute('''
|
|
# CREATE TABLE IF NOT EXISTS nutrition_table (
|
|
# id INTEGER PRIMARY KEY,
|
|
# name TEXT NOT NULL,
|
|
# kcal REAL,
|
|
# EW REAL,
|
|
# Fett REAL,
|
|
# KH REAL,
|
|
# BST REAL,
|
|
# CA REAL
|
|
# )
|
|
# ''')
|
|
#
|
|
# # Testdaten einfügen
|
|
# test_data = [
|
|
# ('Apfel', 52, 0.3, 0.2, 14, 0.2, 6),
|
|
# ('Banane', 89, 1.1, 0.3, 23, 0.3, 5),
|
|
# ('Karotte', 41, 0.9, 0.2, 10, 0.2, 3),
|
|
# ('Tomate', 18, 0.9, 0.2, 3.9, 0.2, 4),
|
|
# ('Brokkoli', 34, 2.8, 0.4, 6.6, 0.4, 2),
|
|
# ('Spinat', 23, 2.9, 0.4, 3.6, 0.4, 99),
|
|
# ('Kartoffel', 77, 2, 0.1, 17, 0.1, 12),
|
|
# ('Huhn', 239, 27, 14, 0, 0, 2),
|
|
# ('Lachs', 208, 20, 13, 0, 0, 1),
|
|
# ('Ei', 155, 13, 11, 1.1, 1, 1)
|
|
# ]
|
|
#
|
|
# cursor.executemany('INSERT INTO nutrition_table (name, kcal, EW, Fett, KH, BST, CA) VALUES (?, ?, ?, ?, ?, ?, ?)', test_data)
|
|
#
|
|
# conn.commit()
|
|
# conn.close()
|
|
|
|
|
|
|
|
def calculate_nutrition(food, weight):
|
|
conn = get_db_connection()
|
|
cursor = conn.cursor()
|
|
|
|
# Abfrage der Nährwertdaten aus der Datenbank
|
|
cursor.execute('SELECT kcal, EW, Fett, KH, BST, CA FROM nutrition_table WHERE name = ?', (food,))
|
|
|
|
result = cursor.fetchone()
|
|
conn.close()
|
|
|
|
if result:
|
|
# Runden und Berechnen der Nährwerte basierend auf dem Gewicht
|
|
kcal, ew, fett, kh, bst, ca = result
|
|
nutrition_values = [
|
|
round(kcal * weight / 100), # kcal gerundet auf ganze Zahl
|
|
round(ew * weight / 100, 1), # EW gerundet auf eine Dezimalstelle
|
|
round(fett * weight / 100, 1), # Fett gerundet auf eine Dezimalstelle
|
|
round(kh * weight / 100, 1), # KH gerundet auf eine Dezimalstelle
|
|
round(bst * weight / 100, 1), # BST gerundet auf eine Dezimalstelle
|
|
round(ca * weight / 100) # CA gerundet auf ganze Zahl
|
|
]
|
|
return nutrition_values
|
|
else:
|
|
return None
|
|
|
|
|
|
# Index-Route
|
|
@app.route('/')
|
|
@oidc.require_login
|
|
@oidc.require_keycloak_role('user')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
# ...
|
|
|
|
@app.route('/get_products')
|
|
@oidc.require_login
|
|
@oidc.require_keycloak_role('user')
|
|
def get_products():
|
|
conn = get_db_connection()
|
|
cursor = conn.cursor()
|
|
cursor.execute('SELECT name FROM nutrition_table')
|
|
products = cursor.fetchall()
|
|
conn.close()
|
|
print("ter")
|
|
return {'products': [product[0] for product in products]}
|
|
|
|
|
|
|
|
|
|
|
|
# Route zum Hinzufügen und Berechnen von Lebensmitteln
|
|
@app.route('/add_lm', methods=['GET'])
|
|
@oidc.require_login
|
|
@oidc.require_keycloak_role('user')
|
|
def add_lm():
|
|
food = request.args.get('food')
|
|
weight = float(request.args.get('weight'))
|
|
|
|
nutrition = calculate_nutrition(food, weight)
|
|
if nutrition:
|
|
# Extrahieren der einzelnen Nährwerte
|
|
kcal, ew, fett, kh, bst, ca = nutrition
|
|
return jsonify({
|
|
"food": food,
|
|
"kcal": kcal,
|
|
"ew": ew,
|
|
"fett": fett,
|
|
"kh": kh,
|
|
"bst": bst,
|
|
"ca": ca
|
|
})
|
|
else:
|
|
return "Lebensmittel nicht gefunden.", 404
|
|
|
|
|
|
|
|
@app.route('/add_nutrition', methods=['POST'])
|
|
@oidc.accept_token(require_token=True, scopes_required=['openid'])
|
|
def add_nutrition():
|
|
food = request.form.get('food')
|
|
kcal = float(request.form.get('kcal'))
|
|
ew = float(request.form.get('ew'))
|
|
fett = float(request.form.get('fett'))
|
|
kh = float(request.form.get('kh'))
|
|
bst = float(request.form.get('bst'))
|
|
ca = float(request.form.get('ca'))
|
|
|
|
print("test")
|
|
# Verbindung zur Datenbank herstellen und Daten einfügen
|
|
conn = get_db_connection()
|
|
cursor = conn.cursor()
|
|
cursor.execute("INSERT INTO nutrition_table (name, kcal, ew, fett, kh, bst, ca) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
(food, kcal, ew, fett, kh, bst, ca))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
return redirect(url_for('nutrition'))
|
|
|
|
|
|
@app.route('/nutrition')
|
|
@oidc.require_login
|
|
@oidc.require_keycloak_role('user')
|
|
def nutrition():
|
|
return render_template('nutrition.html')
|
|
|
|
|
|
|
|
app = ProxyFix(app, x_for=1, x_host=1)
|
|
|
|
|
|
|
|
|