-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
314 lines (251 loc) · 12.7 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
# created 4/12/2021 by Josh Oppenheimer
# model of the database. Has constructors for handling the database information within python
# (Will change as we change that details of variables and table interaction)
from app import db, app
from flask_login import UserMixin
from werkzeug.security import generate_password_hash
# parent class of Student and Teacher, role defines which of the two they are.
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column(db.Integer(), autoincrement=True, nullable=False, primary_key=True)
role = db.Column(db.String(50), nullable=False)
name = db.Column(db.String(100), nullable=False)
username = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, unique=True)
last_login = db.Column(db.String(50))
registered_on = db.Column(db.String(50), nullable=False)
__mapper_args__ = {'polymorphic_identity': 'users', 'polymorphic_on': role}
# constructor
def __init__(self, role, name, username, password, last_login, registered_on):
self.role = role
self.name = name
self.username = username
self.password = generate_password_hash(password)
self.lastLogin = last_login
self.registered_on = registered_on
class Student(User):
__tablename__ = "student"
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
# foreign keys show one to one relationship
group_id = db.Column(db.ForeignKey('groups.id'))
# mapping relationship
__mapper_args__ = {'polymorphic_identity': 'student'}
def __init__(self, role, name, username, password, last_login, registered_on, group_id):
super().__init__(role, name, username, password, last_login, registered_on)
self.group_id = group_id
# Decryption of group codes considered, decided it was unnecessary however, implementation in future is still possible.
class Teacher(User):
__tablename__ = 'teacher'
# foreign keys show one to one relationship
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
email = db.Column(db.String(60), unique=True)
# one (teacher) to many (groups) relationship
group = db.relationship('Group', uselist=True)
# mapping relationship
__mapper_args__ = {'polymorphic_identity': 'teacher'}
def __init__(self, role, name, username, password, last_login, registered_on, email):
super().__init__(role, name, username, password, last_login, registered_on)
self.email = email
class Group(db.Model):
__tablename__ = 'groups'
id = db.Column(db.String(6), nullable=False, primary_key=True, unique=True)
name = db.Column(db.String(100), nullable=False)
size = db.Column(db.Integer(), default=50, nullable=False)
teacher_id = db.Column(db.ForeignKey('teacher.id'))
key_stage = db.Column(db.ForeignKey('key_stage.key_stage'))
# one (group) to many (student) relationship
students = db.relationship('Student', backref='groups')
def __init__(self, id, name, size, teacher_id, key_stage):
self.id = id
self.name = name
self.size = size
self.teacher_id = teacher_id
self.key_stage = key_stage
class Quiz(db.Model):
__tablename__ = 'quizzes'
id = db.Column(db.Integer(), autoincrement=True, nullable=False, primary_key=True)
name = db.Column(db.String(), nullable=False)
key_stage = db.Column(db.ForeignKey('key_stage.key_stage'))
# many (questions) to one (quiz)
questions = db.relationship('Question', backref='user')
def __init__(self, name, key_stage):
self.name = name
self.key_stage = key_stage
class Question(db.Model):
__tablename__ = 'questions'
id = db.Column(db.Integer(), autoincrement=True, nullable=False, primary_key=True)
quiz_id = db.Column(db.ForeignKey('quizzes.id'))
question_text = db.Column(db.String(), nullable=False)
choices = db.Column(db.String(), nullable=False)
correct_choice = db.Column(db.Integer(), nullable=False)
def __init__(self, quiz_id, question_text, choices, correct_choice):
self.quiz_id = quiz_id
self.question_text = question_text
self.choices = choices
self.correct_choice = correct_choice
class StudentQuizScores(db.Model):
__tablename__ = 'student_quiz_scores'
id = db.Column(db.Integer(), autoincrement=True, nullable=False, primary_key=True)
quiz_id = db.Column(db.ForeignKey('quizzes.id'))
student_id = db.Column(db.ForeignKey('student.id'))
score = db.Column(db.Integer()) # percentile score
# one (quiz) to many (scores)
quiz = db.relationship('Quiz', foreign_keys='StudentQuizScores.quiz_id')
# one (student) to many (scores)
student = db.relationship('Student', foreign_keys='StudentQuizScores.student_id')
def __init__(self, quiz_id, student_id, score):
self.quiz_id = quiz_id
self.student_id = student_id
self.score = score
# will likely be expanded to contain more data in future development
class KeyStage(db.Model):
__tablename__ = 'key_stage'
id = db.Column(db.Integer(), nullable=False, primary_key=True)
key_stage = db.Column(db.String(), nullable=False)
def __init__(self, key_stage):
self.key_stage = key_stage
def create_models():
""" Function to define testing instances """
questions, quizzes = [], []
teacher = Teacher(role="teacher",
name="Adam Smith",
username="AdamSmith3412",
password="Testing123",
last_login=None,
registered_on="19/12/2021 00:55:11",
email="[email protected]")
group = Group(id="453153",
name="Class 4",
size=30,
teacher_id=None,
key_stage=1)
student = Student(role="student",
name="James Newsome",
username="JamesNewsome5412",
password="TestTestTest5412",
last_login=None,
registered_on="19/12/2021 00:55:11",
group_id=group.id)
quizzes.append(Quiz(name="Weather and Climate",
key_stage=1))
questions.append(Question(quiz_id=1,
question_text="What is Weather?",
choices="If it's raining or not"
"|The daily conditions in a small area"
"|The average temperature"
"|The number of clouds in the sky",
correct_choice=1))
questions.append(Question(quiz_id=1,
question_text="What is Climate?",
choices="The long term weather pattern over a large area"
"|The average temperature of the world"
"|The weather at the end of the year"
"|The weather over a large area",
correct_choice=0))
questions.append(Question(quiz_id=1,
question_text="What is the Climate like in the United Kingdom?",
choices="Cool summers and warm winters"
"|Very hot and windy all year"
"|Cool winters and warm summers"
"|Very Cold with no rain all year",
correct_choice=2))
questions.append(Question(quiz_id=1,
question_text="What is the Climate like in the desert?",
choices="Cold with lots of snowfall"
"|Hot and lots of rainfall"
"|Cold and windy"
"|Very hot and dry all year",
correct_choice=3))
questions.append(Question(quiz_id=1,
question_text="What is the Climate like in the rainforest",
choices="Hot all year and very wet"
"|Hot and dry"
"|Cold and lots of rainfall"
"|Very hot and windy",
correct_choice=0))
quizzes.append(Quiz(name="Climate Change",
key_stage=2))
questions.append(Question(quiz_id=2,
question_text="What is Climate Change?",
choices="Describes the weather changing each day"
"|Describes the earth moving further away from the sun"
"|When the seasons change, for example: spring to summer"
"|Describes how the planets climates change over a long period of time ",
correct_choice=3))
questions.append(Question(quiz_id=2,
question_text="What is Global Warming?",
choices="Describes the change in the Earth's average temperature over a long period"
"|Describes the increase in the number of clouds around the world"
"|The cooling of the Earth and increase of rainfall"
"|Describes the increase in human body temperature over time",
correct_choice=0))
questions.append(Question(quiz_id=2,
question_text="Which of these is not a greenhouse gas?",
choices="Water Vapour"
"|Acid"
"|Carbon Dioxide"
"|Methane",
correct_choice=1))
questions.append(Question(quiz_id=2,
question_text="Why is it important that the levels of greenhouse gases in our atmosphere "
"remain roughly the same?",
choices="So that we can breath more easily"
"|To maintain a stable climate"
"|To help keep us cool"
"|To keep the sky blue",
correct_choice=1))
questions.append(Question(quiz_id=2,
question_text="What is causing the sharp rise in carbon dioxide levels in our "
"atmosphere today?",
choices="The Earth's natural cycle"
"|Human actions such as burning fossil fuels, farming and deforestation"
"|Sun spots and volcanic activity"
"|The sun getting hotter",
correct_choice=0))
key_stages = [KeyStage(key_stage=1), KeyStage(key_stage=2)]
student_quiz_score0 = StudentQuizScores(quiz_id=1,
student_id=2,
score=80)
student_quiz_score1 = StudentQuizScores(quiz_id=2,
student_id=2,
score=60)
admin = Teacher(role="teacher",
name="admin",
username="admin",
password="Unguessable12*",
last_login=None,
registered_on="19/12/2021 00:55:11",
email="[email protected]")
admin_group = Group(id="-1",
name="admin group",
size=1,
teacher_id=admin.id,
key_stage=0)
db.session.add(teacher)
db.session.add(admin)
db.session.commit()
db.session.add(group)
db.session.add(admin_group)
db.session.add(student)
for key_stage in key_stages:
db.session.add(key_stage)
for quiz in quizzes:
db.session.add(quiz)
for question in questions:
db.session.add(question)
db.session.add(student_quiz_score0)
db.session.add(student_quiz_score1)
group.teacher_id = teacher.id
admin_group.teacher_id = admin.id
db.session.commit()
def init_db():
""" Database table creation using models """
db.init_app(app)
with app.app_context():
db.drop_all()
db.create_all()
create_models()
db.session.commit()
print(f"\n===== Sucessfully created {db} ======")
if __name__ == "__main__":
init_db()