-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_db.py
58 lines (38 loc) · 1.47 KB
/
access_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
#!/usr/bin/env python3
import sqlite3
MEMBERS_DB = 'members.db'
members_con = sqlite3.connect(MEMBERS_DB)
members_cursor = members_con.cursor()
ID2ACCOUNT_DB = 'id2account.db'
idmap_con = sqlite3.connect(ID2ACCOUNT_DB)
idmap_cursor = idmap_con.cursor()
#
# assuming: Twitter accounts
#
def get_logged_in_ids():
members_cursor.execute("SELECT id FROM members WHERE loggedin=1")
return list(map(lambda id: id[0], members_cursor.fetchall()))
def id2account(student_id):
idmap_cursor.execute("SELECT account FROM idmap WHERE id=:id", {'id': student_id})
account = idmap_cursor.fetchone()
return (student_id[:4] + "****") if account is None else ("@" + account[0])
def get_logged_in_accounts():
return [id2account(id) for id in get_logged_in_ids()]
def logout_all_members():
members_cursor.execute("UPDATE members SET loggedin = 0");
members_con.commit()
def register_account(student_id, account):
idmap_cursor.execute("INSERT INTO idmap (id, account) VALUES (?,?)", (student_id, account))
idmap_con.commit()
def unregister_account(student_id):
idmap_cursor.execute("DELETE FROM idmap WHERE id=:id", {'id': student_id})
idmap_con.commit()
def get_accounts():
return idmap_cursor.execute("SELECT * FROM idmap").fetchall()
if __name__ == '__main__':
print(get_logged_in_ids())
accounts = get_logged_in_accounts()
if not accounts:
print("404 No one logged in")
else:
print("200 {}".format(" ".join(accounts)))