diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index 9c7d21b..390c94d 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -45,7 +45,8 @@ def get_user_by_email(email: str): def update_user(id: int, name: str, email: str, password: str = None): now = datetime.now().isoformat() if password: - query = f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' WHERE id = {id};" + query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', 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() @@ -57,12 +58,10 @@ def update_user(id: int, name: str, email: str, password: str = None): def delete_user(id: int): - query = f"DELETE FROM habit_lists WHERE (SELECT list_id FROM habit_users WHERE user_id = {id}) = id;" - query2 = f"DELETE FROM users WHERE id = {id};" + query = f"DELETE FROM users WHERE id = {id};" conn = con3() cursor = conn.cursor() cursor.execute(query) - cursor.execute(query2) conn.commit() conn.close() return cursor.lastrowid @@ -71,8 +70,8 @@ def delete_user(id: int): ### Habit.py ### def create_habit(list_id: int, name: str, times: int, unit: int, slot: int, note: str | None=None): now = datetime.now().isoformat() - query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) VALUES ('{list_id}', " - f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');") + query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) " + f"VALUES ('{list_id}', '{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');") conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -168,7 +167,8 @@ def update_slot(id: int, slot: int): def update_habit(id: int, name: str, note: str, times: int, unit: int): now = datetime.now().isoformat() - query = f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' WHERE id = {id};" + query = (f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' " + f"WHERE id = {id};") conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -178,10 +178,12 @@ def update_habit(id: int, name: str, note: str, times: int, unit: int): def delete_habit(id: int): - query = f"DELETE FROM habits WHERE id = {id};" + query = f"DELETE FROM habit_trackings WHERE id = habit_id;" + query2 = f"DELETE FROM habits WHERE id = {id};" conn = con3() cursor = conn.cursor() cursor.execute(query) + cursor.execute(query2) conn.commit() conn.close() @@ -264,6 +266,37 @@ def get_habitLists(user_id: int): return habit_lists +def get_users(list_id: int): + query = (f"SELECT users.* FROM users JOIN habit_users ON users.id = habit_users.user_id WHERE " + f"habit_users.list_id = {list_id};") + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + users = cursor.fetchall() + conn.close() + return users + + +def add_user(list_id: int, user_id: int): + now = datetime.now().isoformat() + query = (f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at)" + f" VALUES ('{user_id}', '{list_id}', '{now}', '{now}');") + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + conn.commit() + conn.close() + + +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};" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + conn.commit() + conn.close() + + def update_habitList(id: int, name: str, description: str): now = datetime.now().isoformat() query = f"UPDATE habit_lists SET name = {name}, description = {description}, updated_at = '{now}' WHERE id = {id};" @@ -284,16 +317,6 @@ 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 9c26954..35faf65 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -3,11 +3,11 @@ from dataclasses import dataclass 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_habitList +from db.SQLiteClient import (create_habit, get_habit, update_habit, delete_habit, get_next_slot, get_slots, update_slot, + get_habitTrackings_by_habit_id, get_habitList) -# Unit wird als Integers wie folgt gemessen: +# Unit wird als Integer wie folgt gemessen: # 0: Tag # 1: Woche (Default) # 2: Monat @@ -36,9 +36,7 @@ class Habit: @staticmethod def get(id: int): habit = get_habit(id) - habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None - - return habit + return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None def update(self, name: str=None, note: str=None, times: int=None, unit: int=None): update_habit(self.id, name, note, times, unit) @@ -69,13 +67,17 @@ class Habit: update_slot(slot[0], slot[1] - 1) delete_habit(self.id) - def get_habitTrackings(self) -> list[HabitTrackings]: + def get_habitTrackings(self) -> list: trackings = [] for rawTracking in get_habitTrackings_by_habit_id(self.id): - trackings.append(HabitTrackings(rawTracking[0], rawTracking[1], - datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f"))) + trackings.append(HabitTrackings(rawTracking[0], rawTracking[1])) return trackings + 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]) if raw_habitLists else None + def fill_statistics(self): count = 0 self.checked = False @@ -104,11 +106,3 @@ 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 d59e265..64fa292 100644 --- a/models/HabitList.py +++ b/models/HabitList.py @@ -1,9 +1,10 @@ from dataclasses import dataclass -from datetime import date, datetime +from datetime import datetime -from db.SQLiteClient import delete_habitList, create_habitList, get_habitList, get_habits, get_users from models.Habit import Habit from models.User import User +from db.SQLiteClient import (delete_habitList, create_habitList, get_habitList, get_habits, + get_users, add_user, remove_user) @dataclass @@ -11,24 +12,19 @@ class HabitList: id: int name: str description: str - created_at: date - updated_at: date habits: list = None @staticmethod def create(user_id: int, name: str, description: str): id = create_habitList(user_id, name, description) - return HabitList(id, name, description, datetime.now(), datetime.now()) + return HabitList(id, name, description) @staticmethod def get(id: int): habitList = get_habitList(id) return HabitList(habitList[0], habitList[1], habitList[2], datetime.strptime(habitList[3], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(habitList[4], "%Y-%m-%dT%H:%M:%S.%f")) if habitList else None - def delete(self): - delete_habitList(self.id) - - def get_habits(self): + def get_habits(self) -> list: raw_habits = get_habits(self.id) habits = [] for habit in raw_habits: @@ -37,7 +33,7 @@ class HabitList: return habits - def get_users(self): + def get_users(self) -> list: raw_users = get_users(self.id) users = [] for user in raw_users: @@ -45,3 +41,14 @@ class HabitList: users.append(user) return users + + def add_user(self, email: str): + user = User.get_by_email(email) + add_user(self.id, user.id) + + # The id of the current user is necessary + def delete(self, user_id): + if len(get_users) > 1: + remove_user(self.id, user_id) + else: + delete_habitList(self.id) diff --git a/models/HabitTrackings.py b/models/HabitTrackings.py index 9ad30f1..996aed6 100644 --- a/models/HabitTrackings.py +++ b/models/HabitTrackings.py @@ -1,6 +1,4 @@ from dataclasses import dataclass -from datetime import date, datetime - from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings @@ -8,17 +6,16 @@ from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_ha class HabitTrackings: id: int habit_id: int - created_at: date @staticmethod - def create(habit_id: int, times: int): + def create(habit_id: int): id = create_habitTrackings(habit_id) - return HabitTrackings(id, habit_id, datetime.now()) + return HabitTrackings(id, habit_id) @staticmethod def get(id: int): habitTrackings = get_habitTrackings(id) - return HabitTrackings(habitTrackings[0], habitTrackings[1], datetime.strptime(habitTrackings[2], "%Y-%m-%dT%H:%M:%S.%f")) if habitTrackings else None + return HabitTrackings(habitTrackings[0], habitTrackings[1]) if habitTrackings else None def delete(self): delete_habitTrackings(self.id) diff --git a/models/User.py b/models/User.py index 13f9f72..e058fc3 100644 --- a/models/User.py +++ b/models/User.py @@ -1,8 +1,8 @@ from datetime import datetime from flask_login import UserMixin -from db.SQLiteClient import create_user, get_user, get_user_by_email, delete_user, update_user, \ - get_habitLists, get_heatmap_value +from db.SQLiteClient import (create_user, get_user, get_user_by_email, update_user, delete_user, + get_habitLists, get_heatmap_value) class User(UserMixin): @@ -31,9 +31,12 @@ class User(UserMixin): update_user(self.id, self.name, self.email, self.password if self.password else None) def delete(self): + habitLists = self.get_habitLists() + for habitList in habitLists: + habitList.delete(self.id) delete_user(self.id) - def get_habitLists(self): + def get_habitLists(self) -> list: from models.HabitList import HabitList raw_habitLists = get_habitLists(self.id) @@ -44,7 +47,7 @@ class User(UserMixin): return habitLists - def get_heatmap(self): + def get_heatmap(self) -> list: heatmap = [] for day in range (0, 27): value = get_heatmap_value(self.id, day)