Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
e4fe5b9831
|
|||
6a9b938ab9
|
|||
367e76f9f1
|
|||
e59b184da5 | |||
c318cd21e0 | |||
224e6a9147 | |||
8f15a05a4e | |||
f0fcbc3c56
|
34
src/Run.py
34
src/Run.py
@ -5,6 +5,7 @@ from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
import os
|
||||
import json
|
||||
import psycopg2
|
||||
import logging
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.update({
|
||||
@ -18,6 +19,7 @@ app.config.update({
|
||||
})
|
||||
|
||||
oidc = OpenIDConnect(app)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
@ -105,8 +107,10 @@ def convert_decimal(value):
|
||||
|
||||
|
||||
@app.route('/add_nutrition', methods=['POST'])
|
||||
@oidc.accept_token(True)
|
||||
@oidc.accept_token(['openid'])
|
||||
def add_nutrition():
|
||||
logger.info("add_nutrition")
|
||||
|
||||
food = request.form.get('food')
|
||||
kcal = convert_decimal(request.form.get('kcal'))
|
||||
ew = convert_decimal(request.form.get('ew'))
|
||||
@ -132,6 +136,7 @@ def add_nutrition():
|
||||
@app.route('/nutrition')
|
||||
@oidc.require_login
|
||||
def nutrition():
|
||||
logger.info("nutrition")
|
||||
return render_template('nutrition.html')
|
||||
|
||||
@app.route('/get_token')
|
||||
@ -140,7 +145,34 @@ def get_token():
|
||||
return jsonify(token=oidc.get_access_token())
|
||||
|
||||
|
||||
@app.route('/get_database_entries')
|
||||
def get_database_entries():
|
||||
try:
|
||||
# Ersetzen Sie diese Werte mit Ihren Datenbank-Verbindungsinformationen
|
||||
conn = psycopg2.connect()
|
||||
cursor = conn.cursor()
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute("SELECT name, kcal, ew, fett, kh, bst, ca FROM nutrition_table")
|
||||
entries = cursor.fetchall()
|
||||
|
||||
# Umwandeln der Daten in ein JSON-freundliches Format
|
||||
entries_list = []
|
||||
for entry in entries:
|
||||
entries_list.append({
|
||||
"food": entry[0],
|
||||
"kcal": entry[1],
|
||||
"ew": entry[2],
|
||||
"fett": entry[3],
|
||||
"kh": entry[4],
|
||||
"bst": entry[5],
|
||||
"ca": entry[6]
|
||||
})
|
||||
return jsonify(entries_list)
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
app = ProxyFix(app, x_for=1, x_host=1)
|
||||
|
||||
|
@ -152,3 +152,65 @@ tr:hover:not(.selected) {
|
||||
overflow-x: auto; /* Ermöglicht horizontales Scrollen auf kleinen Bildschirmen */
|
||||
}
|
||||
}
|
||||
|
||||
#table-container {
|
||||
overflow-y: auto;
|
||||
max-height: 400px; /* Passen Sie die Höhe nach Bedarf an */
|
||||
border-radius: 10px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
#database-nutrition-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
#database-nutrition-table th, #database-nutrition-table td {
|
||||
border: 1px solid #ddd;
|
||||
padding: 8px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
#database-nutrition-table tr.selected {
|
||||
background-color: #f0e68c;
|
||||
}
|
||||
|
||||
|
||||
#password-prompt {
|
||||
margin-top: 10px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
#password-prompt input[type="password"] {
|
||||
padding: 8px;
|
||||
margin-right: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
#password-prompt button {
|
||||
padding: 8px 15px;
|
||||
margin-right: 10px;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
color: white;
|
||||
}
|
||||
|
||||
#password-prompt button[type="submit"] {
|
||||
background-color: #008C50; /* Helles Grün */
|
||||
}
|
||||
|
||||
#password-prompt button[type="submit"]:hover {
|
||||
background-color: #007344; /* Dunkleres Grün */
|
||||
}
|
||||
|
||||
#password-prompt button[type="button"] {
|
||||
background-color: #640000; /* Helles Rot */
|
||||
}
|
||||
|
||||
#password-prompt button[type="button"]:hover {
|
||||
background-color: #490000; /* Dunkleres Rot */
|
||||
}
|
||||
|
@ -55,6 +55,82 @@
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const table = document.getElementById('database-nutrition-table');
|
||||
table.addEventListener('click', function(e) {
|
||||
if (e.target.tagName === 'TD') {
|
||||
e.target.parentNode.classList.toggle('selected');
|
||||
updateDeleteButtonState();
|
||||
}
|
||||
});
|
||||
updateDeleteButtonState();
|
||||
});
|
||||
|
||||
function deleteSelectedRows() {
|
||||
const table = document.getElementById('database-nutrition-table');
|
||||
Array.from(table.rows).forEach(row => {
|
||||
if (row.classList.contains('selected')) {
|
||||
// Logik zum Löschen der Zeile aus der Datenbank
|
||||
table.deleteRow(row.rowIndex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function showPasswordPrompt() {
|
||||
document.getElementById('delete-row-button').style.display = 'none';
|
||||
document.getElementById('password-prompt').style.display = 'block';
|
||||
}
|
||||
|
||||
function hidePasswordPrompt() {
|
||||
document.getElementById('delete-row-button').style.display = 'block';
|
||||
document.getElementById('password-prompt').style.display = 'none';
|
||||
}
|
||||
|
||||
function deleteRowsIfPasswordCorrect() {
|
||||
const password = document.getElementById('password-input').value;
|
||||
if (password === 'wowmuchsecurity') {
|
||||
deleteSelectedRows(); // Funktion, die die ausgewählten Zeilen löscht
|
||||
hidePasswordPrompt();
|
||||
} else {
|
||||
alert('Falsches Passwort!');
|
||||
}
|
||||
}
|
||||
|
||||
function updateDeleteButtonState() {
|
||||
const selectedRows = document.querySelectorAll('#nutrition-table .selected').length;
|
||||
const deleteButton = document.getElementById('delete-row-button');
|
||||
deleteButton.disabled = selectedRows === 0;
|
||||
if (selectedRows === 0) {
|
||||
hidePasswordPrompt(); // Versteckt die Passwort-Eingabe, falls keine Zeile ausgewählt ist
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function loadDatabaseEntries() {
|
||||
fetch('/get_database_entries') // Pfad zur entsprechenden Flask-Route
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const tableBody = document.getElementById('database-nutrition-table').getElementsByTagName('tbody')[0];
|
||||
data.forEach(entry => {
|
||||
const row = tableBody.insertRow();
|
||||
row.insertCell(0).innerHTML = entry.food;
|
||||
row.insertCell(1).innerHTML = entry.kcal;
|
||||
row.insertCell(2).innerHTML = entry.ew;
|
||||
row.insertCell(3).innerHTML = entry.fett;
|
||||
row.insertCell(4).innerHTML = entry.kh;
|
||||
row.insertCell(5).innerHTML = entry.bst;
|
||||
row.insertCell(6).innerHTML = entry.ca;
|
||||
|
||||
// ... Fügen Sie weitere Zellen für die anderen Werte hinzu ...
|
||||
});
|
||||
})
|
||||
.catch(error => console.error('Fehler:', error));
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', loadDatabaseEntries);
|
||||
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
@ -90,6 +166,34 @@
|
||||
</table>
|
||||
<button type="submit" id="submit-button" disabled>Hinzufügen</button>
|
||||
</form>
|
||||
|
||||
<div id="table-container">
|
||||
<table id="database-nutrition-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Lebensmittel</th>
|
||||
<th>kcal</th>
|
||||
<th>EW</th>
|
||||
<th>Fett</th>
|
||||
<th>KH</th>
|
||||
<th>BST</th>
|
||||
<th>CA</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<!-- Die Zeilen werden dynamisch aus der Datenbank geladen -->
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<button id="delete-row-button" onclick="showPasswordPrompt()">Zeilen löschen</button>
|
||||
|
||||
<div id="password-prompt" style="display: none;">
|
||||
<input type="password" id="password-input" placeholder="Passwort">
|
||||
<button onclick="deleteRowsIfPasswordCorrect()">OK</button>
|
||||
<button onclick="hidePasswordPrompt()">Abbrechen</button>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user