This commit is contained in:
Yapollon 2024-02-16 08:11:22 +01:00
parent ae0e0cf907
commit 7600bd8f2c
4 changed files with 46 additions and 33 deletions

View File

@ -8,7 +8,7 @@ def con3():
return conn
### User.py ###
### User ###
def create_user(name: str, email: str, password: str):
password = hashlib.sha256(password.encode()).hexdigest()
now = datetime.now().isoformat()
@ -42,7 +42,7 @@ def get_user_by_email(email: str):
return user
def update_user(id: int, name: str, email: str, password: str = None):
def update_user(id: int, name: str, email: str, password: str):
now = datetime.now().isoformat()
if password:
query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' "
@ -67,8 +67,8 @@ def delete_user(id: int):
return cursor.lastrowid
### Habit.py ###
def create_habit(list_id: int, name: str, times: int, unit: int, slot: int, note: str | None=None):
### Habit ###
def create_habit(list_id: int, name: str, note: str, times: int, unit: int, slot: int):
now = datetime.now().isoformat()
query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) "
f"VALUES ('{list_id}', '{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")

View File

@ -7,11 +7,11 @@ from db.SQLiteClient import (create_habit, get_habit, update_habit, delete_habit
get_habitTrackings_by_habit_id, get_habitList)
# Unit wird als Integer wie folgt gemessen:
# 0: Tag
# 1: Woche (Default)
# 2: Monat
# 3: Jahr
# unit will be represented by integers like this:
# 0: day
# 1: week (default)
# 2: month
# 3: year
@dataclass
class Habit:
@ -28,9 +28,9 @@ class Habit:
self.fill_statistics()
@staticmethod
def create(list_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
def create(list_id: int, name: str, times: int, note: str = None, unit: int = 1):
slot = get_next_slot(list_id)
id = create_habit(list_id, name, times, unit, slot, note)
id = create_habit(list_id, name, note, times, unit, slot)
return Habit(id, list_id, name, note, times, unit, slot)
@staticmethod
@ -38,46 +38,52 @@ class Habit:
habit = get_habit(id)
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
update_habit(self.id, name, note, times, unit)
if name is not None:
self.name = name
if note is not None:
self.note = note
if times is not None:
self.times = times
if unit is not None:
self.unit = unit
def update(self):
update_habit(self.id, self.name, self.note, self.times, self.unit)
def update_slot(self, new_slot: int):
# Fetches a list with the following structure [(id, slot), (id, slot), ...]
slots = get_slots(self.list_id)
if new_slot > self.slot:
slots = slots[self.slot:new_slot]
# Splits the list depending on whether the new slot is higher or lower than the current one
if new_slot > self.slot: # Example self.slot=1 new_slot=4
slots = slots[self.slot:new_slot] # Expected list: [(id, 2), (id, 3), (id, 4)]
for slot in slots:
update_slot(slot[0], slot[1]-1)
if new_slot < self.slot:
slots = slots[new_slot-1:self.slot-1]
if new_slot < self.slot: # Example self.slot=4 new_slot=1
slots = slots[new_slot-1:self.slot-1] # Expected list: [(id, 1), (id, 2), (id, 3)]
for slot in slots:
update_slot(slot[0], slot[1]+1)
# Update the slot of the current habit
update_slot(self.id, new_slot)
def delete(self):
# Deletes the current habit
slots = get_slots(self.list_id)[self.slot+1:]
for slot in slots:
update_slot(slot[0], slot[1] - 1)
# Deletes the current habit
delete_habit(self.id)
def get_habitTrackings(self) -> list:
trackings = []
for rawTracking in get_habitTrackings_by_habit_id(self.id):
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1]))
return trackings
def habit_list(self):
def habit_list(self) -> list:
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):
count = 0
self.checked = False
@ -104,5 +110,6 @@ class Habit:
self.percentage = int(count / self.times * 100)
def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)

View File

@ -1,5 +1,4 @@
from dataclasses import dataclass
from datetime import datetime
from models.Habit import Habit
from models.User import User
@ -22,7 +21,7 @@ class HabitList:
@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
return HabitList(habitList[0], habitList[1], habitList[2]) if habitList else None
def get_habits(self) -> list:
raw_habits = get_habits(self.id)

View File

@ -1,12 +1,10 @@
from datetime import datetime
from flask_login import UserMixin
from db.SQLiteClient import (create_user, get_user, get_user_by_email, update_user, delete_user,
get_habitLists, get_heatmap_value)
class User(UserMixin):
def __init__(self, id: int, name: str, email: str, password: str | None = None):
def __init__(self, id: int, name: str, email: str, password: str = None):
self.id = id
self.name = name
self.email = email
@ -27,29 +25,38 @@ class User(UserMixin):
user = get_user_by_email(email)
return User(user[0], user[1], user[2], user[3]) if user else None
def update(self):
update_user(self.id, self.name, self.email, self.password if self.password else None)
def delete(self):
# calls the deletion of the users habitLists
habitLists = self.get_habitLists()
for habitList in habitLists:
habitList.delete(self.id)
# deletes the user
delete_user(self.id)
# returns all habitLists assigned with the user
def get_habitLists(self) -> list:
from models.HabitList import HabitList
raw_habitLists = get_habitLists(self.id)
habitLists = []
for habitList in raw_habitLists:
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"))
habitList = HabitList(habitList[0], habitList[1], habitList[2])
habitLists.append(habitList)
return habitLists
# return the heatmap-values of the last 28 days
def get_heatmap(self) -> list:
heatmap = []
for day in range (0, 27):
for day in range (0, 28):
value = get_heatmap_value(self.id, day)
heatmap.append(value)
return heatmap