diff --git a/app.py b/app.py index 2808b81..ef87529 100644 --- a/app.py +++ b/app.py @@ -128,7 +128,8 @@ def index(): name = "Bitte melde dich an." # Sort habits by whether they have been checked today and then by slot - # habits.sort(key=lambda habit: (habit.checked, habit.slot)) + for habit_list in habit_lists: + habit_list.habits = sorted(habit_list.get_habits(), key=lambda habit: (not habit.checked, habit.slot)) return render_template( 'index.html', @@ -324,7 +325,8 @@ def check_habit(): return {"error": "Habit not found"} # Check if habit belongs to user - if habit.user_id != current_user.id: + users = habit.habit_list().get_users() + if current_user not in users: return {"error": "Habit does not belong to user"} trackings = habit.get_habitTrackings() @@ -360,7 +362,7 @@ def delete_habit(): return {"error": "Habit not found"} # Check if habit belongs to user - if habit.user_id != current_user.id: + if current_user not in habit.habit_list().get_users(): return {"error": "Habit does not belong to user"} habit.delete() @@ -377,7 +379,8 @@ def reorder_habits(): return {"error": "Habit not found"} # Check if habit belongs to user - if habit.user_id != current_user.id: + users = habit.habit_list().get_users() + if current_user not in users: return {"error": "Habit does not belong to user"} habit.update_slot(new_index) diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index dec84f5..893b265 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -295,6 +295,16 @@ def delete_habitList(id: int): conn.close() +def get_users(list_id: int): + query = f"SELECT users.* FROM users JOIN habit_users ON users.id = habit_users.user_id WHERE habit_users.list_id = {list_id};" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + users = cursor.fetchall() + conn.close() + return users + + if __name__ == "__main__": habits = get_habits(1) for habit in habits: diff --git a/models/Habit.py b/models/Habit.py index 27d4f55..0ac0f15 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -4,7 +4,7 @@ from datetime import datetime from models.HabitTrackings import HabitTrackings from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \ - get_habitTrackings_by_habit_id, get_slots, update_habit + get_habitTrackings_by_habit_id, get_slots, update_habit, get_habitList, get_habitLists # Unit wird als Integers wie folgt gemessen: @@ -30,6 +30,7 @@ class Habit: @staticmethod def create(list_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1): slot = get_next_slot(list_id) + print(slot) id = create_habit(list_id, name, times, unit, slot, note) return Habit(id, list_id, name, note, times, unit, slot) @@ -115,3 +116,9 @@ class Habit: def to_json(self): return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) + + def habit_list(self): + from models.HabitList import HabitList + raw_habitLists = get_habitList(self.list_id) + + return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2], datetime.strptime(raw_habitLists[3], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(raw_habitLists[4], "%Y-%m-%dT%H:%M:%S.%f")) if raw_habitLists else None diff --git a/models/HabitList.py b/models/HabitList.py index d97b27d..ac888d3 100644 --- a/models/HabitList.py +++ b/models/HabitList.py @@ -2,8 +2,9 @@ from dataclasses import dataclass from datetime import date, datetime from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings, create_habitList, \ - get_habitList, get_habits + get_habitList, get_habits, get_users from models.Habit import Habit +from models.User import User @dataclass @@ -13,6 +14,7 @@ class HabitList: description: str created_at: date updated_at: date + habits: list = None @staticmethod def create(user_id: int, name: str, description: str): @@ -35,3 +37,12 @@ class HabitList: habits.append(habit) return habits + + def get_users(self): + raw_users = get_users(self.id) + users = [] + for user in raw_users: + user = User(user[0], user[1], user[2], user[3]) + users.append(user) + + return users diff --git a/models/User.py b/models/User.py index 95c3933..9657f96 100644 --- a/models/User.py +++ b/models/User.py @@ -3,8 +3,6 @@ from datetime import datetime from flask_login import UserMixin from db.SQLiteClient import create_user, get_user, get_user_by_email, get_habits, delete_user, update_user, \ get_habitLists -from models.Habit import Habit -from models.HabitList import HabitList class User(UserMixin): @@ -44,6 +42,8 @@ class User(UserMixin): # return habits def get_habitLists(self): + from models.HabitList import HabitList + raw_habitLists = get_habitLists(self.id) habitLists = [] for habitList in raw_habitLists: diff --git a/templates/index.html b/templates/index.html index 70f3859..793795f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -95,7 +95,7 @@