96 lines
3.2 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, accept_List, get_unaccepted_habitLists)
class User(UserMixin):
def __init__(self, id: int, name: str, email: str, password: str=None, profile_image:str=None, heatmap_color: str=None):
self.id = id
self.name = name
self.email = email
self.password = password
self.profile_image = profile_image
self.heatmap_color = heatmap_color
@staticmethod
def create(name: str, email: str, password: str):
heatmap_color = "#00FF00"
id, profile_image = create_user(name, email, password, heatmap_color)
return User(id=id, name=name, email=email, profile_image=profile_image, heatmap_color=heatmap_color)
@staticmethod
def get(id: int):
user = get_user(id)
return User(user[0], user[1], user[2], user[3], user[4], user[5]) 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], user[4], user[5]) 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, self.profile_image, self.heatmap_color)
# 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:
accepted = habitList[6]
habitList = HabitList(habitList[0], habitList[1], habitList[2], habitList[3])
if accepted == 1:
habitLists.append(habitList)
return habitLists
def get_unaccepted_habitLists(self) -> list:
from models.HabitList import HabitList
raw_habitLists = get_unaccepted_habitLists(self.id)
habitLists = []
for habitList in raw_habitLists:
habitList = HabitList(habitList[0], habitList[1], habitList[2], habitList[3])
habitLists.append(habitList)
return habitLists
# Returns all heatmap-values from the last 28 days
def get_heatmap(self) -> tuple:
# get current day of week as integer. monday is 0 and sunday is 6
weekday = 6 - datetime.today().weekday()
heatmap = [100]
# append the heatmap values of the current week
for day in range(0, weekday):
heatmap.append(0)
for day in range (0, 28-weekday):
value = get_heatmap_value(self.id, day)
heatmap.append(value)
heatmap.reverse()
day = 27-weekday
return heatmap, day
def accept_List(self, HabitList_id):
accept_List(HabitList_id, self.id)