models.py
2.86 KB
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
from sqlalchemy import Table, Column, ForeignKey, Integer, Float, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
# ===========================================================================
# Declare ORM
Base = declarative_base()
# ---------------------------------------------------------------------------
class Student(Base):
__tablename__ = 'students'
id = Column(String, primary_key=True)
name = Column(String)
password = Column(String)
# ---
tests = relationship('Test', back_populates='student')
questions = relationship('Question', back_populates='student')
def __repr__(self):
return 'Student:\n id: "{0}"\n name: "{1}"\n password: "{2}"'.format(self.id, self.name, self.password)
# ---------------------------------------------------------------------------
class Test(Base):
__tablename__ = 'tests'
id = Column(Integer, primary_key=True) # auto_increment
ref = Column(String)
title = Column(String) # FIXME depends on ref and should come from another table...
grade = Column(Float)
state = Column(String) # ONGOING, FINISHED, QUIT, NULL
comment = Column(String)
starttime = Column(String)
finishtime = Column(String)
filename = Column(String)
student_id = Column(String, ForeignKey('students.id'))
# ---
student = relationship('Student', back_populates='tests')
questions = relationship('Question', back_populates='test')
def __repr__(self):
return 'Test:\n\
id: "{}"\n\
ref="{}"\n\
title="{}"\n\
grade="{}"\n\
state="{}"\n\
comment="{}"\n\
starttime="{}"\n\
finishtime="{}"\n\
filename="{}"\n\
student_id="{}"\n'.format(self.id, self.ref, self.title, self.grade, self.state, self.comment, self.starttime, self.finishtime, self.filename, self.student_id)
# ---------------------------------------------------------------------------
class Question(Base):
__tablename__ = 'questions'
id = Column(Integer, primary_key=True) # auto_increment
ref = Column(String)
grade = Column(Float)
starttime = Column(String)
finishtime = Column(String)
student_id = Column(String, ForeignKey('students.id'))
test_id = Column(String, ForeignKey('tests.id'))
# ---
student = relationship('Student', back_populates='questions')
test = relationship('Test', back_populates='questions')
def __repr__(self):
return 'Question:\n\
id: "{}"\n\
ref: "{}"\n\
grade: "{}"\n\
starttime: "{}"\n\
finishtime: "{}"\n\
student_id: "{}"\n\
test_id: "{}"\n'.fotmat(self.id, self.ref, self.grade, self.starttime, self.finishtime, self.student_id, self.test_id)
# ---------------------------------------------------------------------------