Compare commits
No commits in common. "ae0e0cf907402f5d50929232ea25fb7660b9576a" and "aaeb04d4abd8d0f0060490e772b9fa24e0a29c82" have entirely different histories.
ae0e0cf907
...
aaeb04d4ab
@ -45,8 +45,7 @@ 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}' "
|
query = f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' WHERE id = {id};"
|
||||||
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()
|
||||||
@ -58,10 +57,12 @@ 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 users WHERE id = {id};"
|
query = f"DELETE FROM habit_lists WHERE (SELECT list_id FROM habit_users WHERE user_id = {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
|
||||||
@ -70,8 +71,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) "
|
query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) VALUES ('{list_id}', "
|
||||||
f"VALUES ('{list_id}', '{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
|
f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
|
||||||
conn = con3()
|
conn = con3()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
@ -167,8 +168,7 @@ 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}' "
|
query = f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' WHERE id = {id};"
|
||||||
f"WHERE id = {id};")
|
|
||||||
conn = con3()
|
conn = con3()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
@ -178,12 +178,10 @@ 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()
|
||||||
|
|
||||||
@ -266,37 +264,6 @@ 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};"
|
||||||
@ -317,6 +284,16 @@ 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:
|
||||||
|
|||||||
@ -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 (create_habit, get_habit, update_habit, delete_habit, get_next_slot, get_slots, update_slot,
|
from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \
|
||||||
get_habitTrackings_by_habit_id, get_habitList)
|
get_habitTrackings_by_habit_id, get_slots, update_habit, get_habitList
|
||||||
|
|
||||||
|
|
||||||
# Unit wird als Integer wie folgt gemessen:
|
# Unit wird als Integers wie folgt gemessen:
|
||||||
# 0: Tag
|
# 0: Tag
|
||||||
# 1: Woche (Default)
|
# 1: Woche (Default)
|
||||||
# 2: Monat
|
# 2: Monat
|
||||||
@ -36,7 +36,9 @@ class Habit:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def get(id: int):
|
def get(id: int):
|
||||||
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
|
habit = 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)
|
||||||
@ -67,17 +69,13 @@ 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:
|
def get_habitTrackings(self) -> list[HabitTrackings]:
|
||||||
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
|
||||||
@ -106,3 +104,11 @@ 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
|
||||||
|
|||||||
@ -1,10 +1,9 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import date, 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
|
||||||
@ -12,19 +11,24 @@ 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)
|
return HabitList(id, name, description, datetime.now(), datetime.now())
|
||||||
|
|
||||||
@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 get_habits(self) -> list:
|
def delete(self):
|
||||||
|
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:
|
||||||
@ -33,7 +37,7 @@ class HabitList:
|
|||||||
|
|
||||||
return habits
|
return habits
|
||||||
|
|
||||||
def get_users(self) -> list:
|
def get_users(self):
|
||||||
raw_users = get_users(self.id)
|
raw_users = get_users(self.id)
|
||||||
users = []
|
users = []
|
||||||
for user in raw_users:
|
for user in raw_users:
|
||||||
@ -41,14 +45,3 @@ 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)
|
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
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
|
||||||
|
|
||||||
|
|
||||||
@ -6,16 +8,17 @@ 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):
|
def create(habit_id: int, times: int):
|
||||||
id = create_habitTrackings(habit_id)
|
id = create_habitTrackings(habit_id)
|
||||||
return HabitTrackings(id, habit_id)
|
return HabitTrackings(id, habit_id, datetime.now())
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get(id: int):
|
def get(id: int):
|
||||||
habitTrackings = get_habitTrackings(id)
|
habitTrackings = get_habitTrackings(id)
|
||||||
return HabitTrackings(habitTrackings[0], habitTrackings[1]) if habitTrackings else None
|
return HabitTrackings(habitTrackings[0], habitTrackings[1], datetime.strptime(habitTrackings[2], "%Y-%m-%dT%H:%M:%S.%f")) if habitTrackings else None
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
delete_habitTrackings(self.id)
|
delete_habitTrackings(self.id)
|
||||||
|
|||||||
@ -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, update_user, delete_user,
|
from db.SQLiteClient import create_user, get_user, get_user_by_email, delete_user, update_user, \
|
||||||
get_habitLists, get_heatmap_value)
|
get_habitLists, get_heatmap_value
|
||||||
|
|
||||||
|
|
||||||
class User(UserMixin):
|
class User(UserMixin):
|
||||||
@ -31,12 +31,9 @@ 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) -> list:
|
def get_habitLists(self):
|
||||||
from models.HabitList import HabitList
|
from models.HabitList import HabitList
|
||||||
|
|
||||||
raw_habitLists = get_habitLists(self.id)
|
raw_habitLists = get_habitLists(self.id)
|
||||||
@ -47,7 +44,7 @@ class User(UserMixin):
|
|||||||
|
|
||||||
return habitLists
|
return habitLists
|
||||||
|
|
||||||
def get_heatmap(self) -> list:
|
def get_heatmap(self):
|
||||||
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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user