diff --git a/app.py b/app.py index 13f51fe..2afc7ac 100644 --- a/app.py +++ b/app.py @@ -207,6 +207,10 @@ def habit_create(): except ValueError: errors['list_query'] = 'Die Anzahl muss eine Zahl sein.' + # Check if unit is day and times is one + if unit == 'Tag' and times != 1: + errors['times'] = 'Die Anzahl muss 1 sein, wenn das Habit täglich ist.' + if errors: return render_template( 'habit.html', @@ -312,26 +316,107 @@ 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 unit == 'Tag' and times != 1: + errors['times'] = 'Die Anzahl muss 1 sein, wenn das Habit täglich ist.' + + 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(): @@ -483,6 +568,24 @@ def delete_habit(): return {} +@app.route('/delete-list', methods=['POST']) +@login_required +def delete_list(): + list_id = request.get_json()["listId"] + + habit_list = HabitList.get(list_id) + + if habit_list is None: + return {"error": "List not found"} + + # Check if habit belongs to user + if current_user not in habit_list.get_users(): + return {"error": "List does not belong to user"} + + habit_list.delete(current_user.id) + return {} + + @app.route('/reorder', methods=['POST']) @login_required def reorder_habits(): @@ -549,15 +652,16 @@ def add_user(): if not user: errors['email'] = 'E-Mail Adresse nicht gefunden.' - if user.id == current_user.id: + if user and user.id == current_user.id: errors['email'] = 'Du kannst dich nicht selbst hinzufügen.' # Check if user is already in the habit list already = False - for u in habit_list.get_users(): - if u.id == user.id: - already = True - break + if user: + for u in habit_list.get_users(): + if u.id == user.id: + already = True + break if already: errors['email'] = 'Teilnehmer ist bereits in der Liste.' @@ -581,4 +685,4 @@ def add_user(): # Run the application if __name__ == '__main__': - app.run(port=5000, debug=True) + app.run(host="0.0.0.0", port=5000, debug=True) diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index d9b811c..efb9bb9 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -190,7 +190,7 @@ def update_slot(id: int, slot: int): def update_habit(id: int, name: str, note: str, times: int, unit: int): now = datetime.now().isoformat() - query = (f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' " + query = (f"UPDATE habits SET name = '{name}', note = '{note}', times = {times}, unit = {unit}, updated_at = '{now}' " f"WHERE id = {id};") conn = con3() cursor = conn.cursor() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..dc40e89 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pillow~=10.2.0 +flask~=3.0.0 +flask-login~=0.6.3 \ No newline at end of file diff --git a/templates/components/delete_list.html b/templates/components/delete_list.html new file mode 100644 index 0000000..a3fbddf --- /dev/null +++ b/templates/components/delete_list.html @@ -0,0 +1,20 @@ + +
\ No newline at end of file diff --git a/templates/components/habit_lists.html b/templates/components/habit_lists.html index a111a4a..56a4017 100644 --- a/templates/components/habit_lists.html +++ b/templates/components/habit_lists.html @@ -1,4 +1,4 @@ -