diff --git a/app.py b/app.py index 5419869..a305689 100644 --- a/app.py +++ b/app.py @@ -6,7 +6,7 @@ from flask_login import login_required, LoginManager, login_user, logout_user, c from models.Habit import Habit from models.HabitList import HabitList -from models.HabitTrackings import HabitTrackings +from models.HabitTracking import HabitTrackings from models.User import User from utils import anonymous_required diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index d557bc2..0173b9e 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -178,18 +178,16 @@ 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() -### HabitTrackings.py ### -def create_habitTrackings(habit_id: int): +### HabitTracking ### +def create_habitTracking(habit_id: int): now = datetime.now().isoformat() query = f"INSERT INTO habit_trackings (habit_id, created_at) VALUES ('{habit_id}','{now}');" conn = con3() @@ -200,7 +198,7 @@ def create_habitTrackings(habit_id: int): return cursor.lastrowid -def get_habitTrackings(id: int): +def get_habitTracking(id: int): query = f"SELECT * FROM habit_trackings WHERE id = {id};" conn = con3() cursor = conn.cursor() @@ -210,7 +208,7 @@ def get_habitTrackings(id: int): return habit_tracking -def get_habitTrackings_by_habit_id(habit_id: int): +def get_habitTrackings(habit_id: int): query = f"SELECT * FROM habit_trackings WHERE habit_id = {habit_id};" conn = con3() cursor = conn.cursor() @@ -220,7 +218,7 @@ def get_habitTrackings_by_habit_id(habit_id: int): return habit_trackings -def delete_habitTrackings(id: int): +def delete_habitTracking(id: int): query = f"DELETE FROM habit_trackings WHERE id = {id};" conn = con3() cursor = conn.cursor() @@ -229,7 +227,7 @@ def delete_habitTrackings(id: int): conn.close() -### HabitList.py ### +### HabitList ### def create_habitList(user_id: int, name: str, description: str): now = datetime.now().isoformat() query = (f"INSERT INTO habit_lists (name, description, created_at, updated_at) " diff --git a/models/Habit.py b/models/Habit.py index 24836e5..cf39fce 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -2,9 +2,9 @@ import json from dataclasses import dataclass from datetime import datetime -from models.HabitTrackings import HabitTrackings +from models.HabitTracking import HabitTracking 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) + get_habitTrackings, get_habitList) # unit will be represented by integers like this: @@ -39,10 +39,12 @@ class Habit: return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None + # Updates: name, note, times, unit def update(self): update_habit(self.id, self.name, self.note, self.times, self.unit) + # Updates the slot and reorders the HabitList accordingly def update_slot(self, new_slot: int): # Fetches a list with the following structure [(id, slot), (id, slot), ...] slots = get_slots(self.list_id) @@ -61,29 +63,39 @@ class Habit: update_slot(self.id, new_slot) + # Deletes the Habit def delete(self): - # Deletes the current habit + # Reorders the slots slots = get_slots(self.list_id)[self.slot+1:] for slot in slots: update_slot(slot[0], slot[1] - 1) - # Deletes the current habit + # Deletes all track-records associated with the Habit + trackings = self.get_habitTrackings() + for tracking in trackings: + tracking.delete() + + # Deletes the current Habit delete_habit(self.id) + # Returns all track-records for a Habit def get_habitTrackings(self) -> list: trackings = [] - for rawTracking in get_habitTrackings_by_habit_id(self.id): - trackings.append(HabitTrackings(rawTracking[0], rawTracking[1])) + for rawTracking in get_habitTrackings(self.id): + trackings.append(HabitTracking(rawTracking[0], rawTracking[1], + datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f"))) return trackings + # Returns the HabitList in which the Habit is located def habit_list(self) -> list: 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 + # Saves the progress of the Habit in the attribute percentage def fill_statistics(self): count = 0 self.checked = False @@ -111,5 +123,6 @@ class Habit: self.percentage = int(count / self.times * 100) + # Converts the Habit data to a json format def to_json(self): return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) diff --git a/models/HabitList.py b/models/HabitList.py index bf3525c..bdff3f5 100644 --- a/models/HabitList.py +++ b/models/HabitList.py @@ -2,8 +2,8 @@ from dataclasses import dataclass 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) +from db.SQLiteClient import (create_habitList, get_habitList, get_habits, get_users, add_user, remove_user, + update_habitList, delete_habitList) @dataclass @@ -11,7 +11,7 @@ class HabitList: id: int name: str description: str - habits: list = None + habits: list = None #? unclear usage @staticmethod def create(user_id: int, name: str, description: str): @@ -23,6 +23,21 @@ class HabitList: habitList = get_habitList(id) return HabitList(habitList[0], habitList[1], habitList[2]) if habitList else None + + # Updates: name, description + def update(self): + update_habitList(self.id, self.name, self.description) + + + # Deletes the HabitList | The id of the current user is necessary + def delete(self, user_id): + if len(get_users) > 1: + self.remove_user(user_id) + else: + delete_habitList(self.id) + + + # Returns the Habits connected with the HabitList def get_habits(self) -> list: raw_habits = get_habits(self.id) habits = [] @@ -32,6 +47,8 @@ class HabitList: return habits + + # Returns the Users connected with the HabitList def get_users(self) -> list: raw_users = get_users(self.id) users = [] @@ -41,13 +58,13 @@ class HabitList: return users + + # Adds a User by email to the HabitList 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) + + # Removes a User from the HabitList + def remove_user(self, user_id): + remove_user(self.id, user_id) diff --git a/models/HabitTracking.py b/models/HabitTracking.py new file mode 100644 index 0000000..76ee855 --- /dev/null +++ b/models/HabitTracking.py @@ -0,0 +1,26 @@ +from datetime import date, datetime +from dataclasses import dataclass +from db.SQLiteClient import create_habitTracking, get_habitTracking, delete_habitTracking + + +@dataclass +class HabitTracking: + id: int + habit_id: int + created_at: date + + @staticmethod + def create(habit_id: int): + id = create_habitTracking(habit_id) + return HabitTracking(id, habit_id, datetime.now()) + + @staticmethod + def get(id: int): + habitTrackings = get_habitTracking(id) + return HabitTracking(habitTrackings[0], habitTrackings[1], + datetime.strptime(habitTrackings[2], "%Y-%m-%dT%H:%M:%S.%f")) \ + if habitTrackings else None + + # Deletes the HabitTracking + def delete(self): + delete_habitTracking(self.id) diff --git a/models/HabitTrackings.py b/models/HabitTrackings.py deleted file mode 100644 index 996aed6..0000000 --- a/models/HabitTrackings.py +++ /dev/null @@ -1,21 +0,0 @@ -from dataclasses import dataclass -from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings - - -@dataclass -class HabitTrackings: - id: int - habit_id: int - - @staticmethod - def create(habit_id: int): - id = create_habitTrackings(habit_id) - return HabitTrackings(id, habit_id) - - @staticmethod - def get(id: int): - habitTrackings = get_habitTrackings(id) - 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 423c6b8..05a897b 100644 --- a/models/User.py +++ b/models/User.py @@ -26,10 +26,12 @@ class User(UserMixin): return User(user[0], user[1], user[2], user[3]) 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) + # Deletes the User def delete(self): # calls the deletion of the users habitLists habitLists = self.get_habitLists() @@ -40,7 +42,7 @@ class User(UserMixin): delete_user(self.id) - # returns all habitLists assigned with the user + # Returns all HabitLists connected with the user def get_habitLists(self) -> list: from models.HabitList import HabitList @@ -53,7 +55,7 @@ class User(UserMixin): return habitLists - # return the heatmap-values of the last 28 days + # Returns all heatmap-values from the last 28 days def get_heatmap(self) -> list: heatmap = [] for day in range (0, 28):