forked from exam-of-three-people/exam_ol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
196 lines (142 loc) · 7.01 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
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config["SECRET_KEY"] = "123456"
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:qwer@localhost:3306/exam_ol_database"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
app.config["SQLALCHEMY_COMMIT_ON_TEARDOWN"] = True
db = SQLAlchemy(app)
class Student(db.Model):
__tablename__ = "tb_student"
id = db.Column(db.BigInteger, primary_key=True)
name = db.Column(db.String(10), nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
class_id = db.Column(db.Integer, db.ForeignKey('tb_class.id'), nullable=False)
subjects = db.relationship("StudentSubject", back_populates="student_association")
student_subjects = db.relationship("StudentSubject", back_populates="student")
@property
def password(self):
return AttributeError('password is not a readable attribute')
@password.setter
def password(self, password):
self.password_hash = generate_password_hash(password)
def checkPassword(self, password):
return check_password_hash(self.password_hash, password)
def get_pages(self):
pages = []
for student_subject in self.student_subjects:
for teacher_s_s in student_subject.teacher_s_ss:
pages.extend(teacher_s_s.pages)
return pages
def get_current_page(self):
for student_subject in self.student_subjects:
for teacher_s_s in student_subject.teacher_s_ss:
for page in teacher_s_s.pages:
if page.ongoing:
return page
return None
def __repr__(self):
return "[学生 %r]" % self.id
pass
class TeacherSS(db.Model):
__tablename__ = "tb_teacher_s_s"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
student_subject_id = db.Column(db.Integer, db.ForeignKey("tb_student_subject.id"), primary_key=True)
teacher_id = db.Column(db.BigInteger, db.ForeignKey("tb_teacher.id"), primary_key=True)
student_subject_association = db.relationship("StudentSubject", back_populates="teachers")
teacher_association = db.relationship("Teacher", back_populates="student_subjects")
student_subject = db.relationship("StudentSubject", back_populates="teacher_s_ss")
teacher = db.relationship("Teacher", back_populates="teacher_s_ss")
pages = db.relationship("Page", back_populates="teacher_s_s")
class StudentSubject(db.Model):
__tablename__ = "tb_student_subject"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
student_id = db.Column(db.BigInteger, db.ForeignKey("tb_student.id"), primary_key=True)
subject_id = db.Column(db.Integer, db.ForeignKey("tb_subject.id"), primary_key=True)
subject_association = db.relationship("Subject", back_populates="students")
student_association = db.relationship("Student", back_populates="subjects")
subject = db.relationship("Subject", back_populates="student_subjects")
student = db.relationship("Student", back_populates="student_subjects")
teachers = db.relationship("TeacherSS", back_populates="student_subject_association")
teacher_s_ss = db.relationship("TeacherSS", back_populates="student_subject")
class Teacher(db.Model):
__tablename__ = "tb_teacher"
id = db.Column(db.BigInteger, primary_key=True)
name = db.Column(db.String(10), nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
student_subjects = db.relationship("TeacherSS", back_populates="teacher_association")
teacher_s_ss = db.relationship("TeacherSS", back_populates="teacher")
def checkPassword(self, password):
return check_password_hash(self.password_hash, password)
def setPassword(self, password):
self.password_hash = generate_password_hash(password)
def get_pages(self):
pages = Page.query.join(Page.teacher_s_s).filter(TeacherSS.teacher_id == self.id).all()
return pages
def __repr__(self):
return "[%r老师]" % self.name
pass
class College(db.Model):
__tablename__ = "tb_college"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16), nullable=False)
majors = db.relationship("Major", backref="college")
def __repr__(self):
return "[学院 %r]" % self.name
class Major(db.Model):
__tablename__ = "tb_major"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16), nullable=False)
college_id = db.Column(db.Integer, db.ForeignKey('tb_college.id'), nullable=False)
classes = db.relationship("Class", backref="major")
def __repr__(self):
return "[专业 %r]" % self.name
class Subject(db.Model):
__tablename__ = "tb_subject"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16), nullable=False)
tests = db.relationship("Test", backref="subject")
students = db.relationship("StudentSubject", back_populates="subject_association")
student_subjects = db.relationship("StudentSubject", back_populates="subject")
def __repr__(self):
return "[学科 %r]" % self.name
class Page(db.Model):
__tablename__ = "tb_page"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=True)
structure = db.Column(db.String(128), nullable=False)
level = db.Column(db.Integer, default=2)
date = db.Column(db.Date, nullable=False)
time_start = db.Column(db.Time, nullable=False)
time_length = db.Column(db.Integer, nullable=False)
content = db.Column(db.String(1024), nullable=True)
answer = db.Column(db.Text, nullable=True)
scores = db.Column(db.String(1024), nullable=True)
ongoing = db.Column(db.Boolean, default=False)
finished = db.Column(db.Boolean, default=False)
rest_time = db.Column(db.Integer, nullable=True)
code = db.Column(db.Integer, nullable=True, default=0)
teacher_s_s_id = db.Column(db.Integer, db.ForeignKey("tb_teacher_s_s.id"))
teacher_s_s = db.relationship("TeacherSS", back_populates="pages")
def __repr__(self):
return "[试卷 %r]" % self.id
class Test(db.Model):
__tablename__ = "tb_test"
id = db.Column(db.Integer, primary_key=True)
subject_id = db.Column(db.Integer, db.ForeignKey('tb_subject.id'), nullable=False)
type_ = db.Column(db.String(50), nullable=False)
question = db.Column(db.VARCHAR(1024), nullable=False)
answer = db.Column(db.VARCHAR(1000), nullable=False)
level = db.Column(db.Integer, nullable=False)
def __repr__(self):
return "[试题 %r]" % self.id
class Class(db.Model):
__tablename__ = "tb_class"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(32), nullable=False)
grade = db.Column(db.Integer, nullable=False)
major_id = db.Column(db.Integer, db.ForeignKey('tb_major.id'), nullable=False)
students = db.relationship("Student", backref="class_")
def __repr__(self):
return "[%r]" % self.name