2024-02-20 09:58:34 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2024-01-12 16:53:03 +01:00
|
|
|
from flask_login import UserMixin
|
2024-02-14 12:55:00 +01:00
|
|
|
from db.SQLiteClient import (create_user, get_user, get_user_by_email, update_user, delete_user,
|
2024-03-07 20:34:30 +01:00
|
|
|
get_habitLists, get_heatmap_value, accept_List, get_unaccepted_habitLists)
|
2024-01-12 10:57:58 +01:00
|
|
|
|
2024-01-12 16:53:03 +01:00
|
|
|
|
|
|
|
|
class User(UserMixin):
|
2024-03-07 17:54:49 +01:00
|
|
|
def __init__(self, id: int, name: str, email: str, password: str=None, profile_image:str=None, heatmap_color: str=None):
|
2024-01-12 10:57:58 +01:00
|
|
|
self.id = id
|
|
|
|
|
self.name = name
|
|
|
|
|
self.email = email
|
2024-01-12 16:53:03 +01:00
|
|
|
self.password = password
|
2024-02-18 04:53:15 +01:00
|
|
|
self.profile_image = profile_image
|
2024-03-07 17:54:49 +01:00
|
|
|
self.heatmap_color = heatmap_color
|
2024-01-12 10:57:58 +01:00
|
|
|
|
2024-02-20 04:16:28 +01:00
|
|
|
|
2024-01-12 10:57:58 +01:00
|
|
|
@staticmethod
|
2024-02-20 04:16:28 +01:00
|
|
|
def create(name: str, email: str, password: str):
|
2024-03-08 12:35:46 +01:00
|
|
|
heatmap_color = "#00FF00"
|
2024-03-07 17:54:49 +01:00
|
|
|
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)
|
2024-01-12 10:57:58 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-12 16:53:03 +01:00
|
|
|
def get(id: int):
|
2024-01-12 10:57:58 +01:00
|
|
|
user = get_user(id)
|
2024-03-07 17:54:49 +01:00
|
|
|
return User(user[0], user[1], user[2], user[3], user[4], user[5]) if user else None
|
2024-01-12 16:53:03 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_by_email(email: str):
|
|
|
|
|
user = get_user_by_email(email)
|
2024-03-07 17:54:49 +01:00
|
|
|
return User(user[0], user[1], user[2], user[3], user[4], user[5]) if user else None
|
2024-01-17 11:46:07 +01:00
|
|
|
|
2024-02-16 08:11:22 +01:00
|
|
|
|
2024-02-20 04:16:28 +01:00
|
|
|
# Updates: name, email, password
|
2024-01-26 10:40:12 +01:00
|
|
|
def update(self):
|
2024-03-07 17:54:49 +01:00
|
|
|
update_user(self.id, self.name, self.email, self.password if self.password else None, self.profile_image, self.heatmap_color)
|
2024-02-16 08:11:22 +01:00
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
# Deletes the User
|
2024-01-26 08:10:36 +01:00
|
|
|
def delete(self):
|
2024-02-16 08:11:22 +01:00
|
|
|
# calls the deletion of the users habitLists
|
2024-02-14 21:03:11 +01:00
|
|
|
habitLists = self.get_habitLists()
|
|
|
|
|
for habitList in habitLists:
|
|
|
|
|
habitList.delete(self.id)
|
2024-02-16 08:11:22 +01:00
|
|
|
|
|
|
|
|
# deletes the user
|
2024-01-26 08:10:36 +01:00
|
|
|
delete_user(self.id)
|
2024-01-17 11:46:07 +01:00
|
|
|
|
2024-02-16 08:11:22 +01:00
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
# Returns all HabitLists connected with the user
|
2024-02-14 12:55:00 +01:00
|
|
|
def get_habitLists(self) -> list:
|
2024-02-12 22:31:51 +01:00
|
|
|
from models.HabitList import HabitList
|
|
|
|
|
|
2024-02-12 21:07:55 +01:00
|
|
|
raw_habitLists = get_habitLists(self.id)
|
|
|
|
|
habitLists = []
|
2024-03-07 20:34:30 +01:00
|
|
|
for habitList in raw_habitLists:
|
|
|
|
|
accepted = habitList[6]
|
2024-03-10 13:10:54 +01:00
|
|
|
habitList = HabitList(habitList[0], habitList[1], habitList[2], habitList[3])
|
2024-03-14 19:50:50 +01:00
|
|
|
if accepted == 1:
|
2024-03-07 20:34:30 +01:00
|
|
|
habitLists.append(habitList)
|
|
|
|
|
|
|
|
|
|
return habitLists
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_unaccepted_habitLists(self) -> list:
|
|
|
|
|
from models.HabitList import HabitList
|
|
|
|
|
|
|
|
|
|
raw_habitLists = get_unaccepted_habitLists(self.id)
|
|
|
|
|
habitLists = []
|
2024-02-12 21:07:55 +01:00
|
|
|
for habitList in raw_habitLists:
|
2024-03-10 13:10:54 +01:00
|
|
|
habitList = HabitList(habitList[0], habitList[1], habitList[2], habitList[3])
|
2024-02-12 21:07:55 +01:00
|
|
|
habitLists.append(habitList)
|
|
|
|
|
|
|
|
|
|
return habitLists
|
2024-02-14 10:52:25 +01:00
|
|
|
|
2024-02-16 08:11:22 +01:00
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
# Returns all heatmap-values from the last 28 days
|
2024-03-07 15:56:33 +01:00
|
|
|
def get_heatmap(self) -> tuple:
|
2024-02-20 10:01:08 +01:00
|
|
|
# get current day of week as integer. monday is 0 and sunday is 6
|
2024-03-25 12:41:01 +01:00
|
|
|
weekday = 6 - datetime.today().weekday()
|
2024-03-07 15:07:26 +01:00
|
|
|
heatmap = [100]
|
2024-02-20 09:58:34 +01:00
|
|
|
|
|
|
|
|
# append the heatmap values of the current week
|
2024-03-25 12:41:01 +01:00
|
|
|
for day in range(0, weekday):
|
2024-02-20 09:58:34 +01:00
|
|
|
heatmap.append(0)
|
|
|
|
|
|
2024-03-25 12:41:01 +01:00
|
|
|
for day in range (0, 28-weekday):
|
2024-02-14 10:52:25 +01:00
|
|
|
value = get_heatmap_value(self.id, day)
|
|
|
|
|
heatmap.append(value)
|
2024-03-07 15:04:29 +01:00
|
|
|
heatmap.reverse()
|
2024-03-25 12:41:01 +01:00
|
|
|
day = 27-weekday
|
2024-03-07 18:44:13 +01:00
|
|
|
return heatmap, day
|
|
|
|
|
|
|
|
|
|
def accept_List(self, HabitList_id):
|
2024-03-07 20:34:30 +01:00
|
|
|
accept_List(HabitList_id, self.id)
|