-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
53 lines (42 loc) · 1.71 KB
/
Copy pathdatabase.py
File metadata and controls
53 lines (42 loc) · 1.71 KB
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
import sqlite3
import os
def get_db_connection():
"""Get database connection from the Docker volume.
Enables ON DELETE CASCADE so deleting a goal automatically removes its
rows in goal_history and iteration_history.
"""
data_dir = 'data'
os.makedirs(data_dir, exist_ok=True)
db_path = os.path.join(data_dir, 'mindbaboon.db')
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA foreign_keys = ON")
return conn
def get_setting(key):
conn = get_db_connection()
setting = conn.execute("SELECT value FROM settings WHERE key = ?", (key,)).fetchone()
conn.close()
return setting["value"] if setting else None
def set_setting(key, value):
conn = get_db_connection()
conn.execute("""
INSERT INTO settings (key, value)
VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value
""", (key, str(value)))
conn.commit()
conn.close()
def get_iteration_slot():
"""Return dict {weekday:0-6, hour:0-23, minute:0-59} for global iteration window."""
from config import DEFAULT_ITERATION_SLOT
weekday = int(get_setting("iteration_weekday") or DEFAULT_ITERATION_SLOT["weekday"])
hour = int(get_setting("iteration_hour") or DEFAULT_ITERATION_SLOT["hour"])
minute = int(get_setting("iteration_minute") or DEFAULT_ITERATION_SLOT["minute"])
return {"weekday": weekday, "hour": hour, "minute": minute}
def set_iteration_slot(weekday=None, hour=None, minute=None):
if weekday is not None:
set_setting("iteration_weekday", int(weekday))
if hour is not None:
set_setting("iteration_hour", int(hour))
if minute is not None:
set_setting("iteration_minute", int(minute))