HabitTracker/models/HabitList.py
Yapollon 759c7d277f Model Overhaul
I'm done, this is the now complete code for the models and all.

To make it short:
- made some renaming that was necessary, HabitTrackings -> HabitTracking because every goddamn class is singular and not plural
- made the deletion process everywhere the same (Habit Deletion now handles the HabitTracking deletion inside the model and not through Sqlite.py)
- Some much needed comments
- Some adjustments to the HabitLists, nothing big, plus addition of update

- Also as a question, why does the HabitList have a redundant habits attribute, for now i marked this issue in the code to not forget it
2024-02-16 17:57:49 +01:00

71 lines
1.9 KiB
Python

from dataclasses import dataclass
from models.Habit import Habit
from models.User import User
from db.SQLiteClient import (create_habitList, get_habitList, get_habits, get_users, add_user, remove_user,
update_habitList, delete_habitList)
@dataclass
class HabitList:
id: int
name: str
description: str
habits: list = None #? unclear usage
@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]) if habitList else None
# Updates: name, description
def update(self):
update_habitList(self.id, self.name, self.description)
# Deletes the HabitList | The id of the current user is necessary
def delete(self, user_id):
if len(get_users) > 1:
self.remove_user(user_id)
else:
delete_habitList(self.id)
# Returns the Habits connected with the HabitList
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
# Returns the Users connected with the HabitList
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
# Adds a User by email to the HabitList
def add_user(self, email: str):
user = User.get_by_email(email)
add_user(self.id, user.id)
# Removes a User from the HabitList
def remove_user(self, user_id):
remove_user(self.id, user_id)