2024-02-12 21:07:55 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2024-01-12 16:53:03 +01:00
|
|
|
from flask_login import UserMixin
|
2024-02-12 21:07:55 +01:00
|
|
|
from db.SQLiteClient import create_user, get_user, get_user_by_email, get_habits, delete_user, update_user, \
|
|
|
|
|
get_habitLists
|
2024-01-12 10:57:58 +01:00
|
|
|
|
2024-01-12 16:53:03 +01:00
|
|
|
|
|
|
|
|
class User(UserMixin):
|
2024-01-13 13:07:56 +01:00
|
|
|
def __init__(self, id: int, name: str, email: str, password: str | None = 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-01-12 10:57:58 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-12 16:53:03 +01:00
|
|
|
def create(name: str, email: str, password: str):
|
2024-01-12 10:57:58 +01:00
|
|
|
id = create_user(name, email, password)
|
|
|
|
|
return User(id, name, email)
|
|
|
|
|
|
|
|
|
|
@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-01-12 16:53:03 +01:00
|
|
|
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
|
2024-01-17 11:46:07 +01:00
|
|
|
|
2024-01-26 10:40:12 +01:00
|
|
|
def update(self):
|
2024-01-26 10:45:58 +01:00
|
|
|
update_user(self.id, self.name, self.email, self.password if self.password else None)
|
2024-01-26 08:10:36 +01:00
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
|
delete_user(self.id)
|
2024-01-17 11:46:07 +01:00
|
|
|
|
2024-02-12 21:07:55 +01:00
|
|
|
# def get_habits(self):
|
|
|
|
|
# raw_habits = get_habits(self.id)
|
|
|
|
|
# habits = []
|
|
|
|
|
# for habit in raw_habits:
|
|
|
|
|
# habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6])
|
|
|
|
|
# habits.append(habit)
|
|
|
|
|
# return habits
|
|
|
|
|
|
|
|
|
|
def get_habitLists(self):
|
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 = []
|
|
|
|
|
for habitList in raw_habitLists:
|
2024-02-12 22:06:27 +01:00
|
|
|
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"))
|
2024-02-12 21:07:55 +01:00
|
|
|
habitLists.append(habitList)
|
|
|
|
|
|
|
|
|
|
return habitLists
|