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
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
from datetime import datetime
|
|
|
|
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 = 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
|
|
|
|
def update(self):
|
|
update_user(self.id, self.name, self.email, self.password if self.password else None)
|
|
|
|
def delete(self):
|
|
delete_user(self.id)
|
|
|
|
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], datetime.strptime(habitList[3], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(habitList[4], "%Y-%m-%dT%H:%M:%S.%f"))
|
|
habitLists.append(habitList)
|
|
|
|
return habitLists
|
|
|
|
def get_heatmap(self) -> list:
|
|
heatmap = []
|
|
for day in range (0, 27):
|
|
value = get_heatmap_value(self.id, day)
|
|
heatmap.append(value)
|
|
return heatmap |