forked from Sajad321/MCQsWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
186 lines (138 loc) · 4.54 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
import os
from sqlalchemy import Column, String, Integer, create_engine, ForeignKey
from flask_sqlalchemy import SQLAlchemy
import json
from flask_migrate import Migrate
db = SQLAlchemy()
'''
setup_db(app)
binds a flask application and a SQLAlchemy service
'''
def setup_db(app):
app.config.from_object('config')
db.app = app
db.init_app(app)
migrate = Migrate(app, db)
return db
'''
Stage
'''
class Grade(db.Model):
__tablename__ = 'Grade'
id = Column(Integer, primary_key=True, autoincrement=True)
num = Column(Integer)
semesters = db.relationship('Semester', backref=db.backref('grade_S', uselist=False), lazy='dynamic')
modules = db.relationship('Module', backref=db.backref('grade_M', uselist=False), lazy='dynamic')
def __init__(self, num):
self.num = num
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'stage_num': self.num
}
'''
Semester
'''
class Semester(db.Model):
__tablename__ = 'Semester'
id = Column(Integer, primary_key=True, autoincrement=True)
num = Column(Integer)
grade_id = Column(Integer, ForeignKey('Grade.id'), nullable=False)
modules = db.relationship('Module', backref=db.backref('semester', uselist=False), lazy='dynamic')
def __init__(self, num, grade_id):
self.num = num
self.grade_id = grade_id
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'grade_id': self.grade_id,
'semester_num': self.num
}
'''
Module
'''
class Module(db.Model):
__tablename__ = 'Module'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String, nullable=False)
grade_id = Column(Integer, ForeignKey('Grade.id'), nullable=False)
semester_id = Column(Integer, ForeignKey('Semester.id'), nullable=False)
mcqs = db.relationship('MCQ', backref=db.backref('module', uselist=False), lazy='dynamic')
def __init__(self, name, grade_id, semester_id):
self.name = name
self.grade_id = grade_id
self.semester_id = semester_id
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'grade_id': self.grade_id,
'semester_id': self.semester_id,
'name': self.name
}
'''
MCQ
'''
class MCQ(db.Model):
__tablename__ = 'MCQs'
id = Column(Integer, primary_key=True, autoincrement=True)
question = Column(String, nullable=False)
choice_A = Column(String, nullable=False)
choice_B = Column(String, nullable=False)
choice_C = Column(String, nullable=False)
choice_D = Column(String, nullable=False)
choice_E = Column(String)
answer = Column(String, nullable=False)
module_id = Column(Integer, ForeignKey('Module.id'), nullable=False)
def __init__(self, question, choice_A, choice_B, choice_C, choice_D, choice_E, answer, module_id):
self.question = question
self.choice_A = choice_A
self.choice_B = choice_B
self.choice_C = choice_C
self.choice_D = choice_D
self.choice_E = choice_E
self.answer = answer
self.module_id = module_id
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'question': self.question,
'choice_A': self.choice_A,
'choice_B': self.choice_B,
'choice_C': self.choice_C,
'choice_D': self.choice_D,
'choice_E': self.choice_E,
'answer': self.answer,
'module_id': self.module_id
}