diff --git a/app.py b/app.py index 77a800e..8ddb275 100644 --- a/app.py +++ b/app.py @@ -132,11 +132,13 @@ def index(): habit_lists = current_user.get_habitLists() name = "Hallo " + current_user.name heatmap_values, day = current_user.get_heatmap() + heatmap_color = current_user.heatmap_color else: habit_lists = [] name = "Bitte melde dich an." heatmap_values = [] day = None + heatmap_color = None # Sort habits by whether they have been checked today and then by slot for habit_list in habit_lists: @@ -146,7 +148,6 @@ def index(): "Friday": "Freitag", "Saturday": "Samstag", "Sunday": "Sonntag"} date = datetime.datetime.now().strftime("%d.%m.%Y %H:%M ") + days[datetime.datetime.now().strftime("%A")] - color = '255, 0, 255' return render_template( 'index.html', @@ -155,7 +156,7 @@ def index(): habit_lists=habit_lists, heatmap_values=heatmap_values, day=day, - color=color, + color=heatmap_color, errors={} ) @@ -513,7 +514,8 @@ def save_profile_image(image_file): # Save the processed image image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename.replace(".gif", ".jpg")) image.save(image_path, 'JPEG', quality=100) - current_user.update_profile_image(image_path) + current_user.profile_image = image_path + current_user.update() def save_profile_animated(image_file, filename): @@ -522,7 +524,8 @@ def save_profile_animated(image_file, filename): gif_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) image_file.save(gif_path) - current_user.update_profile_image(gif_path) + current_user.profile_image = gif_path + current_user.update() @app.route('/upload', methods=['POST']) @@ -652,6 +655,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 c5bfc33..768342d 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -27,11 +27,11 @@ def create_user_profile_image(user_id): return relative_destination_path -def create_user(name: str, email: str, password: str, profile_image: str = None): +def create_user(name: str, email: str, password: str, heatmap_color: str, profile_image: str = None): password = hashlib.sha256(password.encode()).hexdigest() now = datetime.now().isoformat() - query = (f"INSERT INTO users (name, email, password, profile_image, created_at, updated_at) VALUES " - f"('{name}', '{email}', '{password}', '{profile_image}', '{now}', '{now}');") + query = (f"INSERT INTO users (name, email, password, profile_image, heatmap_color, created_at, updated_at) VALUES " + f"('{name}', '{email}', '{password}', '{profile_image}', '{heatmap_color}', '{now}', '{now}');") conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -66,24 +66,15 @@ def get_user_by_email(email: str): return user -def update_user(id: int, name: str, email: str, password: str): +def update_user(id: int, name: str, email: str, password: str, profile_image: str, heatmap_color: str): now = datetime.now().isoformat() if password: - query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' " + query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', " + f"profile_image ='{profile_image}', heatmap_color = '{heatmap_color}', updated_at = '{now}' " f"WHERE id = {id};") else: - query = f"UPDATE users SET name = '{name}', email = '{email}', updated_at = '{now}' WHERE id = {id};" - conn = con3() - cursor = conn.cursor() - cursor.execute(query) - conn.commit() - conn.close() - return cursor.lastrowid - - -def update_user_profile(id: int, profile_image: str): - now = datetime.now().isoformat() - query = f"UPDATE users SET profile_image = '{profile_image}', updated_at = '{now}' WHERE id = {id};" + query = (f"UPDATE users SET name = '{name}', email = '{email}', profile_image ='{profile_image}', " + f"heatmap_color = '{heatmap_color}', updated_at = '{now}' WHERE id = {id};") conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -342,7 +333,7 @@ def accept_List(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/db/migrations/1709825341_delete_users_table.sql b/db/migrations/1709825341_delete_users_table.sql new file mode 100644 index 0000000..441087a --- /dev/null +++ b/db/migrations/1709825341_delete_users_table.sql @@ -0,0 +1 @@ +DROP TABLE users; \ No newline at end of file diff --git a/db/migrations/1709825360_create_users_table.sql b/db/migrations/1709825360_create_users_table.sql new file mode 100644 index 0000000..96b3f94 --- /dev/null +++ b/db/migrations/1709825360_create_users_table.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS users +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + email TEXT NOT NULL, + password TEXT NOT NULL, + profile_image TEXT NOT NULL, + heatmap_color TEXT NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL +); \ No newline at end of file diff --git a/models/HabitList.py b/models/HabitList.py index a77f3a1..0770075 100644 --- a/models/HabitList.py +++ b/models/HabitList.py @@ -53,7 +53,7 @@ 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[4]) + user = User(user[0], user[1], user[2], user[3], user[4], user[5]) users.append(user) return users diff --git a/models/User.py b/models/User.py index 8f8f03f..22a2e3d 100644 --- a/models/User.py +++ b/models/User.py @@ -2,40 +2,39 @@ from datetime import datetime from flask_login import UserMixin from db.SQLiteClient import (create_user, get_user, get_user_by_email, update_user, delete_user, - get_habitLists, get_heatmap_value, update_user_profile) + get_habitLists, get_heatmap_value) class User(UserMixin): - def __init__(self, id: int, name: str, email: str, password: str = None, profile_image:str = None): + def __init__(self, id: int, name: str, email: str, password: str=None, profile_image:str=None, heatmap_color: str=None): self.id = id self.name = name self.email = email self.password = password self.profile_image = profile_image + self.heatmap_color = heatmap_color @staticmethod def create(name: str, email: str, password: str): - id, profile_image = create_user(name, email, password) - return User(id=id, name=name, email=email, profile_image=profile_image) + heatmap_color = "0, 255, 0" + id, profile_image = create_user(name, email, password, heatmap_color) + return User(id=id, name=name, email=email, profile_image=profile_image, heatmap_color=heatmap_color) @staticmethod def get(id: int): user = get_user(id) - return User(user[0], user[1], user[2], user[3], user[4]) if user else None + return User(user[0], user[1], user[2], user[3], user[4], user[5]) if user else None @staticmethod def get_by_email(email: str): user = get_user_by_email(email) - return User(user[0], user[1], user[2], user[3], user[4]) if user else None + return User(user[0], user[1], user[2], user[3], user[4], user[5]) if user else None # Updates: name, email, password def update(self): - update_user(self.id, self.name, self.email, self.password if self.password else None) - - def update_profile_image(self, profile_image: str): - update_user_profile(self.id, profile_image) + update_user(self.id, self.name, self.email, self.password if self.password else None, self.profile_image, self.heatmap_color) # Deletes the User def delete(self): diff --git a/static/script/script-index.js b/static/script/script-index.js index 4f2f16a..5277927 100644 --- a/static/script/script-index.js +++ b/static/script/script-index.js @@ -1,6 +1,6 @@ // Funktion zum Erstellen der Heatmap -function createHeatmap(data, day, color) { +function createHeatmap(data, day) { const heatmapContainer = document.getElementById('heatmap'); const days = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'] @@ -18,16 +18,14 @@ function createHeatmap(data, day, color) { // console.log(i * 7 + j, data[i * 7 + j], Math.max(...data)); const opacity = data[i * 7 + j] / (Math.max(...data) <= 0 ? 1 : Math.max(...data)); // Berechne die Opazität basierend auf Aktivitätsanzahl - if (data[i * 7 + j]) { - const dayElement = document.createElement('div'); - dayElement.classList.add('day'); - dayElement.style.backgroundColor = `rgba(${color}, ${opacity})`; - if (day === i * 7 + j){ - dayElement.style.borderColor = `rgba(255, 0, 0)`; - dayElement.style.borderWidth = "2px"; - } - heatmapContainer.appendChild(dayElement); + const dayElement = document.createElement('div'); + dayElement.classList.add('day'); + dayElement.style.backgroundColor = `rgba(${color}, ${opacity})`; + if (day === i * 7 + j){ + dayElement.style.borderColor = `rgba(255, 0, 0)`; + dayElement.style.borderWidth = "2px"; } + heatmapContainer.appendChild(dayElement); } } } @@ -165,6 +163,7 @@ $(function () { $('[data-toggle="tooltip"]').tooltip() }) +console.log(activityData, day, color) // Erstelle die Heatmap mit den simulierten Daten createHeatmap(activityData, day, color); 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}}"> -
+
{{ habit_list.description }}
{% endif %} -
- + + + + {% if habit_list.get_users()|length > 1 %} + + + + {% endif %}
{% else %}
{% endif %} -
+
+ diff --git a/templates/components/heatmap.html b/templates/components/heatmap.html index 90f16c3..a4010dc 100644 --- a/templates/components/heatmap.html +++ b/templates/components/heatmap.html @@ -11,4 +11,5 @@ const activityData = {{ heatmap_values }}; const day = {{ day }}; const color = "{{ color }}"; + console.log(activityData, day, color) \ No newline at end of file diff --git a/templates/users-edit.html b/templates/users-edit.html new file mode 100644 index 0000000..b466b4c --- /dev/null +++ b/templates/users-edit.html @@ -0,0 +1,37 @@ +{% extends 'index.html' %} + +{% block content %} +
+

+ {{ title }} +

+ + + {% for user in users %} +
+
+ +
+
+ {{ user.name }} +
+
+ {{ user.email }} +
+
+ + + +
+
+ + {% endfor %} + +
+{% endblock %} \ No newline at end of file