oidc added
This commit is contained in:
95
src/Run.py
95
src/Run.py
@ -1,11 +1,24 @@
|
||||
from flask import Flask, request, render_template, jsonify, redirect, url_for
|
||||
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': "fdsgffdgretfsdgfsf"
|
||||
'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
|
||||
@ -13,42 +26,42 @@ def get_db_connection():
|
||||
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 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()
|
||||
|
||||
|
||||
|
||||
@ -80,12 +93,14 @@ def calculate_nutrition(food, weight):
|
||||
|
||||
# Index-Route
|
||||
@app.route('/')
|
||||
@oidc.require_login
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
# ...
|
||||
|
||||
@app.route('/get_products')
|
||||
@oidc.require_login
|
||||
def get_products():
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
@ -95,13 +110,13 @@ def get_products():
|
||||
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
|
||||
def add_lm():
|
||||
food = request.args.get('food')
|
||||
weight = float(request.args.get('weight'))
|
||||
@ -125,6 +140,7 @@ def add_lm():
|
||||
|
||||
|
||||
@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'))
|
||||
@ -147,6 +163,7 @@ def add_nutrition():
|
||||
|
||||
|
||||
@app.route('/nutrition')
|
||||
@oidc.require_login
|
||||
def nutrition():
|
||||
return render_template('nutrition.html')
|
||||
|
||||
|
Reference in New Issue
Block a user