diff --git a/app.py b/app.py index cbce558..746b62a 100644 --- a/app.py +++ b/app.py @@ -93,6 +93,10 @@ def signup_post(): if not password: errors['password'] = 'Das Passwort ist erforderlich.' + # Check if email is already in use + if User.get_by_email(email): + errors['email'] = 'E-Mail Adresse bereits in Benutzung.' + if errors: return render_template( 'auth/signup.html', @@ -135,10 +139,15 @@ def index(): for habit_list in habit_lists: habit_list.habits = sorted(habit_list.get_habits(), key=lambda habit: (not habit.checked, habit.slot)) + days = {"Monday": "Montag", "Tuesday": "Dienstag", "Wednesday": "Mittwoch", "Thursday": "Donnerstag", + "Friday": "Freitag", "Saturday": "Samstag", "Sunday": "Sonntag"} + + date = datetime.datetime.now().strftime("%d.%m.%Y %H:%M ") + days[datetime.datetime.now().strftime("%A")] + return render_template( 'index.html', title=name, - utc_dt=datetime.datetime.now().strftime("%d.%m.%Y %H:%M %A"), + utc_dt=date, habit_lists=habit_lists, heatmap_values=heatmap_values, errors={}, @@ -571,6 +580,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/auth/login.html b/templates/auth/login.html index 68edeca..8276503 100644 --- a/templates/auth/login.html +++ b/templates/auth/login.html @@ -1,8 +1,8 @@ {% extends 'layouts/main.html' %} {% block content %} -