From b6a9bf5520c2bac03fb5113f686fd0d136df9967 Mon Sep 17 00:00:00 2001 From: Verox001 Date: Fri, 1 Mar 2024 08:50:33 +0100 Subject: [PATCH] Implemented User-Invite Page --- app.py | 74 +++++++++++++++++++++++++++ templates/components/habit_lists.html | 7 ++- templates/users.html | 23 +++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 templates/users.html diff --git a/app.py b/app.py index d3839a1..7b3e91e 100644 --- a/app.py +++ b/app.py @@ -493,6 +493,80 @@ def reorder_habits(): return {} +@app.route('/users') +@login_required +def 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.html', + title='Teilnehmer', + habit_list=habit_list, + users=users, + errors={}, + ) + +@app.route('/users', methods=['POST']) +@login_required +def add_user(): + email = request.form.get('email') + habit_list_id = request.form.get('habit_list_id') + habit_list = HabitList.get(int(habit_list_id)) + + # Check for errors + errors = {} + if not email: + errors['email'] = 'Die E-Mail Adresse ist erforderlich.' + if not habit_list_id: + errors['habit_list'] = 'Die Habitliste ist erforderlich.' + + if errors: + return render_template( + 'users.html', + title='Teilnehmer', + email=email, + habit_list=habit_list, + errors=errors, + users=habit_list.get_users(), + ) + + # Check if user exists + user = User.get_by_email(email) + if not user: + errors['email'] = 'E-Mail Adresse nicht gefunden.' + + if user.id == current_user.id: + errors['email'] = 'Du kannst dich nicht selbst hinzufügen.' + + if errors: + return render_template( + 'users.html', + title='Teilnehmer', + email=email, + habit_list=habit_list, + errors=errors, + users=habit_list.get_users(), + ) + + # Add user to habit list + habit_list = HabitList.get(int(habit_list_id)) + habit_list.add_user(user) + + return render_template( + 'users.html', + title='Teilnehmer', + habit_list=habit_list, + users=habit_list.get_users(), + errors={}, + email=email, + ) + + # Run the application if __name__ == '__main__': app.run(port=5000, debug=True) diff --git a/templates/components/habit_lists.html b/templates/components/habit_lists.html index 3ae3bb8..06dcb9d 100644 --- a/templates/components/habit_lists.html +++ b/templates/components/habit_lists.html @@ -77,12 +77,11 @@
- +
diff --git a/templates/users.html b/templates/users.html new file mode 100644 index 0000000..e306157 --- /dev/null +++ b/templates/users.html @@ -0,0 +1,23 @@ +{% extends 'layouts/main.html' %} + +{% block content %} + +

{{ habit_list.name }}: {{ title }}

+

Lade Nutzer per ihrer E-Mail-Adresse ein

+ +
+
+ + +
+ {{ errors.get('email', '') }} +
+
+ + + + + +
+ +{% endblock %}