diff --git a/app.py b/app.py index d3839a1..bc5b2cd 100644 --- a/app.py +++ b/app.py @@ -493,6 +493,83 @@ 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.' + + # 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 already: + errors['email'] = 'Teilnehmer ist bereits in der Liste.' + + 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 redirect(url_for('index', habit_list=habit_list.id)) + + # Run the application if __name__ == '__main__': app.run(port=5000, debug=True) diff --git a/models/HabitList.py b/models/HabitList.py index e20ac25..1346390 100644 --- a/models/HabitList.py +++ b/models/HabitList.py @@ -53,15 +53,14 @@ class HabitList: raw_users = get_users(self.id) users = [] for user in raw_users: - user = User(user[0], user[1], user[2], user[3]) + user = User(user[0], user[1], user[2], user[3], user[4]) users.append(user) return users # Adds a User by email to the HabitList - def add_user(self, email: str): - user = User.get_by_email(email) + def add_user(self, user: User): if user: add_user(self.id, user.id) else: diff --git a/templates/components/habit_lists.html b/templates/components/habit_lists.html index 3ae3bb8..892535c 100644 --- a/templates/components/habit_lists.html +++ b/templates/components/habit_lists.html @@ -66,24 +66,30 @@
-
+ {% if habit_list.get_users()[0].id == current_user.id %} + {% if habit_list.get_users()|length > 1 %} +
- - - - + {% for user in habit_list.get_users() %} + {% if current_user.id != user.id %} + + {% endif %} + {% endfor %}
+ {% endif %} - + {% else %} +
+ {% endif %}
diff --git a/templates/components/scripts.html b/templates/components/scripts.html index 161588d..c05ac07 100644 --- a/templates/components/scripts.html +++ b/templates/components/scripts.html @@ -1,81 +1,80 @@ - \ No newline at end of file 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 %}