From f7967c2dfd8ec74783d55a3ed5e0675db3dbd886 Mon Sep 17 00:00:00 2001 From: nikolaswollenberg Date: Fri, 1 Mar 2024 09:25:27 +0100 Subject: [PATCH] many --- app.py | 100 +++++++++++++++++++++++++++++++++----- templates/edit-habit.html | 79 +++++++++++++++++++++++++++++- 2 files changed, 166 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index d3839a1..cbce558 100644 --- a/app.py +++ b/app.py @@ -303,26 +303,104 @@ def profile_change(): @app.route('/edit-habit') @login_required def edit_habit(): - habit_id = request.args.get("habit") - #habit_id = request.get_json()["editHabitId"] - + habit_id = int(request.args.get("habit")) habit = Habit.get(habit_id) + + + units = ["Tag", "Woche", "Monat", "Jahr"] + return render_template( "edit-habit.html", - title=habit.name + title=habit.name, + habit=habit.id, + name=habit.name, + note=habit.note, + times=habit.times, + unit=units[habit.unit], + errors={} ) -''' + @app.route('/edit-habit', methods=['POST']) @login_required def edit_habit_change(): - #habit_id = request.get_json()["habitId"] - #habit = Habit.get(habit_id) - return render_template( - "edit-habit.html" - ) -''' + units = ["Tag", "Woche", "Monat", "Jahr"] + name = request.form.get('name') + note = request.form.get('note') + times = request.form.get('times') + unit = request.form.get('unit') + list_id = request.form.get('habit') + + habit = Habit.get(list_id) + + # Check for errors + errors = {} + if not name: + errors['name'] = 'Der Name ist erforderlich.' + if not times: + errors['times'] = 'Die Anzahl ist erforderlich.' + if not note: + note = '' + if not unit: + errors['unit'] = 'Die Einheit ist erforderlich.' + if not list_id: + errors['habit'] = 'Das Habit ist erforderlich.' + + # Check if times is an integer + try: + print(times) + times = int(times) + + + # Check that times is greater than 0 + if times <= 0: + errors['times'] = 'Die Anzahl muss größer als 0 sein.' + except ValueError: + errors['times'] = 'Die Anzahl muss eine Zahl sein.' + + # Check that unit is valid + if unit not in ['Tag', 'Woche', 'Monat', 'Jahr']: + errors['unit'] = 'Die Einheit ist ungültig.' + + # check if list_id is an int + try: + list_id = int(list_id) + except ValueError: + errors['list_query'] = 'Die Anzahl muss eine Zahl sein.' + + if errors: + return render_template( + "edit-habit.html", + title=habit.name, + habit=habit.id, + name=habit.name, + note=habit.note, + times=habit.times, + unit=units[habit.unit], + errors={} + ) + + # Map unit to integer + if unit == 'Tag': + unit = 0 + elif unit == 'Woche': + unit = 1 + elif unit == 'Monat': + unit = 2 + elif unit == 'Jahr': + unit = 3 + else: + unit = 1 + + # Save habit to database + print(name, note, times, unit) + habit.name, habit.note, habit.times, habit.unit = name, note, times, unit + + habit.update() + # Back to index + return redirect(url_for('index')) + @app.route('/check_password', methods=['POST']) @login_required def check_password(): diff --git a/templates/edit-habit.html b/templates/edit-habit.html index fbe5b9e..ccf95ac 100644 --- a/templates/edit-habit.html +++ b/templates/edit-habit.html @@ -2,6 +2,81 @@ {% block content %} -test -{{ title }} +

Habit Bearbeiten📋

+ +
+
+ + +
+ {{ errors.get('name', '') }} +
+
+
+ + +
+ {{ errors.get('note', '') }} +
+
+ +
+
+ + +
+ {{ errors.get('times', '') }} +
+
+
+ + + +
+ {{ errors.get('unit', '') }} +
+
+
+ + +
+ {{ errors.get('list_query', '') }} +
+ + +
+ {% endblock %} \ No newline at end of file