-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.py
More file actions
72 lines (56 loc) · 1.67 KB
/
student.py
File metadata and controls
72 lines (56 loc) · 1.67 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
from components import Utils
from config import STUDENTS_COLLECTION_NAME
class Student:
def __init__(self, sdk, payload, data=None, chat=None):
self.sdk = sdk
self.chat = None
self.id = None
self.name = None
self.scores = None
self.programs = None
self.collection = Utils.create_collection_name(STUDENTS_COLLECTION_NAME, payload)
if chat is not None:
self.__get(chat)
elif data is not None:
self.__fill_model(data)
def save(self):
data_to_save = {
'chat': self.chat,
'id': self.id,
'name': self.name,
'scores': self.scores,
'programs': self.programs
}
self.sdk.db.update(
# Collection name
self.collection,
# Find params
{'chat': self.chat},
# Data to be saved
data_to_save,
# Upsert = true
True
)
def remove(self):
self.sdk.db.remove(
# Collection name
self.collection,
# Find params
{'chat': self.chat}
)
def __get(self, chat):
"""
Get user data from db
:param string chat: chat_hash from payload
:return:
"""
result = self.sdk.db.find_one(self.collection, {'chat': chat})
self.__fill_model(result)
def __fill_model(self, data):
if data is None:
return
self.chat = data.get('chat')
self.id = data.get('id')
self.name = data.get('name')
self.scores = data.get('scores')
self.programs = data.get('programs')