-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
499 lines (435 loc) · 17.4 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True, nullable=False)
encrypted_password = db.Column(db.String(255), nullable=False)
name = db.Column(db.String(255), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now)
def __init__(self, email, encrypted_password, name):
self.email = email
self.encrypted_password = encrypted_password
self.name = name
def asdict(self):
return {
'id': self.id,
'email': self.email,
'encrypted_password': self.encrypted_password,
'name': self.name,
'created_at': self.created_at,
}
def get_encrypted_password(self):
return self.encrypted_password
def set_encrypted_password(self, encrypted_password):
self.encrypted_password = encrypted_password
class Staff(User):
__tablename__ = 'staffs'
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
quizzes = db.relationship('Quiz', backref='staff')
def __init__(self, user):
self.email = user.email
self.encrypted_password = user.encrypted_password
self.name = user.name
def asdict(self):
return {
'id': self.id,
'email': self.email,
'encrypted_password': self.encrypted_password,
'name': self.name,
'created_at': self.created_at,
'count_quizzes': len(self.quizzes),
'quizzes': [q.to_json() for q in self.quizzes]
}
def asdict_courseMng(self):
return {
'id': self.id,
'email': self.email,
'name': self.name,
'count_courses': len(self.courses),
'courses': [c.to_json() for c in self.courses],
'quizzes': [q.asdict() for q in self.quizzes]
}
class Student(User):
__tablename__ = 'students'
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
matriculation_number = db.Column(db.String(255), unique=True, nullable=False)
questions_attempts = db.relationship('QuestionAttempt', backref='student')
quiz_attempts = db.relationship('QuizAttempt', backref='student')
def __init__(self, user, matriculation_number):
self.email = user.email
self.encrypted_password = user.encrypted_password
self.name = user.name
self.matriculation_number = matriculation_number
def asdict(self):
return {
'id': self.id,
'email': self.email,
'encrypted_password': self.encrypted_password,
'name': self.name,
'created_at': self.created_at,
'matriculation_number': self.matriculation_number
}
def asdict_courseMng(self):
return {
'id': self.id,
'email': self.email,
'name': self.name,
'count_courses': len(self.courses),
'courses': [c.to_json() for c in self.courses]
}
class Course(db.Model):
__tablename__ = 'courses'
index = db.Column(db.String(255), primary_key=True)
created_at = db.Column(db.DateTime, default=datetime.now)
def __init__(self, index):
self.index = index
def asdict(self):
return {
'index': self.index,
'created_at': self.created_at
}
class Topic(db.Model):
__tablename__ = 'topics'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now)
lessons = db.relationship('Lesson', backref='topic')
def __init__(self, name):
self.name = name
def asdict(self):
return {
'id': self.id,
'name': self.name,
'created_at': self.created_at
}
class Lesson(db.Model):
__tablename__ = 'lessons'
topic_id = db.Column(db.Integer, db.ForeignKey('topics.id'), primary_key=True)
id = db.Column(db.Integer, primary_key=True, nullable=True)
name = db.Column(db.String(255), nullable=False)
content = db.Column(db.Text, nullable=False)
url_link = db.Column(db.String)
created_at = db.Column(db.DateTime, default=datetime.now)
questions = db.relationship('Question', backref='lesson')
def __init__(self, topic_id, id, name, content, url_link=None):
self.topic_id = topic_id
self.id = id
self.name = name
self.content = content
self.url_link = url_link
def asdict(self):
return {
'topic_id': self.topic_id,
'id': self.id,
'name': self.name,
'content': self.content,
'url_link': self.url_link,
'created_at': self.created_at,
'count_questions': len(self.questions),
'questions': [q.asdict() for q in self.questions]
}
class Question(db.Model):
__tablename__ = 'questions'
id = db.Column(db.Integer, primary_key=True)
topic_id = db.Column(db.Integer)
lesson_id = db.Column(db.Integer)
description = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now)
__table_args__ = (db.ForeignKeyConstraint([topic_id, lesson_id], [Lesson.topic_id, Lesson.id]),{})
choices = db.relationship('QuestionChoice', backref='question')
attempts = db.relationship('QuestionAttempt', backref='question')
def __init__(self, topic_id, lesson_id, description):
self.topic_id = topic_id
self.lesson_id = lesson_id
self.description = description
def asdict(self):
return {
'id': self.id,
'topic_id': self.topic_id,
'lesson_id': self.lesson_id,
'description': self.description,
'created_at': self.created_at,
'count_choices': len(self.choices),
'choices': [z.to_json() for z in self.choices],
'count_attempts': len(self.attempts),
'attempts': [qa.asdict() for qa in self.attempts]
}
class QuestionChoice(db.Model):
__tablename__ = 'questionchoices'
question_id = db.Column(db.Integer, db.ForeignKey('questions.id'), primary_key=True)
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.Text, nullable=False)
is_correct = db.Column(db.Boolean, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now)
def __init__(self, question_id, id, description, is_correct):
self.question_id = question_id
self.id = id
self.description = description
self.is_correct = is_correct
def asdict(self):
return {
'question_id': self.question_id,
'id': self.id,
'description': self.description,
'is_correct': self.is_correct,
'created_at': self.created_at
}
def to_json(self):
return {
'id': self.id,
'description': self.description,
'is_correct': self.is_correct,
'created_at': self.created_at
}
class Quiz(db.Model):
__tablename__ = 'quizzes'
id = db.Column(db.Integer, primary_key=True)
staff_id = db.Column(db.Integer, db.ForeignKey('staffs.id'))
name = db.Column(db.String(255), nullable=False)
is_fast = db.Column(db.Boolean, nullable=False)
date_start = db.Column(db.Date, nullable=False)
date_end = db.Column(db.Date, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now)
attempts = db.relationship('QuizAttempt', backref='quiz')
challenges = db.relationship('Challenge', backref='quiz')
def __init__(self, staff_id, name, is_fast, date_start, date_end):
self.name = name
self.staff_id = staff_id
self.is_fast = is_fast
self.date_start = date_start
self.date_end = date_end
def asdict(self):
return {
'id': self.id,
'name': self.name,
'staff': {
'id': self.staff_id,
'name': self.staff.name
},
'is_fast': self.is_fast,
'date_start': self.date_start,
'date_end': self.date_end,
'attempts': [qa.asdict() for qa in self.attempts]
}
def to_json(self):
return {
'id': self.id,
'name': self.name,
'is_fast': self.is_fast,
'date_start': self.date_start,
'date_end': self.date_end,
'created_at': self.created_at
}
def asdict_courseMng(self):
return {
'id': self.id,
'staff_id': self.staff_id,
'name': self.name,
'is_fast': self.is_fast,
'date_start': self.date_start,
'date_end': self.date_end,
'count_attempts': len(self.attempts),
'count_courses': len(self.courses),
'courses': [c.asdict() for c in self.courses]
}
def asdict_questionMng(self):
return {
'id': self.id,
'staff_id': self.staff_id,
'name': self.name,
'is_fast': self.is_fast,
'date_start': self.date_start,
'date_end': self.date_end,
'count_attempts': len(self.attempts),
'count_questions': len(self.questions),
'questions': [q.asdict_getQuestion() for q in self.questions]
}
class QuestionAttempt(db.Model):
__tablename__ = 'questionattempts'
id = db.Column(db.Integer, primary_key=True)
student_id = db.Column(db.Integer, db.ForeignKey('students.id'), nullable=False)
question_id = db.Column(db.Integer, db.ForeignKey('questions.id'), nullable=False)
is_correct = db.Column(db.Boolean, nullable=False)
duration_ms = db.Column(db.Integer, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now)
def __init__(self, student_id, question_id, is_correct, duration_ms):
self.student_id = student_id
self.question_id = question_id
self.is_correct = is_correct
self.duration_ms = duration_ms
def asdict(self):
return {
'id': self.id,
'student_id': self.student_id,
'question_id': self.question_id,
'is_correct': self.is_correct,
'duration_ms': self.duration_ms,
'created_at': self.created_at
}
class QuizAttempt(db.Model):
__tablename__ = 'quizattempts'
id = db.Column(db.Integer, primary_key=True)
student_id = db.Column(db.Integer, db.ForeignKey('students.id'))
quiz_id = db.Column(db.Integer, db.ForeignKey('quizzes.id'))
score = db.Column(db.Integer, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now)
def __init__(self, student_id, quiz_id, score):
self.student_id = student_id
self.quiz_id = quiz_id
self.score = score
def asdict(self):
return {
'id': self.id,
'student_id': self.student_id,
'quiz_id': self.quiz_id,
'score': self.score,
'created_at': self.created_at
}
class Challenge(db.Model):
__tablename__ = 'challenges'
from_student_id = db.Column(db.Integer, db.ForeignKey('students.id'), primary_key=True)
to_student_id = db.Column(db.Integer, db.ForeignKey('students.id'), primary_key=True)
quiz_id = db.Column(db.Integer, db.ForeignKey('quizzes.id'), primary_key=True)
winner_id = db.Column(db.Integer)
is_completed = db.Column(db.Boolean, default=False)
created_at = db.Column(db.DateTime, default=datetime.now)
__table_args__ = (
db.UniqueConstraint('from_student_id', 'to_student_id', 'quiz_id'),
)
from_person = db.relationship('Student', backref='challenge_received', foreign_keys=[from_student_id])
to_person = db.relationship('Student', backref='challenge_sent', foreign_keys=[to_student_id])
def __init__(self, from_student_id, to_student_id, quiz_id):
self.from_student_id = from_student_id
self.to_student_id = to_student_id
self.quiz_id = quiz_id
self.winner_id = None
def asdict(self):
return {
'from_student_id': self.from_student_id,
'to_student_id': self.to_student_id,
'quiz_id': self.quiz_id,
'is_completed': self.is_completed,
'winner_id': self.winner_id,
'created_at': self.created_at
}
class Rs_staff_course_teach(db.Model):
__tablename__ = 'rs_staff_course_teaches'
staff_id = db.Column(db.Integer, db.ForeignKey('staffs.id'), primary_key=True)
course_index = db.Column(db.String(255), db.ForeignKey('courses.index'), primary_key=True)
created_at = db.Column(db.DateTime, default=datetime.now)
staff = db.relationship('Staff', backref=db.backref('courses', cascade="all, delete-orphan"))
course = db.relationship('Course', backref=db.backref('staffs', cascade="all, delete-orphan"))
def __init__(self, staff_id, course_index):
self.staff_id = staff_id
self.course_index = course_index
def asdict(self):
return {
'staff_id': self.staff_id,
'course_index': self.course_index,
'created_at': self.created_at
}
def to_json(self):
return {
'course_index': self.course_index,
'created_at': self.created_at
}
class Rs_student_course_enrol(db.Model):
__tablename__ = 'rs_student_course_enrols'
student_id = db.Column(db.Integer, db.ForeignKey('students.id'), primary_key=True)
course_index = db.Column(db.String(255), db.ForeignKey('courses.index'), primary_key=True)
created_at = db.Column(db.DateTime, default=datetime.now)
student = db.relationship('Student', backref=db.backref('courses', cascade="all, delete-orphan"))
course = db.relationship('Course', backref=db.backref('students', cascade="all, delete-orphan"))
def __init__(self, student_id, course_index):
self.student_id = student_id
self.course_index = course_index
def asdict(self):
return {
'student_id': self.student_id,
'course_index': self.course_index,
'created_at': self.created_at
}
def to_json(self):
return {
'course_index': self.course_index,
'created_at': self.created_at
}
class Rs_quiz_course_assign(db.Model):
__tablename__ = 'rs_quiz_course_assigns'
quiz_id = db.Column(db.Integer, db.ForeignKey('quizzes.id'), primary_key=True)
course_index = db.Column(db.String(255), db.ForeignKey('courses.index'), primary_key=True)
created_at = db.Column(db.DateTime, default=datetime.now)
quiz = db.relationship('Quiz', backref=db.backref('courses', cascade="all, delete-orphan"))
course = db.relationship('Course', backref=db.backref('quizzes', cascade="all, delete-orphan"))
def __init__(self, quiz_id, course_index):
self.quiz_id = quiz_id
self.course_index = course_index
def asdict(self):
return {
'quiz_id': self.quiz_id,
'course_index': self.course_index,
'created_at': self.created_at
}
class Rs_quiz_question_contain(db.Model):
__tablename__ = 'rs_quiz_question_contains'
quiz_id = db.Column(db.Integer, db.ForeignKey('quizzes.id'), primary_key=True)
question_id = db.Column(db.Integer, db.ForeignKey('questions.id'), primary_key=True)
created_at = db.Column(db.DateTime, default=datetime.now)
quiz = db.relationship('Quiz', backref=db.backref('questions', cascade="all, delete-orphan"))
question = db.relationship('Question', backref=db.backref('quizzes', cascade="all, delete-orphan"))
def __init__(self, quiz_id, question_id):
self.quiz_id = quiz_id
self.question_id = question_id
def asdict(self):
return {
'quiz_id': self.quiz_id,
'question_id': self.question_id,
'created_at': self.created_at
}
def asdict_getQuestion(self):
return {
'question': self.question.asdict(),
'created_at': self.created_at
}
class Rs_lesson_quiz_contain(db.Model):
__tablename__ = 'rs_lesson_quiz_contains'
id = db.Column(db.Integer, primary_key=True, nullable=False)
topic_id = db.Column(db.Integer)
lesson_id = db.Column(db.Integer)
quiz_id = db.Column(db.Integer)
__table_args__ = (
db.ForeignKeyConstraint(
[topic_id, lesson_id],
[Lesson.topic_id, Lesson.id]
),
)
__table_args__ = (
db.ForeignKeyConstraint(
[quiz_id],
[Quiz.id]
),
)
__table_args__ = (
db.UniqueConstraint('topic_id', 'lesson_id', 'quiz_id'),
)
created_at = db.Column(db.DateTime, default=datetime.now)
quiz = db.relationship('Quiz', backref=db.backref('lessons', cascade="all, delete-orphan"))
lesson = db.relationship('Lesson', backref=db.backref('quizzes', cascade="all, delete-orphan"))
def __init__(self, topic_id, lesson_id, quiz_id):
self.topic_id = topic_id
self.lesson_id = lesson_id
self.quiz_id = quiz_id
def asdict(self):
return {
'id': self.id,
'topic_id': self.topic_id,
'lesson_id': self.lesson_id,
'quiz_id': self.quiz_id,
'created_at': self.created_at
}
def asdict_quizMng(self):
return {
'quiz_id': self.quiz_id
}