Fixing
This commit is contained in:
parent
ae0e0cf907
commit
7600bd8f2c
@ -8,7 +8,7 @@ def con3():
|
|||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
### User.py ###
|
### User ###
|
||||||
def create_user(name: str, email: str, password: str):
|
def create_user(name: str, email: str, password: str):
|
||||||
password = hashlib.sha256(password.encode()).hexdigest()
|
password = hashlib.sha256(password.encode()).hexdigest()
|
||||||
now = datetime.now().isoformat()
|
now = datetime.now().isoformat()
|
||||||
@ -42,7 +42,7 @@ def get_user_by_email(email: str):
|
|||||||
return user
|
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()
|
now = datetime.now().isoformat()
|
||||||
if password:
|
if password:
|
||||||
query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' "
|
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
|
return cursor.lastrowid
|
||||||
|
|
||||||
|
|
||||||
### Habit.py ###
|
### Habit ###
|
||||||
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, note: str, times: int, unit: int, slot: int):
|
||||||
now = datetime.now().isoformat()
|
now = datetime.now().isoformat()
|
||||||
query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) "
|
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}');")
|
f"VALUES ('{list_id}', '{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
|
||||||
|
|||||||
@ -7,11 +7,11 @@ from db.SQLiteClient import (create_habit, get_habit, update_habit, delete_habit
|
|||||||
get_habitTrackings_by_habit_id, get_habitList)
|
get_habitTrackings_by_habit_id, get_habitList)
|
||||||
|
|
||||||
|
|
||||||
# Unit wird als Integer wie folgt gemessen:
|
# unit will be represented by integers like this:
|
||||||
# 0: Tag
|
# 0: day
|
||||||
# 1: Woche (Default)
|
# 1: week (default)
|
||||||
# 2: Monat
|
# 2: month
|
||||||
# 3: Jahr
|
# 3: year
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Habit:
|
class Habit:
|
||||||
@ -28,9 +28,9 @@ class Habit:
|
|||||||
self.fill_statistics()
|
self.fill_statistics()
|
||||||
|
|
||||||
@staticmethod
|
@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)
|
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)
|
return Habit(id, list_id, name, note, times, unit, slot)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -38,46 +38,52 @@ class Habit:
|
|||||||
habit = get_habit(id)
|
habit = get_habit(id)
|
||||||
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
|
||||||
|
|
||||||
def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
|
|
||||||
update_habit(self.id, name, note, times, unit)
|
def update(self):
|
||||||
if name is not None:
|
update_habit(self.id, self.name, self.note, self.times, self.unit)
|
||||||
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_slot(self, new_slot: int):
|
def update_slot(self, new_slot: int):
|
||||||
|
# Fetches a list with the following structure [(id, slot), (id, slot), ...]
|
||||||
slots = get_slots(self.list_id)
|
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:
|
for slot in slots:
|
||||||
update_slot(slot[0], slot[1]-1)
|
update_slot(slot[0], slot[1]-1)
|
||||||
if new_slot < self.slot:
|
if new_slot < self.slot: # Example self.slot=4 new_slot=1
|
||||||
slots = slots[new_slot-1:self.slot-1]
|
slots = slots[new_slot-1:self.slot-1] # Expected list: [(id, 1), (id, 2), (id, 3)]
|
||||||
for slot in slots:
|
for slot in slots:
|
||||||
update_slot(slot[0], slot[1]+1)
|
update_slot(slot[0], slot[1]+1)
|
||||||
|
|
||||||
|
# Update the slot of the current habit
|
||||||
update_slot(self.id, new_slot)
|
update_slot(self.id, new_slot)
|
||||||
|
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
|
# Deletes the current habit
|
||||||
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
|
||||||
delete_habit(self.id)
|
delete_habit(self.id)
|
||||||
|
|
||||||
|
|
||||||
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_by_habit_id(self.id):
|
||||||
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1]))
|
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1]))
|
||||||
return trackings
|
return trackings
|
||||||
|
|
||||||
def habit_list(self):
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
def fill_statistics(self):
|
def fill_statistics(self):
|
||||||
count = 0
|
count = 0
|
||||||
self.checked = False
|
self.checked = False
|
||||||
@ -104,5 +110,6 @@ class Habit:
|
|||||||
|
|
||||||
self.percentage = int(count / self.times * 100)
|
self.percentage = int(count / self.times * 100)
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
from models.Habit import Habit
|
from models.Habit import Habit
|
||||||
from models.User import User
|
from models.User import User
|
||||||
@ -22,7 +21,7 @@ class HabitList:
|
|||||||
@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]) if habitList else None
|
||||||
|
|
||||||
def get_habits(self) -> list:
|
def get_habits(self) -> list:
|
||||||
raw_habits = get_habits(self.id)
|
raw_habits = get_habits(self.id)
|
||||||
|
|||||||
@ -1,12 +1,10 @@
|
|||||||
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, update_user, delete_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):
|
||||||
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.id = id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.email = email
|
self.email = email
|
||||||
@ -27,29 +25,38 @@ class User(UserMixin):
|
|||||||
user = get_user_by_email(email)
|
user = get_user_by_email(email)
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
|
# calls the deletion of the users habitLists
|
||||||
habitLists = self.get_habitLists()
|
habitLists = self.get_habitLists()
|
||||||
for habitList in habitLists:
|
for habitList in habitLists:
|
||||||
habitList.delete(self.id)
|
habitList.delete(self.id)
|
||||||
|
|
||||||
|
# deletes the user
|
||||||
delete_user(self.id)
|
delete_user(self.id)
|
||||||
|
|
||||||
|
|
||||||
|
# returns all habitLists assigned with the user
|
||||||
def get_habitLists(self) -> list:
|
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)
|
||||||
habitLists = []
|
habitLists = []
|
||||||
for habitList in raw_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)
|
habitLists.append(habitList)
|
||||||
|
|
||||||
return habitLists
|
return habitLists
|
||||||
|
|
||||||
|
|
||||||
|
# return the heatmap-values of the last 28 days
|
||||||
def get_heatmap(self) -> list:
|
def get_heatmap(self) -> list:
|
||||||
heatmap = []
|
heatmap = []
|
||||||
for day in range (0, 27):
|
for day in range (0, 28):
|
||||||
value = get_heatmap_value(self.id, day)
|
value = get_heatmap_value(self.id, day)
|
||||||
heatmap.append(value)
|
heatmap.append(value)
|
||||||
return heatmap
|
return heatmap
|
||||||
Loading…
x
Reference in New Issue
Block a user