models.py 2.86 KB


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)


# ---------------------------------------------------------------------------