model #34

Merged
Verox merged 2 commits from model into master 2024-02-14 21:54:04 +01:00
5 changed files with 79 additions and 55 deletions

View File

@ -45,7 +45,8 @@ def get_user_by_email(email: str):
def update_user(id: int, name: str, email: str, password: str = None): def update_user(id: int, name: str, email: str, password: str = None):
now = datetime.now().isoformat() now = datetime.now().isoformat()
if password: if password:
query = f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' WHERE id = {id};" query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' "
f"WHERE id = {id};")
else: else:
query = f"UPDATE users SET name = '{name}', email = '{email}', updated_at = '{now}' WHERE id = {id};" query = f"UPDATE users SET name = '{name}', email = '{email}', updated_at = '{now}' WHERE id = {id};"
conn = con3() conn = con3()
@ -57,12 +58,10 @@ def update_user(id: int, name: str, email: str, password: str = None):
def delete_user(id: int): def delete_user(id: int):
query = f"DELETE FROM habit_lists WHERE (SELECT list_id FROM habit_users WHERE user_id = {id}) = id;" query = f"DELETE FROM users WHERE id = {id};"
query2 = f"DELETE FROM users 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()
return cursor.lastrowid return cursor.lastrowid
@ -71,8 +70,8 @@ def delete_user(id: int):
### Habit.py ### ### Habit.py ###
def create_habit(list_id: int, name: str, times: int, unit: int, slot: int, note: str | None=None): def create_habit(list_id: int, name: str, times: int, unit: int, slot: int, note: str | None=None):
now = datetime.now().isoformat() now = datetime.now().isoformat()
query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) VALUES ('{list_id}', " query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) "
f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');") f"VALUES ('{list_id}', '{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(query) cursor.execute(query)
@ -168,7 +167,8 @@ def update_slot(id: int, slot: int):
def update_habit(id: int, name: str, note: str, times: int, unit: int): def update_habit(id: int, name: str, note: str, times: int, unit: int):
now = datetime.now().isoformat() now = datetime.now().isoformat()
query = f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' WHERE id = {id};" query = (f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' "
f"WHERE id = {id};")
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(query) cursor.execute(query)
@ -178,10 +178,12 @@ 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 habits WHERE id = {id};" query = f"DELETE FROM habit_trackings WHERE id = habit_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()
@ -264,6 +266,37 @@ def get_habitLists(user_id: int):
return habit_lists return habit_lists
def get_users(list_id: int):
query = (f"SELECT users.* FROM users JOIN habit_users ON users.id = habit_users.user_id WHERE "
f"habit_users.list_id = {list_id};")
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
users = cursor.fetchall()
conn.close()
return users
def add_user(list_id: int, user_id: int):
now = datetime.now().isoformat()
query = (f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at)"
f" VALUES ('{user_id}', '{list_id}', '{now}', '{now}');")
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
conn.close()
def remove_user(list_id: int, user_id: int):
query = f"DELETE FROM habit_lists WHERE user_id = {user_id} AND list_id = {list_id};"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
conn.close()
def update_habitList(id: int, name: str, description: str): def update_habitList(id: int, name: str, description: str):
now = datetime.now().isoformat() now = datetime.now().isoformat()
query = f"UPDATE habit_lists SET name = {name}, description = {description}, updated_at = '{now}' WHERE id = {id};" query = f"UPDATE habit_lists SET name = {name}, description = {description}, updated_at = '{now}' WHERE id = {id};"
@ -284,16 +317,6 @@ def delete_habitList(id: int):
conn.close() conn.close()
def get_users(list_id: int):
query = f"SELECT users.* FROM users JOIN habit_users ON users.id = habit_users.user_id WHERE habit_users.list_id = {list_id};"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
users = cursor.fetchall()
conn.close()
return users
if __name__ == "__main__": if __name__ == "__main__":
habits = get_habits(1) habits = get_habits(1)
for habit in habits: for habit in habits:

View File

@ -3,11 +3,11 @@ from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from models.HabitTrackings import HabitTrackings from models.HabitTrackings import HabitTrackings
from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_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_slots, update_habit, get_habitList get_habitTrackings_by_habit_id, get_habitList)
# Unit wird als Integers wie folgt gemessen: # Unit wird als Integer wie folgt gemessen:
# 0: Tag # 0: Tag
# 1: Woche (Default) # 1: Woche (Default)
# 2: Monat # 2: Monat
@ -36,9 +36,7 @@ class Habit:
@staticmethod @staticmethod
def get(id: int): def get(id: int):
habit = get_habit(id) habit = get_habit(id)
habit = 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
return habit
def update(self, name: str=None, note: str=None, times: int=None, unit: int=None): def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
update_habit(self.id, name, note, times, unit) update_habit(self.id, name, note, times, unit)
@ -69,13 +67,17 @@ class Habit:
update_slot(slot[0], slot[1] - 1) update_slot(slot[0], slot[1] - 1)
delete_habit(self.id) delete_habit(self.id)
def get_habitTrackings(self) -> list[HabitTrackings]: def get_habitTrackings(self) -> list:
trackings = [] trackings = []
for rawTracking in get_habitTrackings_by_habit_id(self.id): for rawTracking in get_habitTrackings_by_habit_id(self.id):
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1], trackings.append(HabitTrackings(rawTracking[0], rawTracking[1]))
datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f")))
return trackings return trackings
def habit_list(self):
from models.HabitList import HabitList
raw_habitLists = get_habitList(self.list_id)
return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2]) if raw_habitLists else None
def fill_statistics(self): def fill_statistics(self):
count = 0 count = 0
self.checked = False self.checked = False
@ -104,11 +106,3 @@ class Habit:
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)
def habit_list(self):
from models.HabitList import HabitList
raw_habitLists = get_habitList(self.list_id)
return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2],
datetime.strptime(raw_habitLists[3], "%Y-%m-%dT%H:%M:%S.%f"),
datetime.strptime(raw_habitLists[4], "%Y-%m-%dT%H:%M:%S.%f")) \
if raw_habitLists else None

View File

@ -1,9 +1,10 @@
from dataclasses import dataclass from dataclasses import dataclass
from datetime import date, datetime from datetime import datetime
from db.SQLiteClient import delete_habitList, create_habitList, get_habitList, get_habits, get_users
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,
get_users, add_user, remove_user)
@dataclass @dataclass
@ -11,24 +12,19 @@ class HabitList:
id: int id: int
name: str name: str
description: str description: str
created_at: date
updated_at: date
habits: list = None habits: list = None
@staticmethod @staticmethod
def create(user_id: int, name: str, description: str): def create(user_id: int, name: str, description: str):
id = create_habitList(user_id, name, description) id = create_habitList(user_id, name, description)
return HabitList(id, name, description, datetime.now(), datetime.now()) return HabitList(id, name, description)
@staticmethod @staticmethod
def get(id: int): def get(id: int):
habitList = get_habitList(id) 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 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 delete(self): def get_habits(self) -> list:
delete_habitList(self.id)
def get_habits(self):
raw_habits = get_habits(self.id) raw_habits = get_habits(self.id)
habits = [] habits = []
for habit in raw_habits: for habit in raw_habits:
@ -37,7 +33,7 @@ class HabitList:
return habits return habits
def get_users(self): def get_users(self) -> list:
raw_users = get_users(self.id) raw_users = get_users(self.id)
users = [] users = []
for user in raw_users: for user in raw_users:
@ -45,3 +41,14 @@ class HabitList:
users.append(user) users.append(user)
return users 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)

View File

@ -1,6 +1,4 @@
from dataclasses import dataclass from dataclasses import dataclass
from datetime import date, datetime
from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings
@ -8,17 +6,16 @@ from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_ha
class HabitTrackings: class HabitTrackings:
id: int id: int
habit_id: int habit_id: int
created_at: date
@staticmethod @staticmethod
def create(habit_id: int, times: int): def create(habit_id: int):
id = create_habitTrackings(habit_id) id = create_habitTrackings(habit_id)
return HabitTrackings(id, habit_id, datetime.now()) return HabitTrackings(id, habit_id)
@staticmethod @staticmethod
def get(id: int): def get(id: int):
habitTrackings = get_habitTrackings(id) habitTrackings = get_habitTrackings(id)
return HabitTrackings(habitTrackings[0], habitTrackings[1], datetime.strptime(habitTrackings[2], "%Y-%m-%dT%H:%M:%S.%f")) if habitTrackings else None return HabitTrackings(habitTrackings[0], habitTrackings[1]) if habitTrackings else None
def delete(self): def delete(self):
delete_habitTrackings(self.id) delete_habitTrackings(self.id)

View File

@ -1,8 +1,8 @@
from datetime import datetime from datetime import datetime
from flask_login import UserMixin from flask_login import UserMixin
from db.SQLiteClient import create_user, get_user, get_user_by_email, delete_user, update_user, \ from db.SQLiteClient import (create_user, get_user, get_user_by_email, update_user, delete_user,
get_habitLists, get_heatmap_value get_habitLists, get_heatmap_value)
class User(UserMixin): class User(UserMixin):
@ -31,9 +31,12 @@ class User(UserMixin):
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)
def delete(self): def delete(self):
habitLists = self.get_habitLists()
for habitList in habitLists:
habitList.delete(self.id)
delete_user(self.id) delete_user(self.id)
def get_habitLists(self): def get_habitLists(self) -> list:
from models.HabitList import HabitList from models.HabitList import HabitList
raw_habitLists = get_habitLists(self.id) raw_habitLists = get_habitLists(self.id)
@ -44,7 +47,7 @@ class User(UserMixin):
return habitLists return habitLists
def get_heatmap(self): def get_heatmap(self) -> list:
heatmap = [] heatmap = []
for day in range (0, 27): for day in range (0, 27):
value = get_heatmap_value(self.id, day) value = get_heatmap_value(self.id, day)