-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
166 lines (138 loc) · 3.63 KB
/
database.py
File metadata and controls
166 lines (138 loc) · 3.63 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
from flask import g
from helper import hash_pwd, verify_hash
import sqlite3
def get_db():
"""
Connects to database;
Returns sqlite3 Row object
"""
if "db" not in g:
g.db = sqlite3.connect("database.db")
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
"""
Closes conenction with database
"""
db = g.pop("db", None)
if db is not None:
db.close()
def init_tables():
"""
Initializes all essential tables
"""
db = get_db()
db.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
)
"""
)
db.execute(
"""
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL,
content TEXT NOT NULL,
username TEXT NOT NULL,
UNIQUE(username, filename)
)
"""
)
db.commit()
def fetch_content(filename: str, username: str) -> str:
"""
Returns content of a,
by filename and username,
specified file
"""
db = get_db()
cursor = db.execute(
"SELECT content FROM files WHERE username = ? AND filename = ?",
(username, filename),
)
row = cursor.fetchone()
if row:
return row["content"]
return ""
def fetch_files(username: str) -> tuple[str]:
"""
Fetches filenames for all files
from a certain user
"""
db = get_db()
cursor = db.execute("SELECT filename FROM files WHERE username = ?", (username,))
rows = cursor.fetchall()
files: tuple[str] = tuple(r["filename"] for r in rows)
return files
def append_line(filename: str, line: str, username: str):
"""
Append/Updates a specific
"""
db = get_db()
db.execute(
"""
INSERT OR IGNORE INTO files (username, filename, content)
VALUES (?, ?, '')
""",
(username, filename),
)
db.execute(
"""
UPDATE files
SET content = COALESCE(content, '') || CHAR(10) || ?
WHERE username = ? AND filename = ?
""",
(line, username, filename),
)
db.commit()
def delete_char(filename: str, username: str):
db = get_db()
cur = db.execute(
"SELECT content FROM files WHERE filename = ? AND username = ?",
(filename, username),
)
row = cur.fetchone()
if not row:
return
content = row["content"]
if len(content) == 0:
return
content = content[:-1] # remove last character
db.execute(
"UPDATE files SET content = ? WHERE filename = ? AND username = ?",
(content, filename, username),
)
db.commit()
def delete_file(filename: str, username: str):
db = get_db()
db.execute(
"""
DELETE FROM files
WHERE filename = ? AND username = ?
""",
(filename, username),
)
db.commit()
def register_user(username: str, password: str):
db = get_db()
hashed_password = hash_pwd(password)
try:
db.execute(
"INSERT INTO users (username, password) VALUES (?, ?)",
(username, hashed_password),
)
db.commit()
return None
except sqlite3.IntegrityError:
return "Username already taken"
def login_user(username: str, password: str) -> bool:
db = get_db()
cursor = db.execute("SELECT password FROM users WHERE username = ?", (username,))
row = cursor.fetchone()
if row is None:
return False
return verify_hash(password, row["password"])