-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflash_cards.py
447 lines (382 loc) · 11.9 KB
/
flash_cards.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
from __future__ import annotations
import os
import random
import sqlite3
import stat
from flask import flash
from flask import Flask
from flask import make_response
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from flask_login import AnonymousUserMixin
from flask_login import current_user
from flask_login import login_required
from flask_login import login_user
from flask_login import LoginManager
from flask_login import logout_user
from flask_login import UserMixin
# set up flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'development key'
class AnonymousUser(AnonymousUserMixin):
def __init__(self):
self.color_scheme = '0,0,0'
self.light_scheme = 0
# set up Flask-Login
login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.init_app(app)
login_manager.anonymous_user = AnonymousUser
# store open db connections
OPEN_CONNECTIONS = {}
def connect_db(name: str) -> sqlite3.Connection:
rv = sqlite3.connect(name, check_same_thread=False)
rv.row_factory = sqlite3.Row
return rv
def get_db(name: str) -> sqlite3.Connection:
try:
return OPEN_CONNECTIONS[name]
except KeyError:
OPEN_CONNECTIONS[name] = connect_db(name)
with open('data/schema.sql') as f:
OPEN_CONNECTIONS[name].executescript(f.read())
OPEN_CONNECTIONS[name].commit()
return OPEN_CONNECTIONS[name]
def process_db_name(name: str, to_user: bool = False) -> str:
if to_user:
return name.replace('_', ' ')
return name.replace(' ', '_')
@app.template_filter()
def pretty_set_name(name: str):
return process_db_name(name, to_user=True).replace('.db', '')
@app.template_filter()
def half_pretty_set_name(name: str):
return name.replace('.db', '')
def create_hex_color():
data = [hex(int(x)) for x in current_user.color_scheme.split(',')]
return f'#{"".join(data)}'
@app.context_processor
def fill_jinja():
return {
'logged_in': current_user.is_authenticated,
'user_color': current_user.color_scheme,
'hex_user_color': create_hex_color(),
'light_scheme': current_user.light_scheme,
}
class User(UserMixin):
def __init__(
self,
id: int,
name: str,
password: str,
color_scheme: str,
light_scheme: str,
) -> None:
self.id = id
self.name = name
self.password = password
self.color_scheme = color_scheme
self.light_scheme = light_scheme
@classmethod
def get_user_by_name(cls, name: str) -> tuple[str, str]:
query = '''\
SELECT id, password, color_scheme, light_scheme
FROM users
WHERE username=?
'''
db = get_db('users.db')
user = db.execute(query, (name,)).fetchone()
print('name:', name, user, db)
if user is None:
return None
return cls(user[0], name, *user[1:])
@classmethod
def get_user_by_id(cls, id_: str) -> tuple[str, str]:
query = '''\
SELECT id, password, color_scheme, light_scheme
FROM users
WHERE id=?
'''
db = get_db('users.db')
user = db.execute(query, (id_,)).fetchone()
print('id:', id_, user, db)
if user is None:
return None
return cls(id_, *user)
@login_manager.user_loader
def load_user(id: str):
return User.get_user_by_id(id)
@app.route('/login', methods=['GET', 'POST'])
@app.route('/', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('sets'))
error = None
if request.method == 'POST':
user = User.get_user_by_name(request.form['username'])
if user is None or request.form['password'] != user.password:
error = 'Invalid username or password!'
else:
login_user(user)
resp = make_response(redirect(url_for('sets')))
resp.set_cookie('username', request.form['username'])
return resp
return render_template('login.html', error=error, thing2=True)
@app.route('/cards')
@login_required
def cards():
query = '''
SELECT *
FROM cards where tag_id=?
ORDER BY id DESC
'''
db_path = request.cookies.get('username')
db_name = request.cookies.get('current_set')
tag_id = request.args.get('current_tag_id')
db = get_db(f'{db_path}/{db_name}')
cards = db.execute(query, (tag_id,)).fetchall()
tag_name = db.execute(
'SELECT name FROM tags WHERE id=?', (tag_id,),
).fetchone()[0]
resp = make_response(render_template('cards.html', cards=cards))
resp.set_cookie('current_tag_id', tag_id)
resp.set_cookie('current_tag_name', tag_name)
return resp
@app.route('/cards/add', methods=['POST'])
@login_required
def add_card():
# TODO: consider programming_language?
db_path = request.cookies.get('username')
db_name = request.cookies.get('current_set')
tag_id = request.cookies.get('current_tag_id')
db = get_db(f'{db_path}/{db_name}')
db.execute(
'''INSERT INTO cards (type, front, back, tag_id)
VALUES (?,?,?,?)''',
(
request.form['type'], request.form['front'],
request.form['back'], tag_id,
),
)
db.commit()
flash('New card was successfully added.')
return redirect(url_for('cards', current_tag_id=tag_id))
@app.route('/cards/edit', methods=['POST'])
@login_required
def edit_card():
command = '''
UPDATE cards
SET
type = ?,
front = ?,
back = ?,
known = ?
WHERE id = ?
'''
db_path = request.cookies.get('username')
db_name = request.cookies.get('current_set')
db = get_db(f'{db_path}/{db_name}')
db.execute(
command,
[
request.form['type'],
request.form['front'],
request.form['back'],
request.form['known'],
request.form['card_id'],
],
)
db.commit()
flash('Card saved.')
return redirect(
url_for(
'cards',
current_tag_id=request.cookies.get('current_tag_id'),
),
)
@app.route('/cards/delete', methods=['POST'])
@login_required
def delete(card_id):
db_path = request.cookies.get('username')
db_name = request.cookies.get('username')
db = get_db(f'{db_path}/{db_name}')
db.execute('DELETE FROM cards WHERE id = ?', [card_id])
db.commit()
flash('Card deleted.')
return redirect(url_for('cards'))
@app.route('/learn')
@login_required
def memorize():
dickt = {
'0': '1',
'1': 'known=1',
'2': 'known=0',
}
tag_id = request.cookies.get('current_tag_id')
db_path = request.cookies.get('username')
db_name = request.cookies.get('current_set')
ltype = request.args.get('ltype')
db = get_db(f'{db_path}/{db_name}')
cards = db.execute(
'SELECT front,back,type FROM cards WHERE tag_id=? AND ?',
(tag_id, dickt[ltype]),
).fetchall()
if not cards:
flash("You've learned all cards")
return redirect(url_for('cards', current_tag_id=tag_id))
return render_template(
'memorize.html',
card=random.choice(cards),
ltype=ltype,
)
@app.route('/learned')
@login_required
def mark_known():
db_path = request.cookies.get('username')
db_name = request.cookies.get('username')
card_id = request.args.get('card_id')
ltype = request.args.get('ltype')
db = get_db(f'{db_path}/{db_name}.db')
db.execute('UPDATE cards SET known = 1 WHERE id = ?', [card_id])
db.commit()
return redirect(url_for('memorize', ltype=ltype))
@app.route('/sets')
@login_required
def sets():
db_path = request.cookies.get('username')
os.makedirs(db_path, exist_ok=True)
os.chmod(db_path, stat.S_IRWXU)
print(os.listdir(db_path), db_path)
dbs = [
f for f in os.listdir(
db_path,
) if os.path.isfile(os.path.join(db_path, f)) and f.endswith('.db')
]
resp = make_response(render_template('sets.html', dbs=dbs, thing2=True))
resp.delete_cookie('current_set')
resp.delete_cookie('current_tag_name')
resp.delete_cookie('current_tag_id')
return resp
@app.route('/sets/add', methods=['POST'])
@login_required
def add_set():
db_path = request.cookies.get('username')
db_name = request.form['name']
open(os.path.join(db_path, process_db_name(db_name) + '.db'), 'a').close()
os.chmod(
os.path.join(
db_path, process_db_name(
db_name,
) + '.db',
), stat.S_IRWXU,
)
return redirect(url_for('sets'))
@app.route('/sets/delete')
@login_required
def delete_set():
db_path = request.cookies.get('username')
db_name = request.form['name']
try:
os.remove(os.path.join(db_path, db_name + '.db'))
except OSError:
pass
return redirect(url_for('sets'))
@app.route('/sets/edit', methods=['PATCH'])
@login_required
def edit_set():
db_path = request.cookies.get('username')
old_db_name = request.form['old_name']
new_db_name = request.form['new_name']
try:
os.rename(
os.path.join(db_path, old_db_name + '.db'),
os.path.join(db_path, new_db_name + '.db'),
)
except OSError:
pass
return redirect(url_for('sets'))
@app.route('/sets/<name>')
@login_required
def set_overview(name):
db_path = request.cookies.get('username')
db_name = process_db_name(name) + '.db'
db = get_db(f'{db_path}/{db_name}')
tags = db.execute('SELECT id, name FROM tags').fetchall()
resp = make_response(render_template('set_overview.html', tags=tags))
resp.set_cookie('current_set', db_name)
resp.delete_cookie('current_tag_name')
resp.delete_cookie('current_tag_id')
return resp
@app.route('/tags/add', methods=['POST'])
@login_required
def add_tag():
db_path = request.cookies.get('username')
db_name = request.cookies.get('current_set')
db = get_db(f'{db_path}/{db_name}')
db.execute(
'INSERT INTO tags (name) VALUES (?)',
[request.form['tagName']],
)
db.commit()
return redirect(
url_for(
'set_overview',
name=process_db_name(db_name, to_user=True).replace('.db', ''),
),
)
@app.route('/tags/delete', methods=['DELETE'])
@login_required
def delete_tag():
db_path = request.cookies.get('username')
db_name = request.cookies.get('current_set')
db = get_db(f'{db_path}/{db_name}')
db.execute('Delete from tags where id =?', [request.form['tag_id']])
db.commit()
@app.route('/tags/edit', methods=['PATCH'])
@login_required
def edit_tag():
db_path = request.cookies.get('username')
db_name = request.cookies.get('current_set')
db = get_db(f'{db_path}/{db_name}')
db.execute(
'Update tags set name =? where id = ?', [
request.form['tagName'], request.form['tag_id'],
],
)
db.commit()
# @app.route('/settings')
# @login_required
# def settings():
# return render_template(
# 'settings.html',
# username=current_user.name,
# thing2=True
# )
#
#
# @app.route('/settings/change-light', methods=['POST'])
# @login_required
# def change_lightscheme():
# db_path = request.cookies.get('username')
# db_name = request.cookies.get('current_set')
# light_scheme = request.form.get('lightscheme')
# db = get_db('users.db')
# db.execute(
# 'UPDATE users SET light_scheme=? WHERE id=?',
# (light_scheme, current_user.id)
# )
# db.commit()
# return redirect(url_for('settings'))
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
if __name__ == '__main__':
try:
app.run(host='0.0.0.0', debug=True)
except BaseException:
for connection in OPEN_CONNECTIONS.values():
connection.close()