From 20f9800203ee1a8d9f2b40444eb574ccddd48e47 Mon Sep 17 00:00:00 2001 From: janphilippweinsheimer Date: Thu, 7 Mar 2024 17:54:06 +0100 Subject: [PATCH] edit users --- app.py | 61 +++++++++++++++++++++++++++ db/SQLiteClient.py | 2 +- templates/components/habit_lists.html | 19 ++++++--- templates/users-edit.html | 37 ++++++++++++++++ 4 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 templates/users-edit.html diff --git a/app.py b/app.py index 77a800e..ae81a64 100644 --- a/app.py +++ b/app.py @@ -652,6 +652,67 @@ def users(): errors={}, ) +@app.route('/users-edit') +@login_required +def edit_users(): + habit_list_id = request.args.get('habit_list', current_user.get_habitLists()[0].id) + habit_list = HabitList.get(int(habit_list_id)) + users = habit_list.get_users() + + # Remove the current user from the list + users = [user for user in users if user.id != current_user.id] + + return render_template( + 'users-edit.html', + title='Teilnehmer bearbeiten', + habit_list=habit_list, + users=users, + errors={}, + ) + + +@app.route('/user-delete', methods=['POST']) +@login_required +def delete_user_from_list(): + habit_list_id = request.form.get('habit_list_id') + habit_list = HabitList.get(int(habit_list_id)) + + habit_user_id = request.form.get('habit_user_id') + habit_user = User.get(int(habit_user_id)) + + users = habit_list.get_users() + + # Remove the current user from the list + users = [user for user in users if user.id != current_user.id] + + # Check for errors + errors = {} + if not habit_list_id: + errors['habit_list'] = 'Die Habitliste ist erforderlich.' + if not habit_list_id: + errors['habit_user'] = 'Ein User ist erforderlich.' + + if errors: + return render_template( + 'users-edit.html', + title='Teilnehmer bearbeiten', + habit_list=habit_list, + users=users, + errors={}, + ) + + # delete user from habit list + id = int(habit_user_id) + habit_list.delete(id) + + return render_template( + 'users-edit.html', + title='Teilnehmer bearbeiten', + habit_list=habit_list, + users=users, + errors={}, + ) + @app.route('/users', methods=['POST']) @login_required diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index 2b6311a..e15c381 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -333,7 +333,7 @@ def add_user(list_id: int, user_id: int): def remove_user(list_id: int, user_id: int): - query = f"DELETE FROM habit_lists WHERE user_id = {user_id} AND list_id = {list_id};" + query = f"DELETE FROM habit_users WHERE user_id = {user_id} AND list_id = {list_id};" conn = con3() cursor = conn.cursor() cursor.execute(query) diff --git a/templates/components/habit_lists.html b/templates/components/habit_lists.html index b8c16c9..99c722c 100644 --- a/templates/components/habit_lists.html +++ b/templates/components/habit_lists.html @@ -65,14 +65,14 @@ id="simple-tabpanel-{{habit_list.id}}" role="tabpanel" aria-labelledby="simple-tab-{{habit_list.id}}"> -
+