Big adjustments to the models

These adjustments address a lot of the current problems with the models, mainly the deletion process but also the addition of a add_user and remove_user function for the HabitLists.

In addition a lot of inconsistencies over the whole code were fixed and readjusted.

Thx for reading
This commit is contained in:
Yapollon 2024-02-14 12:55:00 +01:00
parent aaeb04d4ab
commit 869ead2077
5 changed files with 74 additions and 51 deletions

View File

@ -45,7 +45,8 @@ 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}' WHERE id = {id};"
query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' "
f"WHERE id = {id};")
else:
query = f"UPDATE users SET name = '{name}', email = '{email}', updated_at = '{now}' WHERE id = {id};"
conn = con3()
@ -71,8 +72,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) VALUES ('{list_id}', "
f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
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}');")
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
@ -168,7 +169,8 @@ 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}' WHERE id = {id};"
query = (f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' "
f"WHERE id = {id};")
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
@ -178,10 +180,12 @@ def update_habit(id: int, name: str, note: str, times: int, unit: int):
def delete_habit(id: int):
query = f"DELETE FROM habits WHERE id = {id};"
query = f"DELETE FROM habit_trackings WHERE id = habit_id;"
query2 = f"DELETE FROM habits WHERE id = {id};"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
cursor.execute(query2)
conn.commit()
conn.close()
@ -264,6 +268,37 @@ 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};"
@ -284,16 +319,6 @@ 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:

View File

@ -3,11 +3,11 @@ from dataclasses import dataclass
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
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)
# Unit wird als Integers wie folgt gemessen:
# Unit wird als Integer wie folgt gemessen:
# 0: Tag
# 1: Woche (Default)
# 2: Monat
@ -36,9 +36,7 @@ class Habit:
@staticmethod
def get(id: int):
habit = get_habit(id)
habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
return habit
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
update_habit(self.id, name, note, times, unit)
@ -69,13 +67,17 @@ class Habit:
update_slot(slot[0], slot[1] - 1)
delete_habit(self.id)
def get_habitTrackings(self) -> list[HabitTrackings]:
def get_habitTrackings(self) -> list:
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]))
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
@ -104,11 +106,3 @@ 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

View File

@ -1,9 +1,10 @@
from dataclasses import dataclass
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
@ -11,24 +12,19 @@ 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, datetime.now(), datetime.now())
return HabitList(id, name, description)
@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 delete(self):
delete_habitList(self.id)
def get_habits(self):
def get_habits(self) -> list:
raw_habits = get_habits(self.id)
habits = []
for habit in raw_habits:
@ -37,7 +33,7 @@ class HabitList:
return habits
def get_users(self):
def get_users(self) -> list:
raw_users = get_users(self.id)
users = []
for user in raw_users:
@ -45,3 +41,14 @@ 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)

View File

@ -1,6 +1,4 @@
from dataclasses import dataclass
from datetime import date, datetime
from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings
@ -8,17 +6,16 @@ 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, times: int):
def create(habit_id: int):
id = create_habitTrackings(habit_id)
return HabitTrackings(id, habit_id, datetime.now())
return HabitTrackings(id, habit_id)
@staticmethod
def get(id: int):
habitTrackings = get_habitTrackings(id)
return HabitTrackings(habitTrackings[0], habitTrackings[1], datetime.strptime(habitTrackings[2], "%Y-%m-%dT%H:%M:%S.%f")) if habitTrackings else None
return HabitTrackings(habitTrackings[0], habitTrackings[1]) if habitTrackings else None
def delete(self):
delete_habitTrackings(self.id)

View File

@ -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, delete_user, update_user, \
get_habitLists, get_heatmap_value
from db.SQLiteClient import (create_user, get_user, get_user_by_email, update_user, delete_user,
get_habitLists, get_heatmap_value)
class User(UserMixin):
@ -33,7 +33,7 @@ class User(UserMixin):
def delete(self):
delete_user(self.id)
def get_habitLists(self):
def get_habitLists(self) -> list:
from models.HabitList import HabitList
raw_habitLists = get_habitLists(self.id)
@ -44,7 +44,7 @@ class User(UserMixin):
return habitLists
def get_heatmap(self):
def get_heatmap(self) -> list:
heatmap = []
for day in range (0, 27):
value = get_heatmap_value(self.id, day)