diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index 211cddc..9c7d21b 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -102,7 +102,6 @@ def get_habits(list_id: int): def get_heatmap_value(user_id: int, days: int): - # Berechnet das Datum, ab dem die Habits gezählt werden sollen date = (datetime.now() - timedelta(days=days)).date() print(date) @@ -121,23 +120,15 @@ def get_heatmap_value(user_id: int, days: int): conn = con3() cursor = conn.cursor() - - # Execute the queries cursor.execute(query) all_habits = cursor.fetchall() cursor.execute(query2) checked_habits = cursor.fetchall() - - # Close the database connection - count = len(all_habits) - print(count) - count2 = len(checked_habits) - print(count2) - - # Close the database connection conn.close() # Calculate the percentage of checked Habits + count = len(all_habits) + count2 = len(checked_habits) if count > 0: return int(count2 / count * 100) else: @@ -198,8 +189,7 @@ def delete_habit(id: int): ### HabitTrackings.py ### def create_habitTrackings(habit_id: int): now = datetime.now().isoformat() - query = ( - f"INSERT INTO habit_trackings (habit_id, created_at) VALUES ('{habit_id}','{now}');") + query = f"INSERT INTO habit_trackings (habit_id, created_at) VALUES ('{habit_id}','{now}');" conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -240,16 +230,14 @@ def delete_habitTrackings(id: int): ### HabitList.py ### 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) VALUES ('{name}', '{description}', '{now}', '{now}');") - + query = (f"INSERT INTO habit_lists (name, description, created_at, updated_at) " + f"VALUES ('{name}', '{description}', '{now}', '{now}');") conn = con3() cursor = conn.cursor() cursor.execute(query) - - query1 = f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at) VALUES ('{user_id}', '{cursor.lastrowid}', '{now}', '{now}');" - - cursor.execute(query1) + query2 = (f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at)" + f" VALUES ('{user_id}', '{cursor.lastrowid}', '{now}', '{now}');") + cursor.execute(query2) conn.commit() conn.close() return cursor.lastrowid @@ -266,7 +254,8 @@ def get_habitList(id: int): def get_habitLists(user_id: int): - query = f"SELECT habit_lists.* FROM habit_lists JOIN habit_users ON habit_lists.id = habit_users.list_id WHERE habit_users.user_id = {user_id};" + query = (f"SELECT habit_lists.* FROM habit_lists JOIN habit_users ON habit_lists.id = habit_users.list_id " + f"WHERE habit_users.user_id = {user_id};") conn = con3() cursor = conn.cursor() cursor.execute(query) diff --git a/models/Habit.py b/models/Habit.py index f524854..9e0144a 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_habitList, get_habitLists + get_habitTrackings_by_habit_id, get_slots, update_habit, get_habitList # Unit wird als Integers wie folgt gemessen: @@ -40,7 +40,6 @@ class Habit: 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) if name is not None: @@ -52,7 +51,6 @@ class Habit: if unit is not None: self.unit = unit - def update_slot(self, new_slot: int): slots = get_slots(self.list_id) if new_slot > self.slot: @@ -65,58 +63,48 @@ class Habit: update_slot(slot[0], slot[1]+1) update_slot(self.id, new_slot) - def delete(self): slots = get_slots(self.list_id)[self.slot+1:] for slot in slots: update_slot(slot[0], slot[1] - 1) delete_habit(self.id) - def get_habitTrackings(self) -> list[HabitTrackings]: 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], + datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f"))) return trackings - def fill_statistics(self): count = 0 - self.checked = False for tracking in self.get_habitTrackings(): - if tracking.created_at == datetime.today(): - self.checked = True - # day if self.unit == 0: - if tracking.created_at == datetime.today(): - # self.checked = True + if tracking.created_at.date() == datetime.today().date(): count += 1 # week elif self.unit == 1: if tracking.created_at.isocalendar()[1] == datetime.today().isocalendar()[1]: - # self.checked = True count += 1 # month elif self.unit == 2: if tracking.created_at.month == datetime.today().month: - # self.checked = True count += 1 # year elif self.unit == 3: if tracking.created_at.year == datetime.today().year: - # self.checked = True count += 1 self.percentage = int(count / self.times * 100) - 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 + 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 97aa309..d59e265 100644 --- a/models/HabitList.py +++ b/models/HabitList.py @@ -1,7 +1,7 @@ from dataclasses import dataclass from datetime import date, datetime -from db.SQLiteClient import delete_habitTrackings, create_habitList, get_habitList, get_habits, get_users +from db.SQLiteClient import delete_habitList, create_habitList, get_habitList, get_habits, get_users from models.Habit import Habit from models.User import User @@ -26,7 +26,7 @@ class HabitList: 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_habitTrackings(self.id) + delete_habitList(self.id) def get_habits(self): raw_habits = get_habits(self.id) diff --git a/models/User.py b/models/User.py index 9657f96..e4f2384 100644 --- a/models/User.py +++ b/models/User.py @@ -33,14 +33,6 @@ class User(UserMixin): def delete(self): delete_user(self.id) - # def get_habits(self): - # raw_habits = get_habits(self.id) - # habits = [] - # for habit in raw_habits: - # habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) - # habits.append(habit) - # return habits - def get_habitLists(self): from models.HabitList import HabitList