2024-01-30 11:17:34 +01:00
|
|
|
from datetime import datetime, timedelta
|
2024-01-12 10:57:58 +01:00
|
|
|
import hashlib
|
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def con3():
|
|
|
|
|
conn = sqlite3.connect('db/db.sqlite')
|
|
|
|
|
return conn
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 08:11:22 +01:00
|
|
|
### User ###
|
2024-02-18 04:53:15 +01:00
|
|
|
def create_user(name: str, email: str, password: str, profile_image: str):
|
2024-01-12 16:53:03 +01:00
|
|
|
password = hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
|
now = datetime.now().isoformat()
|
2024-02-18 04:53:15 +01:00
|
|
|
query = (f"INSERT INTO users (name, email, password, profile_image, created_at, updated_at) VALUES "
|
|
|
|
|
f"('{name}', '{email}', '{password}', '{profile_image}', '{now}', '{now}');")
|
2024-01-12 10:57:58 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
2024-01-12 16:53:03 +01:00
|
|
|
def get_user(id: int):
|
|
|
|
|
query = f"SELECT * FROM users WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
user = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_by_email(email: str):
|
|
|
|
|
query = f"SELECT * FROM users WHERE email = '{email}';"
|
2024-01-12 10:57:58 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
user = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return user
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
|
2024-02-18 04:53:15 +01:00
|
|
|
def update_user(id: int, name: str, email: str, password: str, profile_image:str):
|
2024-02-02 09:20:19 +01:00
|
|
|
now = datetime.now().isoformat()
|
2024-01-26 10:40:12 +01:00
|
|
|
if password:
|
2024-02-18 04:53:15 +01:00
|
|
|
query = (f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', "
|
|
|
|
|
f"profile_image = '{profile_image}', updated_at = '{now}' WHERE id = {id};")
|
2024-01-26 10:40:12 +01:00
|
|
|
else:
|
2024-02-18 04:53:15 +01:00
|
|
|
query = (f"UPDATE users SET name = '{name}', email = '{email}', profile_image = '{profile_image}',"
|
|
|
|
|
f" updated_at = '{now}' WHERE id = {id};")
|
2024-01-26 08:10:36 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
2024-01-26 10:40:12 +01:00
|
|
|
conn.commit()
|
2024-01-26 08:10:36 +01:00
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
2024-01-17 11:23:22 +01:00
|
|
|
def delete_user(id: int):
|
2024-02-14 21:03:11 +01:00
|
|
|
query = f"DELETE FROM users WHERE id = {id};"
|
2024-01-17 11:23:22 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
2024-01-26 08:10:36 +01:00
|
|
|
return cursor.lastrowid
|
2024-01-17 11:23:22 +01:00
|
|
|
|
|
|
|
|
|
2024-02-16 08:11:22 +01:00
|
|
|
### Habit ###
|
|
|
|
|
def create_habit(list_id: int, name: str, note: str, times: int, unit: int, slot: int):
|
2024-01-13 13:30:29 +01:00
|
|
|
now = datetime.now().isoformat()
|
2024-02-14 12:55:00 +01:00
|
|
|
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}');")
|
2024-01-13 13:30:29 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
2024-01-23 10:32:14 +01:00
|
|
|
|
2024-01-26 09:29:47 +01:00
|
|
|
def get_habit(id: int):
|
|
|
|
|
query = f"SELECT * FROM habits WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habit = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habit
|
|
|
|
|
|
|
|
|
|
|
2024-02-12 21:07:55 +01:00
|
|
|
def get_habits(list_id: int):
|
|
|
|
|
query = f"SELECT * FROM habits WHERE list_id = {list_id};"
|
2024-01-26 09:29:47 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habits = cursor.fetchall()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habits
|
|
|
|
|
|
|
|
|
|
|
2024-01-30 11:17:34 +01:00
|
|
|
def get_heatmap_value(user_id: int, days: int):
|
|
|
|
|
date = (datetime.now() - timedelta(days=days)).date()
|
|
|
|
|
print(date)
|
2024-02-12 22:06:27 +01:00
|
|
|
|
|
|
|
|
# Uses JOINs to get all Habits
|
|
|
|
|
query = (f"SELECT habits.id FROM habits "
|
|
|
|
|
f"JOIN habit_lists ON habits.list_id = habit_lists.id "
|
|
|
|
|
f"JOIN habit_users ON habit_lists.id = habit_users.list_id "
|
|
|
|
|
f"WHERE habit_users.user_id = {user_id};")
|
|
|
|
|
|
|
|
|
|
# Uses JOINs to get all checked Habits on a specific date
|
|
|
|
|
query2 = (f"SELECT habits.id FROM habits "
|
|
|
|
|
f"JOIN habit_lists ON habits.list_id = habit_lists.id "
|
|
|
|
|
f"JOIN habit_users ON habit_lists.id = habit_users.list_id "
|
|
|
|
|
f"JOIN habit_trackings ON habits.id = habit_trackings.habit_id "
|
|
|
|
|
f"WHERE habit_users.user_id = {user_id} AND DATE(habit_trackings.created_at) = '{date}';")
|
|
|
|
|
|
2024-01-30 11:17:34 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
all_habits = cursor.fetchall()
|
|
|
|
|
cursor.execute(query2)
|
|
|
|
|
checked_habits = cursor.fetchall()
|
|
|
|
|
conn.close()
|
2024-02-12 22:06:27 +01:00
|
|
|
|
|
|
|
|
# Calculate the percentage of checked Habits
|
2024-02-13 11:13:50 +01:00
|
|
|
count = len(all_habits)
|
|
|
|
|
count2 = len(checked_habits)
|
2024-02-12 22:06:27 +01:00
|
|
|
if count > 0:
|
|
|
|
|
return int(count2 / count * 100)
|
|
|
|
|
else:
|
|
|
|
|
return 0
|
2024-01-30 11:17:34 +01:00
|
|
|
|
|
|
|
|
|
2024-02-12 21:07:55 +01:00
|
|
|
def get_next_slot(list_id: int):
|
|
|
|
|
query = f"SELECT slot FROM habits WHERE list_id = {list_id} ORDER BY slot DESC LIMIT 1;"
|
2024-01-19 10:44:00 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
slot = cursor.fetchone()
|
|
|
|
|
conn.close()
|
2024-02-13 10:46:54 +01:00
|
|
|
return slot[0] + 1 if slot else 1
|
2024-01-26 08:30:06 +01:00
|
|
|
|
|
|
|
|
|
2024-02-12 21:07:55 +01:00
|
|
|
def get_slots(list_id: int):
|
|
|
|
|
query = f"SELECT id, slot FROM habits WHERE list_id = {list_id} ORDER BY slot;"
|
2024-01-26 09:29:47 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
slots = cursor.fetchall()
|
|
|
|
|
conn.close()
|
|
|
|
|
return slots
|
2024-01-19 10:44:00 +01:00
|
|
|
|
2024-01-13 13:30:29 +01:00
|
|
|
|
2024-02-02 08:30:34 +01:00
|
|
|
def update_slot(id: int, slot: int):
|
2024-02-02 09:20:19 +01:00
|
|
|
now = datetime.now().isoformat()
|
2024-02-02 09:28:36 +01:00
|
|
|
query = f"UPDATE habits SET slot = {slot}, updated_at = '{now}' WHERE id = {id};"
|
2024-01-13 13:30:29 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
2024-02-02 08:30:34 +01:00
|
|
|
conn.commit()
|
2024-01-13 13:30:29 +01:00
|
|
|
conn.close()
|
2024-01-26 09:29:47 +01:00
|
|
|
return cursor.lastrowid
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
|
2024-02-02 08:30:34 +01:00
|
|
|
def update_habit(id: int, name: str, note: str, times: int, unit: int):
|
2024-02-02 09:20:19 +01:00
|
|
|
now = datetime.now().isoformat()
|
2024-02-14 12:55:00 +01:00
|
|
|
query = (f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' "
|
|
|
|
|
f"WHERE id = {id};")
|
2024-01-13 13:30:29 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
2024-02-02 08:30:34 +01:00
|
|
|
conn.commit()
|
2024-01-13 13:30:29 +01:00
|
|
|
conn.close()
|
2024-01-26 09:29:47 +01:00
|
|
|
return cursor.lastrowid
|
2024-01-17 11:23:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_habit(id: int):
|
2024-02-16 17:57:49 +01:00
|
|
|
query = f"DELETE FROM habits WHERE id = {id};"
|
2024-01-17 11:23:22 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
2024-01-23 10:32:14 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
### HabitTracking ###
|
|
|
|
|
def create_habitTracking(habit_id: int):
|
2024-01-23 10:32:14 +01:00
|
|
|
now = datetime.now().isoformat()
|
2024-02-13 11:13:50 +01:00
|
|
|
query = f"INSERT INTO habit_trackings (habit_id, created_at) VALUES ('{habit_id}','{now}');"
|
2024-01-23 10:32:14 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
def get_habitTracking(id: int):
|
2024-01-23 10:32:14 +01:00
|
|
|
query = f"SELECT * FROM habit_trackings WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habit_tracking = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habit_tracking
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
def get_habitTrackings(habit_id: int):
|
2024-01-26 09:06:47 +01:00
|
|
|
query = f"SELECT * FROM habit_trackings WHERE habit_id = {habit_id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habit_trackings = cursor.fetchall()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habit_trackings
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
def delete_habitTracking(id: int):
|
2024-01-23 10:32:14 +01:00
|
|
|
query = f"DELETE FROM habit_trackings WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
2024-01-30 11:17:34 +01:00
|
|
|
|
|
|
|
|
|
2024-02-16 17:57:49 +01:00
|
|
|
### HabitList ###
|
2024-02-12 21:07:55 +01:00
|
|
|
def create_habitList(user_id: int, name: str, description: str):
|
|
|
|
|
now = datetime.now().isoformat()
|
2024-02-13 11:13:50 +01:00
|
|
|
query = (f"INSERT INTO habit_lists (name, description, created_at, updated_at) "
|
|
|
|
|
f"VALUES ('{name}', '{description}', '{now}', '{now}');")
|
2024-02-12 21:07:55 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
2024-02-13 11:13:50 +01:00
|
|
|
query2 = (f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at)"
|
|
|
|
|
f" VALUES ('{user_id}', '{cursor.lastrowid}', '{now}', '{now}');")
|
|
|
|
|
cursor.execute(query2)
|
2024-02-12 21:07:55 +01:00
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_habitList(id: int):
|
2024-02-12 22:06:27 +01:00
|
|
|
query = f"SELECT * FROM habit_lists WHERE id = {id};"
|
2024-02-12 21:07:55 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habit_list = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habit_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_habitLists(user_id: int):
|
2024-02-13 11:13:50 +01:00
|
|
|
query = (f"SELECT habit_lists.* FROM habit_lists JOIN habit_users ON habit_lists.id = habit_users.list_id "
|
|
|
|
|
f"WHERE habit_users.user_id = {user_id};")
|
2024-02-12 21:07:55 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habit_lists = cursor.fetchall()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habit_lists
|
|
|
|
|
|
|
|
|
|
|
2024-02-14 12:55:00 +01:00
|
|
|
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):
|
2024-02-12 21:07:55 +01:00
|
|
|
now = datetime.now().isoformat()
|
2024-02-14 12:55:00 +01:00
|
|
|
query = (f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at)"
|
|
|
|
|
f" VALUES ('{user_id}', '{list_id}', '{now}', '{now}');")
|
2024-02-12 21:07:55 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
2024-02-14 12:55:00 +01:00
|
|
|
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};"
|
2024-02-12 21:07:55 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
2024-02-14 12:55:00 +01:00
|
|
|
def update_habitList(id: int, name: str, description: str):
|
|
|
|
|
now = datetime.now().isoformat()
|
|
|
|
|
query = f"UPDATE habit_lists SET name = {name}, description = {description}, updated_at = '{now}' WHERE id = {id};"
|
2024-02-12 22:31:51 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
2024-02-14 12:55:00 +01:00
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_habitList(id: int):
|
|
|
|
|
query = f"DELETE FROM habit_lists WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
2024-02-12 22:31:51 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
2024-01-30 11:17:34 +01:00
|
|
|
if __name__ == "__main__":
|
2024-02-02 08:30:34 +01:00
|
|
|
habits = get_habits(1)
|
|
|
|
|
for habit in habits:
|
|
|
|
|
print(habit[6])
|