I'm done, this is the now complete code for the models and all. To make it short: - made some renaming that was necessary, HabitTrackings -> HabitTracking because every goddamn class is singular and not plural - made the deletion process everywhere the same (Habit Deletion now handles the HabitTracking deletion inside the model and not through Sqlite.py) - Some much needed comments - Some adjustments to the HabitLists, nothing big, plus addition of update - Also as a question, why does the HabitList have a redundant habits attribute, for now i marked this issue in the code to not forget it
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
from flask_login import UserMixin
|
|
from db.SQLiteClient import (create_user, get_user, get_user_by_email, update_user, delete_user,
|
|
get_habitLists, get_heatmap_value)
|
|
|
|
|
|
class User(UserMixin):
|
|
def __init__(self, id: int, name: str, email: str, password: str = None):
|
|
self.id = id
|
|
self.name = name
|
|
self.email = email
|
|
self.password = password
|
|
|
|
@staticmethod
|
|
def create(name: str, email: str, password: str):
|
|
id = create_user(name, email, password)
|
|
return User(id, name, email)
|
|
|
|
@staticmethod
|
|
def get(id: int):
|
|
user = get_user(id)
|
|
return User(user[0], user[1], user[2], user[3]) if user else None
|
|
|
|
@staticmethod
|
|
def get_by_email(email: str):
|
|
user = get_user_by_email(email)
|
|
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()
|
|
for habitList in habitLists:
|
|
habitList.delete(self.id)
|
|
|
|
# deletes the user
|
|
delete_user(self.id)
|
|
|
|
|
|
# Returns all HabitLists connected with the user
|
|
def get_habitLists(self) -> list:
|
|
from models.HabitList import HabitList
|
|
|
|
raw_habitLists = get_habitLists(self.id)
|
|
habitLists = []
|
|
for habitList in raw_habitLists:
|
|
habitList = HabitList(habitList[0], habitList[1], habitList[2])
|
|
habitLists.append(habitList)
|
|
|
|
return habitLists
|
|
|
|
|
|
# Returns all heatmap-values from the last 28 days
|
|
def get_heatmap(self) -> list:
|
|
heatmap = []
|
|
for day in range (0, 28):
|
|
value = get_heatmap_value(self.id, day)
|
|
heatmap.append(value)
|
|
return heatmap |