Improved Habit delete()

Also added that updated_at gets changed in the table for every update function!
This commit is contained in:
Yapollon 2024-02-02 09:20:19 +01:00
parent f8ee727705
commit 111d11002e
3 changed files with 20 additions and 8 deletions

1
app.py
View File

@ -320,7 +320,6 @@ def delete_habit():
return {"error": "Habit does not belong to user"} return {"error": "Habit does not belong to user"}
habit.delete() habit.delete()
return {} return {}

View File

@ -43,10 +43,11 @@ def get_user_by_email(email: str):
def update_user(id: int, name: str, email: str, password: str = None): def update_user(id: int, name: str, email: str, password: str = None):
now = datetime.now().isoformat()
if password: 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: 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() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(query) cursor.execute(query)
@ -104,7 +105,8 @@ def get_heatmap_value(user_id: int, days: int):
date = (datetime.now() - timedelta(days=days)).date() date = (datetime.now() - timedelta(days=days)).date()
print(date) print(date)
query = f"SELECT id FROM habits WHERE user_id = {user_id};" 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) print(query2)
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
@ -141,7 +143,8 @@ def get_slots(user_id: int):
def update_slot(id: int, slot: 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() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(query) 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): 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() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(query) cursor.execute(query)

View File

@ -54,8 +54,6 @@ class Habit:
self.unit = unit 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): def update_slot(self, new_slot: int):
slots = get_slots(self.user_id) slots = get_slots(self.user_id)
print(slots) print(slots)
@ -71,6 +69,10 @@ class Habit:
def delete(self): 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) delete_habit(self.id)
@ -111,3 +113,10 @@ class Habit:
count += 1 count += 1
self.percentage = int(count / self.times * 100) 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)