From 111d11002e21c99b1482abac7f3db6ad912823f0 Mon Sep 17 00:00:00 2001 From: Yapollon Date: Fri, 2 Feb 2024 09:20:19 +0100 Subject: [PATCH] Improved Habit delete() Also added that updated_at gets changed in the table for every update function! --- app.py | 1 - db/SQLiteClient.py | 14 +++++++++----- models/Habit.py | 13 +++++++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 295a8cc..71fff9e 100644 --- a/app.py +++ b/app.py @@ -320,7 +320,6 @@ def delete_habit(): return {"error": "Habit does not belong to user"} habit.delete() - return {} diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index 4220d40..068541c 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -43,10 +43,11 @@ 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}' 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}' WHERE id = {id};" + query = f"UPDATE users SET name = '{name}', email = '{email}', updated_at = {now} WHERE id = {id};" conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -104,7 +105,8 @@ def get_heatmap_value(user_id: int, days: int): date = (datetime.now() - timedelta(days=days)).date() print(date) query = f"SELECT id FROM habits WHERE user_id = {user_id};" - query2 = f"SELECT habits.id FROM habits, habit_trackings WHERE habits.user_id = {user_id} AND habits.created_at LIKE '{date}%' AND habit_trackings.habit_id = habits.id;" + query2 = (f"SELECT habits.id FROM habits, habit_trackings WHERE habits.user_id = {user_id} " + f"AND habits.created_at LIKE '{date}%' AND habit_trackings.habit_id = habits.id;") print(query2) conn = con3() cursor = conn.cursor() @@ -141,7 +143,8 @@ def get_slots(user_id: int): def update_slot(id: int, slot: int): - query = f"UPDATE habits SET slot = {slot} WHERE id = {id};" + now = datetime.now().isoformat() + query = f"UPDATE habits SET slot = {slot}, updated_at = {now} WHERE id = {id};" conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -151,7 +154,8 @@ def update_slot(id: int, slot: int): def update_habit(id: int, name: str, note: str, times: int, unit: int): - query = f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit} WHERE id = {id};" + now = datetime.now().isoformat() + 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) diff --git a/models/Habit.py b/models/Habit.py index 4e51c59..45787e1 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -54,8 +54,6 @@ class Habit: self.unit = unit - # So sollte die Slots Liste aussehen damit es funktioniert - #[(id, 1), (id, 2), (id, 3), (id, 4), (id, 5)] def update_slot(self, new_slot: int): slots = get_slots(self.user_id) print(slots) @@ -71,6 +69,10 @@ class Habit: def delete(self): + slots = get_slots(self.user_id)[self.slot+1:] + print(slots) + for slot in slots: + update_slot(slot[0], slot[1] - 1) delete_habit(self.id) @@ -111,3 +113,10 @@ class Habit: count += 1 self.percentage = int(count / self.times * 100) + + +# Test for update Slot + #user = User.get(1) + #habits = user.get_habits() + #print(habits[6]) + #habits[6].update_slot(3) \ No newline at end of file