Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
e66e54c424 | |||
0ad4c9e398 | |||
ce86d35b25 | |||
19552bf774 | |||
b74f136f81 | |||
b52dbaec57 | |||
2769d0a33a | |||
5ac1a9f5a9 | |||
ab3378ec4d |
27
src/Run.py
27
src/Run.py
@ -6,7 +6,7 @@ import os
|
||||
import json
|
||||
import psycopg2
|
||||
import logging
|
||||
from math import ceil
|
||||
from math import ceil, floor
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.update({
|
||||
@ -38,12 +38,12 @@ def calculate_nutrition(food, weight):
|
||||
# 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
|
||||
schulrunden(kcal * weight / 100), # kcal gerundet auf ganze Zahl
|
||||
schulrunden(ew * weight / 100, 1), # EW gerundet auf eine Dezimalstelle
|
||||
schulrunden(fett * weight / 100, 1), # Fett gerundet auf eine Dezimalstelle
|
||||
schulrunden(kh * weight / 100, 1), # KH gerundet auf eine Dezimalstelle
|
||||
schulrunden(bst * weight / 100, 1), # BST gerundet auf eine Dezimalstelle
|
||||
schulrunden(ca * weight / 100) # CA gerundet auf ganze Zahl
|
||||
]
|
||||
return nutrition_values
|
||||
else:
|
||||
@ -54,6 +54,19 @@ def calculate_nutrition(food, weight):
|
||||
|
||||
|
||||
|
||||
|
||||
def schulrunden(zahl, stellen=0):
|
||||
faktor = 10 ** stellen
|
||||
zahl = zahl * faktor
|
||||
basis = floor(zahl)
|
||||
if zahl - basis >= 0.5:
|
||||
gerundet = basis + 1
|
||||
else:
|
||||
gerundet = basis
|
||||
return gerundet / faktor
|
||||
|
||||
|
||||
|
||||
# Index-Route
|
||||
@app.route('/')
|
||||
@oidc.require_login
|
||||
|
@ -9,7 +9,9 @@
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
cleanUpLocalStorage();
|
||||
restorePortions()
|
||||
restoreTableFromLocalStorage();
|
||||
|
||||
updateTotalNutrition();
|
||||
fetch('/get_products')
|
||||
.then(response => response.json())
|
||||
@ -32,6 +34,22 @@
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById('portions').addEventListener('input', function() {
|
||||
const portions = this.value;
|
||||
localStorage.setItem('savedPortions', portions); // Speichern der Portionen im localStorage
|
||||
recalculateTableBasedOnPortions(); // Bestehende Funktion, um die Tabelle zu aktualisieren
|
||||
});
|
||||
|
||||
|
||||
function restorePortions() {
|
||||
const savedPortions = localStorage.getItem('savedPortions');
|
||||
if (savedPortions) {
|
||||
document.getElementById('portions').value = savedPortions;
|
||||
recalculateTableBasedOnPortions(); // Falls notwendig, um die Tabelle direkt zu aktualisieren
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function recalculateTableBasedOnPortions() {
|
||||
const portions = parseInt(document.getElementById('portions').value, 10) || 1;
|
||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||
|
@ -7,6 +7,8 @@
|
||||
<link rel="shortcut icon" href="../static/images/favicon.ico">
|
||||
|
||||
<script>
|
||||
let isEditing = false;
|
||||
|
||||
function updateSubmitButtonState() {
|
||||
const inputs = document.querySelectorAll('#nutrition-form input');
|
||||
const allFilled = Array.from(inputs).every(input => input.value.trim() !== '');
|
||||
@ -57,15 +59,95 @@
|
||||
}
|
||||
|
||||
|
||||
function showPasswordPrompt(action) {
|
||||
// Setze isEditing je nach Aktion
|
||||
isEditing = (action === 'edit');
|
||||
if (isEditing) {
|
||||
// Deaktiviere Zeilenauswahl
|
||||
document.getElementById('database-nutrition-table').removeEventListener('click', handleRowClick);
|
||||
} else {
|
||||
// Aktiviere Zeilenauswahl
|
||||
document.getElementById('database-nutrition-table').addEventListener('click', handleRowClick);
|
||||
}
|
||||
document.getElementById('delete-row-button').style.display = action === 'edit' ? 'none' : 'block';
|
||||
document.getElementById('edit-row-button').style.display = action === 'delete' ? 'none' : 'block';
|
||||
document.getElementById('password-prompt').style.display = 'block';
|
||||
document.getElementById('password-prompt').setAttribute('data-action', action);
|
||||
// Deselektiere alle Zeilen
|
||||
deselectAllRows();
|
||||
}
|
||||
|
||||
function deselectAllRows() {
|
||||
document.querySelectorAll('#database-nutrition-table .selected').forEach(row => {
|
||||
row.classList.remove('selected');
|
||||
});
|
||||
updateDeleteButtonState();
|
||||
updateEditButtonState();
|
||||
}
|
||||
|
||||
function hidePasswordPrompt() {
|
||||
document.getElementById('delete-row-button').style.display = 'block';
|
||||
document.getElementById('edit-row-button').style.display = 'block';
|
||||
document.getElementById('password-prompt').style.display = 'none';
|
||||
// Setze isEditing zurück
|
||||
isEditing = false;
|
||||
// Deaktiviere Editierbarkeit aller Zellen und aktiviere Zeilenauswahl
|
||||
setCellsEditable(false);
|
||||
document.getElementById('database-nutrition-table').addEventListener('click', handleRowClick);
|
||||
}
|
||||
|
||||
function performActionIfPasswordCorrect() {
|
||||
const password = document.getElementById('password-input').value;
|
||||
if (password === 'geheim') {
|
||||
const action = document.getElementById('password-prompt').getAttribute('data-action');
|
||||
if (action === 'delete') {
|
||||
deleteSelectedRows();
|
||||
} else if (action === 'edit') {
|
||||
// Mache Zellen editierbar
|
||||
setCellsEditable(true);
|
||||
hidePasswordPrompt();
|
||||
}
|
||||
} else {
|
||||
alert('Falsches Passwort!');
|
||||
}
|
||||
}
|
||||
|
||||
function setCellsEditable(editable) {
|
||||
document.querySelectorAll('#database-nutrition-table td').forEach(cell => {
|
||||
cell.contentEditable = editable;
|
||||
});
|
||||
}
|
||||
|
||||
function handleRowClick(event) {
|
||||
if (event.target.tagName === 'TD' && !isEditing) {
|
||||
event.target.parentNode.classList.toggle('selected');
|
||||
updateDeleteButtonState();
|
||||
updateEditButtonState();
|
||||
}
|
||||
}
|
||||
|
||||
function updateEditButtonState() {
|
||||
// Diese Funktion soll ähnlich wie updateDeleteButtonState() implementiert werden,
|
||||
// um den "Bearbeiten"-Button zu aktivieren/deaktivieren
|
||||
const selectedRows = document.querySelectorAll('#database-nutrition-table .selected').length;
|
||||
const editButton = document.getElementById('edit-row-button');
|
||||
editButton.disabled = selectedRows === 0;
|
||||
}
|
||||
|
||||
|
||||
// Ergänze Event-Listener, um den Zustand des Bearbeiten-Buttons zu aktualisieren
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
document.getElementById('database-nutrition-table').addEventListener('click', handleRowClick);
|
||||
const table = document.getElementById('database-nutrition-table');
|
||||
table.addEventListener('click', function(e) {
|
||||
if (e.target.tagName === 'TD') {
|
||||
e.target.parentNode.classList.toggle('selected');
|
||||
updateDeleteButtonState();
|
||||
updateEditButtonState();
|
||||
}
|
||||
});
|
||||
updateDeleteButtonState();
|
||||
updateEditButtonState();
|
||||
});
|
||||
|
||||
function deleteSelectedRows() {
|
||||
@ -98,15 +180,15 @@
|
||||
}
|
||||
|
||||
|
||||
function showPasswordPrompt() {
|
||||
document.getElementById('delete-row-button').style.display = 'none';
|
||||
document.getElementById('password-prompt').style.display = 'block';
|
||||
}
|
||||
// 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 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;
|
||||
@ -236,11 +318,12 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<button id="delete-row-button" onclick="showPasswordPrompt()">Zeilen löschen</button>
|
||||
|
||||
<button id="delete-row-button" onclick="showPasswordPrompt('delete')">Zeilen löschen</button>
|
||||
<button id="edit-row-button" onclick="showPasswordPrompt('edit')">Bearbeiten</button>
|
||||
|
||||
<div id="password-prompt" style="display: none;">
|
||||
<input type="password" id="password-input" placeholder="Passwort">
|
||||
<button id="ok-button" onclick="deleteRowsIfPasswordCorrect()">OK</button>
|
||||
<button id="ok-button" onclick="performActionIfPasswordCorrect()">OK</button>
|
||||
<button id="cancel-button" onclick="hidePasswordPrompt()">Abbrechen</button>
|
||||
</div>
|
||||
|
||||
|
Reference in New Issue
Block a user