This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_module.py
103 lines (88 loc) · 2.92 KB
/
storage_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# storage_module.py
import sqlite3
from datetime import datetime
from habit import Habit
import os
DB_PATH = 'data/habits.db'
def initialize_database():
"""Initializes the database and tables."""
if not os.path.exists('data'):
os.makedirs('data')
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS habits (
name TEXT PRIMARY KEY,
periodicity TEXT,
creation_date TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS completions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
habit_name TEXT,
completion_date TEXT,
FOREIGN KEY(habit_name) REFERENCES habits(name)
)
''')
conn.commit()
conn.close()
def save_habit(habit):
"""Inserts or updates a habit in the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO habits (name, periodicity, creation_date)
VALUES (?, ?, ?)
''', (habit.name, habit.periodicity, habit.creation_date.isoformat()))
conn.commit()
conn.close()
def delete_habit(name):
"""Deletes a habit from the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('DELETE FROM habits WHERE name = ?', (name,))
cursor.execute('DELETE FROM completions WHERE habit_name = ?', (name,))
conn.commit()
conn.close()
def load_habits():
"""Loads all habits from the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('SELECT name, periodicity, creation_date FROM habits')
habits = cursor.fetchall()
habits_data = []
for name, periodicity, creation_date in habits:
cursor.execute(
'SELECT completion_date FROM completions WHERE habit_name = ?', (name,))
completions = cursor.fetchall()
completion_dates = [datetime.fromisoformat(
row[0]) for row in completions]
habits_data.append({
'name': name,
'periodicity': periodicity,
'creation_date': datetime.fromisoformat(creation_date),
'completion_dates': completion_dates
})
conn.close()
return habits_data
def save_completion(habit):
"""Records a completion event."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO completions (habit_name, completion_date)
VALUES (?, ?)
''', (habit.name, habit.completion_dates[-1].isoformat()))
conn.commit()
conn.close()
def save_completion_manual(habit, date):
"""Records a completion event for a given date."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO completions (habit_name, completion_date)
VALUES (?, ?)
''', (habit.name, date.isoformat()))
conn.commit()
conn.close()