-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
229 lines (159 loc) · 6.13 KB
/
models.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
# [START User def]
class User(object):
def __init__(self, id, firstName, lastName, email, password='', userId='', status='NEW', sysState='OPEN', dateCreated='', dateEdited='', createdBy='', editedBy=''):
self.id = id
self.firstName = firstName
self.lastName = lastName
self.email = email
self.password = password
self.userId = userId
self.status = status
self.sysState = sysState
self.dateCreated = dateCreated
self.dateEdited = dateEdited
self.createdBy = createdBy
self.editedBy = editedBy
@staticmethod
def from_dict(source):
user = User(source['id'], source['firstName'], source['lastName'], source['email'])
if 'password' in source:
user.password = source['password']
if 'userId' in source:
user.userId = source['userId']
if 'status' in source:
user.status = source['status']
if 'sysState' in source:
user.sysState = source['sysState']
if 'dateCreated' in source:
user.dateCreated = source['dateCreated']
if 'dateEdited' in source:
user.dateEdited = source['dateEdited']
if 'createdBy' in source:
user.createdBy = source['createdBy']
if 'editedBy' in source:
user.editedBy = source['editedBy']
return user
def to_dict(self, includePassword = False):
dest = {
'id' : self.id,
'firstName' : self.firstName,
'lastName' : self.lastName,
'email' : self.email,
'userId' : self.userId or '',
'status' : self.status or '',
'sysState' : self.sysState or '',
'dateCreated' : self.dateCreated or '',
'dateEdited' : self.dateEdited or '',
'createdBy' : self.createdBy or '',
'editedBy' : self.editedBy or ''
}
if includePassword:
dest['password'] = self.password
return dest
# [END User def]
# [START Option def]
class Option(object):
def __init__(self, index, option):
self.index = index
self.option = option
@staticmethod
def from_dict(source):
option = Option(source['index'], source['option'])
return option
def to_dict(self):
dest = {
'index' : self.index,
'option' : self.option
}
print('optionDict dest : ', dest)
return dest
# [END Option def]
# [START Question def]
class Question(object):
def __init__(self, question, options, correctAnswer, maxAllowedTime):
self.question = question
self.options = options
self.correctAnswer = correctAnswer
self.maxAllowedTime = maxAllowedTime
@staticmethod
def from_dict(source):
optionsArray = source['options']
options = []
for optionDict in optionsArray:
# print('optionDict : ', optionDict)
option = Option.from_dict(optionDict)
options.append(option)
# print('options : ', options)
question = Question(source['question'], options, source['correctAnswer'], source['maxAllowedTime'])
# print('question : ', question)
return question
def to_dict(self):
optionsDicts = []
for option in self.options:
optionsDicts.append(option.to_dict())
print('optionsDicts : ', optionsDicts)
dest = {
'question' : self.question,
'options' : optionsDicts,
'correctAnswer' : self.correctAnswer,
'maxAllowedTime' : self.maxAllowedTime or ''
}
return dest
# [END Question def]
# [START Subject def]
class Subject(object):
def __init__(self, id, subject, questions, status='NEW', sysState='OPEN', dateCreated='', dateEdited='', createdBy='', editedBy=''):
self.id = id
self.subject = subject
self.questions = questions
self.status = status
self.sysState = sysState
self.dateCreated = dateCreated
self.dateEdited = dateEdited
self.createdBy = createdBy
self.editedBy = editedBy
@staticmethod
def from_dict(source):
questionsArray = source['questions']
questions = []
for questionDict in questionsArray:
print('questionDict : ', questionDict)
question = Question.from_dict(questionDict)
questions.append(question)
# print('questions : ', questions)
# print('---- source : ', source)
subject = Subject(source['id'], source['subject'], questions)
if 'status' in source:
subject.status = source['status']
if 'sysState' in source:
subject.sysState = source['sysState']
if 'dateCreated' in source:
subject.dateCreated = source['dateCreated']
if 'dateEdited' in source:
subject.dateEdited = source['dateEdited']
if 'createdBy' in source:
subject.createdBy = source['createdBy']
if 'editedBy' in source:
subject.editedBy = source['editedBy']
print('subject : ', subject)
return subject
def to_dict(self):
print('---- self : ', self)
print('---- questions : ', self.questions)
questionsDicts = []
for question in self.questions:
questionsDicts.append(question.to_dict())
print('---- questionsDicts : ', questionsDicts)
dest = {
'id' : self.id,
'subject' : self.subject,
'questions' : questionsDicts,
'status' : self.status or '',
'sysState' : self.sysState or '',
'dateCreated' : self.dateCreated or '',
'dateEdited' : self.dateEdited or '',
'createdBy' : self.createdBy or '',
'editedBy' : self.editedBy or ''
}
return dest
# [END Subject def]