-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.py
50 lines (35 loc) · 1.25 KB
/
storage.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
from settings import SHELVE_NAME, get_filename
import shelve
def set_mentor(user_id, field, topic):
with shelve.open(get_filename(SHELVE_NAME)) as storage:
storage["m" + str(user_id) + topic] = {
"user_id": user_id,
"field": field,
"topic": topic
}
def get_mentors():
with shelve.open(get_filename(SHELVE_NAME)) as storage:
keys = []
for key in storage.keys():
if key.startswith("m"):
keys.append(key)
return [storage[k] for k in keys]
def get_user_info(user_id):
with shelve.open(get_filename(SHELVE_NAME)) as storage:
try:
return storage[str(user_id)]
except KeyError:
return None
def set_user_info(user_id, document):
with shelve.open(get_filename(SHELVE_NAME)) as storage:
storage[str(user_id)] = document
def del_user_info(user_id):
with shelve.open(get_filename(SHELVE_NAME)) as storage:
del storage[str(user_id)]
def get_users():
with shelve.open(get_filename(SHELVE_NAME)) as storage:
userids = [storage[key] for key in storage.keys()]
return userids
def clear_storage():
with shelve.open(get_filename(SHELVE_NAME)) as storage:
storage.clear()