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