HabitTracker/models/HabitList.py
Yapollon 869ead2077 Big adjustments to the models
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
2024-02-14 12:55:00 +01:00

55 lines
1.6 KiB
Python

from dataclasses import dataclass
from datetime import date, datetime
from models.Habit import Habit
from models.User import User
from db.SQLiteClient import (delete_habitList, create_habitList, get_habitList, get_habits,
get_users, add_user, remove_user)
@dataclass
class HabitList:
id: int
name: str
description: str
habits: list = None
@staticmethod
def create(user_id: int, name: str, description: str):
id = create_habitList(user_id, name, description)
return HabitList(id, name, description)
@staticmethod
def get(id: int):
habitList = get_habitList(id)
return 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")) if habitList else None
def get_habits(self) -> list:
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_users(self) -> list:
raw_users = get_users(self.id)
users = []
for user in raw_users:
user = User(user[0], user[1], user[2], user[3])
users.append(user)
return users
def add_user(self, email: str):
user = User.get_by_email(email)
add_user(self.id, user.id)
# The id of the current user is necessary
def delete(self, user_id):
if len(get_users) > 1:
remove_user(self.id, user_id)
else:
delete_habitList(self.id)