-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
168 lines (133 loc) · 5.59 KB
/
database.py
File metadata and controls
168 lines (133 loc) · 5.59 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sqlite3
import os
from errors import *
from task import Task
from datetime import datetime
from argon2 import PasswordHasher
class ToDoDB:
@staticmethod
def __connect():
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
resources_dir = os.path.join(BASE_DIR, "resources")
if not os.path.exists(resources_dir):
os.makedirs(resources_dir)
DB_PATH = os.path.join(resources_dir, "todo.db")
connect = sqlite3.connect(DB_PATH)
connect.row_factory = sqlite3.Row
connect.execute('''
CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task TEXT,
reminderDatetime TEXT DEFAULT (datetime('now', 'localtime')),
done INTEGER NOT NULL DEFAULT 0,
reminded INTEGER NOT NULL DEFAULT 0
)
''')
return connect
@staticmethod
def addTask(task: Task):
if not isinstance(task, Task):
raise TypeError("task must be an object of class Task")
with ToDoDB.__connect() as conn:
cursor = conn.execute(
"INSERT INTO todo(task,reminderDatetime,done,reminded) VALUES(?, ?,?,?)",
(task.text,task.reminderDatetime,task.done,task.reminded)
)
task.id = cursor.lastrowid
@staticmethod
def deleteTask(task: Task):
with ToDoDB.__connect() as conn:
cursor = conn.execute("DELETE FROM todo WHERE id = ?", (task.id,))
if cursor.rowcount == 0:
raise TaskNotFound(f"Task {task.id} not found.")
@staticmethod
def toggleDone(task: Task):
with ToDoDB.__connect() as conn:
cursor = conn.execute("UPDATE todo SET done = 1 - done WHERE id = ?", (task.id,))
if cursor.rowcount == 0:
raise TaskNotFound(f"Task ID {task.id} not found.")
@staticmethod
def updateTask(task: Task):
with ToDoDB.__connect() as conn:
cursor = conn.execute(
"UPDATE todo SET task = ?, datetime=? , done = ? WHERE id = ?",
(task.text,task.reminderDatetime, task.done, task.id)
)
if cursor.rowcount == 0:
raise TaskNotFound(f"Task {task.text} not found.")
@staticmethod
def toggleReminded(task: Task):
with ToDoDB.__connect() as conn:
cursor = conn.execute(
"UPDATE todo SET reminded = 1-reminded WHERE id = ?",
(task.id,)
)
if cursor.rowcount == 0:
raise TaskNotFound(f"Task {task.text} not found.")
@staticmethod
def readToDoDB():
tasks = []
with ToDoDB.__connect() as conn:
cursor = conn.execute("SELECT * FROM todo")
for row in cursor.fetchall():
tasks.append(Task(id=row['id'],text=row['task'], reminderDatetime=datetime.fromisoformat(row['reminderDatetime']) ,done=bool(row['done']),reminded=bool(row['reminded'])))
return tasks
class Users:
@staticmethod
def __connect():
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
resources_dir = os.path.join(BASE_DIR, "resources")
if not os.path.exists(resources_dir):
os.makedirs(resources_dir)
DB_PATH = os.path.join(resources_dir, "users.db")
connect = sqlite3.connect(DB_PATH)
connect.row_factory = sqlite3.Row
connect.execute('''
CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password TEXT NOT NULL,
email TEXT NOT NULL
)
''')
return connect
@staticmethod
def addUser(username:str,password:str,email:str):
try:
ph=PasswordHasher()
password=ph.hash(password)
with Users.__connect() as conn:
cursor = conn.execute(
"INSERT INTO users(username, password,email) VALUES(?, ?,?)",
(username,password,email)
)
except sqlite3.IntegrityError as e:
raise UserAlreadyExists
@staticmethod
def deleteUser(username:str):
with Users.__connect() as conn:
cursor = conn.execute("DELETE FROM users WHERE username = ?", (username,))
if cursor.rowcount == 0:
raise TaskNotFound(f"Task {username} not found.")
@staticmethod
def changePassword(username:str,password:str):
ph=PasswordHasher()
password=ph.hash(password)
with Users.__connect() as conn:
cursor = conn.execute("UPDATE users SET password = ? WHERE username = ?", (password,username))
if cursor.rowcount == 0:
raise TaskNotFound(f"Task ID {username} not found.")
@staticmethod
def changeEmail(username:str,email:str):
with Users.__connect() as conn:
cursor = conn.execute("UPDATE users SET email = ? WHERE username = ?", (email,username))
if cursor.rowcount == 0:
raise UserNotFound(f"{username} not found.")
@staticmethod
def account(username:str,password:str):
with Users.__connect() as conn:
ph=PasswordHasher()
cursor=conn.execute("SELECT password FROM users WHERE username=?",(username,)).fetchone()
if not cursor:
raise UserNotFound("User Not Found")
ph.verify(cursor[0],password)
#return cursor