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
This commit is contained in:
Yapollon 2024-02-16 17:57:49 +01:00
parent 252c8825d1
commit 759c7d277f
7 changed files with 83 additions and 48 deletions

2
app.py
View File

@ -6,7 +6,7 @@ from flask_login import login_required, LoginManager, login_user, logout_user, c
from models.Habit import Habit from models.Habit import Habit
from models.HabitList import HabitList from models.HabitList import HabitList
from models.HabitTrackings import HabitTrackings from models.HabitTracking import HabitTrackings
from models.User import User from models.User import User
from utils import anonymous_required from utils import anonymous_required

View File

@ -178,18 +178,16 @@ def update_habit(id: int, name: str, note: str, times: int, unit: int):
def delete_habit(id: int): def delete_habit(id: int):
query = f"DELETE FROM habit_trackings WHERE id = habit_id;" query = f"DELETE FROM habits WHERE id = {id};"
query2 = f"DELETE FROM habits WHERE id = {id};"
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(query) cursor.execute(query)
cursor.execute(query2)
conn.commit() conn.commit()
conn.close() conn.close()
### HabitTrackings.py ### ### HabitTracking ###
def create_habitTrackings(habit_id: int): def create_habitTracking(habit_id: int):
now = datetime.now().isoformat() now = datetime.now().isoformat()
query = f"INSERT INTO habit_trackings (habit_id, created_at) VALUES ('{habit_id}','{now}');" query = f"INSERT INTO habit_trackings (habit_id, created_at) VALUES ('{habit_id}','{now}');"
conn = con3() conn = con3()
@ -200,7 +198,7 @@ def create_habitTrackings(habit_id: int):
return cursor.lastrowid return cursor.lastrowid
def get_habitTrackings(id: int): def get_habitTracking(id: int):
query = f"SELECT * FROM habit_trackings WHERE id = {id};" query = f"SELECT * FROM habit_trackings WHERE id = {id};"
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
@ -210,7 +208,7 @@ def get_habitTrackings(id: int):
return habit_tracking return habit_tracking
def get_habitTrackings_by_habit_id(habit_id: int): def get_habitTrackings(habit_id: int):
query = f"SELECT * FROM habit_trackings WHERE habit_id = {habit_id};" query = f"SELECT * FROM habit_trackings WHERE habit_id = {habit_id};"
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
@ -220,7 +218,7 @@ def get_habitTrackings_by_habit_id(habit_id: int):
return habit_trackings return habit_trackings
def delete_habitTrackings(id: int): def delete_habitTracking(id: int):
query = f"DELETE FROM habit_trackings WHERE id = {id};" query = f"DELETE FROM habit_trackings WHERE id = {id};"
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
@ -229,7 +227,7 @@ def delete_habitTrackings(id: int):
conn.close() conn.close()
### HabitList.py ### ### HabitList ###
def create_habitList(user_id: int, name: str, description: str): def create_habitList(user_id: int, name: str, description: str):
now = datetime.now().isoformat() now = datetime.now().isoformat()
query = (f"INSERT INTO habit_lists (name, description, created_at, updated_at) " query = (f"INSERT INTO habit_lists (name, description, created_at, updated_at) "

View File

@ -2,9 +2,9 @@ import json
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from models.HabitTrackings import HabitTrackings from models.HabitTracking import HabitTracking
from db.SQLiteClient import (create_habit, get_habit, update_habit, delete_habit, get_next_slot, get_slots, update_slot, from db.SQLiteClient import (create_habit, get_habit, update_habit, delete_habit, get_next_slot, get_slots, update_slot,
get_habitTrackings_by_habit_id, get_habitList) get_habitTrackings, get_habitList)
# unit will be represented by integers like this: # unit will be represented by integers like this:
@ -39,10 +39,12 @@ class Habit:
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
# Updates: name, note, times, unit
def update(self): def update(self):
update_habit(self.id, self.name, self.note, self.times, self.unit) update_habit(self.id, self.name, self.note, self.times, self.unit)
# Updates the slot and reorders the HabitList accordingly
def update_slot(self, new_slot: int): def update_slot(self, new_slot: int):
# Fetches a list with the following structure [(id, slot), (id, slot), ...] # Fetches a list with the following structure [(id, slot), (id, slot), ...]
slots = get_slots(self.list_id) slots = get_slots(self.list_id)
@ -61,29 +63,39 @@ class Habit:
update_slot(self.id, new_slot) update_slot(self.id, new_slot)
# Deletes the Habit
def delete(self): def delete(self):
# Deletes the current habit # Reorders the slots
slots = get_slots(self.list_id)[self.slot+1:] slots = get_slots(self.list_id)[self.slot+1:]
for slot in slots: for slot in slots:
update_slot(slot[0], slot[1] - 1) update_slot(slot[0], slot[1] - 1)
# Deletes the current habit # Deletes all track-records associated with the Habit
trackings = self.get_habitTrackings()
for tracking in trackings:
tracking.delete()
# Deletes the current Habit
delete_habit(self.id) delete_habit(self.id)
# Returns all track-records for a Habit
def get_habitTrackings(self) -> list: def get_habitTrackings(self) -> list:
trackings = [] trackings = []
for rawTracking in get_habitTrackings_by_habit_id(self.id): for rawTracking in get_habitTrackings(self.id):
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1])) trackings.append(HabitTracking(rawTracking[0], rawTracking[1],
datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f")))
return trackings return trackings
# Returns the HabitList in which the Habit is located
def habit_list(self) -> list: def habit_list(self) -> list:
from models.HabitList import HabitList from models.HabitList import HabitList
raw_habitLists = get_habitList(self.list_id) raw_habitLists = get_habitList(self.list_id)
return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2]) if raw_habitLists else None return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2]) if raw_habitLists else None
# Saves the progress of the Habit in the attribute percentage
def fill_statistics(self): def fill_statistics(self):
count = 0 count = 0
self.checked = False self.checked = False
@ -111,5 +123,6 @@ class Habit:
self.percentage = int(count / self.times * 100) self.percentage = int(count / self.times * 100)
# Converts the Habit data to a json format
def to_json(self): def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)

View File

@ -2,8 +2,8 @@ from dataclasses import dataclass
from models.Habit import Habit from models.Habit import Habit
from models.User import User from models.User import User
from db.SQLiteClient import (delete_habitList, create_habitList, get_habitList, get_habits, from db.SQLiteClient import (create_habitList, get_habitList, get_habits, get_users, add_user, remove_user,
get_users, add_user, remove_user) update_habitList, delete_habitList)
@dataclass @dataclass
@ -11,7 +11,7 @@ class HabitList:
id: int id: int
name: str name: str
description: str description: str
habits: list = None habits: list = None #? unclear usage
@staticmethod @staticmethod
def create(user_id: int, name: str, description: str): def create(user_id: int, name: str, description: str):
@ -23,6 +23,21 @@ class HabitList:
habitList = get_habitList(id) habitList = get_habitList(id)
return HabitList(habitList[0], habitList[1], habitList[2]) if habitList else None 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: def get_habits(self) -> list:
raw_habits = get_habits(self.id) raw_habits = get_habits(self.id)
habits = [] habits = []
@ -32,6 +47,8 @@ class HabitList:
return habits return habits
# Returns the Users connected with the HabitList
def get_users(self) -> list: def get_users(self) -> list:
raw_users = get_users(self.id) raw_users = get_users(self.id)
users = [] users = []
@ -41,13 +58,13 @@ class HabitList:
return users return users
# Adds a User by email to the HabitList
def add_user(self, email: str): def add_user(self, email: str):
user = User.get_by_email(email) user = User.get_by_email(email)
add_user(self.id, user.id) add_user(self.id, user.id)
# The id of the current user is necessary
def delete(self, user_id): # Removes a User from the HabitList
if len(get_users) > 1: def remove_user(self, user_id):
remove_user(self.id, user_id) remove_user(self.id, user_id)
else:
delete_habitList(self.id)

26
models/HabitTracking.py Normal file
View File

@ -0,0 +1,26 @@
from datetime import date, datetime
from dataclasses import dataclass
from db.SQLiteClient import create_habitTracking, get_habitTracking, delete_habitTracking
@dataclass
class HabitTracking:
id: int
habit_id: int
created_at: date
@staticmethod
def create(habit_id: int):
id = create_habitTracking(habit_id)
return HabitTracking(id, habit_id, datetime.now())
@staticmethod
def get(id: int):
habitTrackings = get_habitTracking(id)
return HabitTracking(habitTrackings[0], habitTrackings[1],
datetime.strptime(habitTrackings[2], "%Y-%m-%dT%H:%M:%S.%f")) \
if habitTrackings else None
# Deletes the HabitTracking
def delete(self):
delete_habitTracking(self.id)

View File

@ -1,21 +0,0 @@
from dataclasses import dataclass
from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings
@dataclass
class HabitTrackings:
id: int
habit_id: int
@staticmethod
def create(habit_id: int):
id = create_habitTrackings(habit_id)
return HabitTrackings(id, habit_id)
@staticmethod
def get(id: int):
habitTrackings = get_habitTrackings(id)
return HabitTrackings(habitTrackings[0], habitTrackings[1]) if habitTrackings else None
def delete(self):
delete_habitTrackings(self.id)

View File

@ -26,10 +26,12 @@ class User(UserMixin):
return User(user[0], user[1], user[2], user[3]) if user else None return User(user[0], user[1], user[2], user[3]) if user else None
# Updates: name, email, password
def update(self): def update(self):
update_user(self.id, self.name, self.email, self.password if self.password else None) update_user(self.id, self.name, self.email, self.password if self.password else None)
# Deletes the User
def delete(self): def delete(self):
# calls the deletion of the users habitLists # calls the deletion of the users habitLists
habitLists = self.get_habitLists() habitLists = self.get_habitLists()
@ -40,7 +42,7 @@ class User(UserMixin):
delete_user(self.id) delete_user(self.id)
# returns all habitLists assigned with the user # Returns all HabitLists connected with the user
def get_habitLists(self) -> list: def get_habitLists(self) -> list:
from models.HabitList import HabitList from models.HabitList import HabitList
@ -53,7 +55,7 @@ class User(UserMixin):
return habitLists return habitLists
# return the heatmap-values of the last 28 days # Returns all heatmap-values from the last 28 days
def get_heatmap(self) -> list: def get_heatmap(self) -> list:
heatmap = [] heatmap = []
for day in range (0, 28): for day in range (0, 28):