Compare commits

..

2 Commits

Author SHA1 Message Date
Yapollon
9ef8f3f513 Merge branch 'master' of https://repo.cimeyclust.com/CimeyClust/HabitTracker 2024-01-13 13:31:12 +01:00
Yapollon
d8c796ced7 Stylesheet for main.html
Table for Habits
2024-01-13 13:30:29 +01:00
4 changed files with 62 additions and 3 deletions

View File

@ -39,3 +39,35 @@ def get_user_by_email(email: str):
user = cursor.fetchone()
conn.close()
return user
def create_habit(name: str, times: int, note: str | None=None):
now = datetime.now().isoformat()
query = (f"INSERT INTO habits (name, note, times, created_at, updated_at) VALUES ('{name}', '{note}', "
f"'{times}', '{now}', '{now}');")
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
conn.close()
return cursor.lastrowid
def get_habits():
query = f"SELECT * FROM habits;"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
habits = cursor.fetchone()
conn.close()
return habits
def get_habit(id: int):
query = f"SELECT * FROM habits WHERE id = {id};"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
habits = cursor.fetchone()
conn.close()
return habits

View File

@ -2,8 +2,8 @@ CREATE TABLE IF NOT EXISTS habits
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
note TEXT NOT NULL,
times TEXT NOT NULL,
note TEXT NULL,
times INTEGER NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);

28
models/Habit.py Normal file
View File

@ -0,0 +1,28 @@
from dataclasses import dataclass
from db.SQLiteClient import create_habit, get_habits, get_habit
@dataclass
class Habit:
id: int
name: str
note: str
times: int
@staticmethod
def create(name: str, times: int, note: str | None=None):
id = create_habit(name, note, times)
return Habit(id, name, note, times)
@staticmethod
def get(id: int):
habit = get_habit(id)
return Habit(habit[0], habit[1], habit[2], habit[3]) if habit else None
@staticmethod
def get_all():
raw_habits = get_habits()
habits = []
for habit in raw_habits:
habit = Habit(habit[0], habit[1], habit[2], habit[3])
habits.append(habit)
return habits if habits else None

View File

@ -1,5 +1,4 @@
from flask_login import UserMixin
from db.SQLiteClient import create_user, get_user, get_user_by_email