-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
97 lines (83 loc) · 3.35 KB
/
db.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
import sqlite3
import datetime
from functools import wraps
def init_tables():
connection = sqlite3.connect('app.db')
cursor = connection.cursor()
cursor.execute(f'''CREATE TABLE IF NOT EXISTS users (
id integer primary key,
username varchar(20) unique not null,
password varchar(128) not null,
email varchar(40) unique not null
)''')
cursor.execute(f'''CREATE TABLE IF NOT EXISTS tasks (
id integer primary key,
user_id integer not null,
title varchar(40) unique not null,
content text,
start_date date not null default CURRENT_DATE,
end_date date,
status boolean default 0
)''')
connection.close()
def db_connection(func):
@wraps(func)
def wrapper(*args, **kwargs):
with sqlite3.connect('app.db') as connection:
cursor = connection.cursor()
func(*args, cursor=cursor, **kwargs)
connection.commit()
return wrapper
@db_connection
def add_user(username, email, password, **kwargs):
cursor = kwargs["cursor"]
cursor.execute('''insert into users (username, password, email)
values (?, ?, ?)''', [username, password, email])
def login(email, password):
with sqlite3.connect('app.db') as connection:
cursor = connection.cursor()
cursor.execute('select username from users where email = ? and password = ?',
[email, password])
user = cursor.fetchone()
return user
def get_user_id(name):
with sqlite3.connect('app.db') as connection:
cursor = connection.cursor()
cursor.execute('select id from users where username = ?', [name])
user_id = cursor.fetchone()
return user_id[0]
@db_connection
def add_task(user_id, title, content, end_date, **kwargs):
cursor = kwargs["cursor"]
cursor.execute('''insert into tasks (user_id, title, content, end_date)
values (?, ?, ?, ?)''', [user_id, title, content, end_date])
def get_tasks(name):
user_id = get_user_id(name)
with sqlite3.connect('app.db') as connection:
cursor = connection.cursor()
cursor.execute('select * from tasks where user_id = ?', [user_id])
user_tasks = cursor.fetchall()
return user_tasks # the reason why you can't use a decorator
@db_connection
def change_task_status(name, task_id, **kwargs):
user_id = get_user_id(name)
cursor = kwargs["cursor"]
cursor.execute('select * from tasks where user_id = ?', [user_id])
user_tasks = cursor.fetchall()
task_id = int(task_id.split('_')[-1]) - 1 # card_id
global_id = user_tasks[task_id][0]
current_status = user_tasks[task_id][6]
print(f'user_tasks: {user_tasks}\ntask_id: {task_id}\nglobal_id: {global_id}')
cursor.execute('update tasks set status = ? where id = ?',
[not current_status, global_id])
@db_connection
def delete_task(name, task_id, **kwargs):
user_id = get_user_id(name)
cursor = kwargs["cursor"]
cursor.execute('select * from tasks where user_id = ?', [user_id])
user_tasks = cursor.fetchall()
task_id = int(task_id.split('_')[-1]) - 1
global_id = user_tasks[task_id][0]
cursor.execute('delete from tasks where user_id = ? and id = ?',
[user_id, global_id])
init_tables()