class Question(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.Text, nullable=False, index=True) created = db.Column(db.Float, default=int(now())) answers = db.relationship('Answers', lazy=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # group_id = db.Column(db.Integer, nullable=True) def asdict(self, with_created=True): a = dict(id=self.id, text=self.text) if with_created: a['created'] = self.created return a def mixed_answers(self): answers = [a.asdict(with_correct=False) for a in self.answers] return mix(answers) def only_corrects(self): corrects = [] for answer in self.answers: if answer.correct: corrects.append(answer.id) return corrects
class Test(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False, index=True) description = db.Column(db.Text, nullable=True) owner = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) questions = db.relationship('Question', secondary=test_questions, backref=db.backref('tests', lazy=True), lazy=True) competitions = db.relationship('Competition', lazy=True) def asdict(self): return dict(id=self.id, title=self.title, description=self.description) def mixed_questions(self): mixed = [] for question in self.questions: q_dict = question.asdict(with_created=False) q_dict['answers'] = question.mixed_answers() mixed.append(q_dict) return mix(mixed) def corrects(self): resp = {q.id: q.only_corrects() for q in self.questions} return resp
class Submission(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) competition_id = db.Column(db.Integer, db.ForeignKey('competition.id')) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) answer_id = db.Column(db.Integer, nullable=True) def asdict(self): return dict(question=self.question_id, answer=self.answer_id)
class Group(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False, index=True) description = db.Column(db.Text, nullable=True) # TODO: many-to-many field, group_id -> array # group_id = db.Column(db.Integer, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) questions = db.relationship('Question', secondary=questions_groups) users = db.relationship('User', secondary=users_groups) def asdict(self): return dict(id=self.id, title=self.title, description=self.description)
class Answers(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.Text, nullable=False) correct = db.Column(db.Boolean, default=False) question_id = db.Column(db.Integer, db.ForeignKey('question.id'), nullable=False) def asdict(self, with_correct=True): a = dict(id=self.id, text=self.text) if with_correct: a['correct'] = self.correct return a
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(), unique=True, nullable=False, index=True) email = db.Column(db.String(), unique=True, nullable=False, index=True) pwd_hash = db.Column(db.String(), nullable=False) registered = db.Column(db.Float, default=now()) questions = db.relationship('Question', backref='user', lazy=True) tests = db.relationship('Test', lazy=True) competitions = db.relationship('Competition', secondary=competition_participants, lazy=True) admin = db.Column(db.Boolean, default=False) manager = db.Column(db.Boolean, default=False) groups = db.relationship('Group', lazy=True) first_name = db.Column(db.String(20), nullable=False) last_name = db.Column(db.String(20), nullable=False) # group_id = db.Column(db.Integer, nullable=True) @property def password(self): raise AttributeError('`password` is not a readable attribute') @password.setter def password(self, password): self.pwd_hash = pbkdf2_sha256.hash(password) def verify_password(self, password): return pbkdf2_sha256.verify(password, self.pwd_hash) def asdict(self): scopes = [] if self.admin: scopes.append('admin') if self.manager: scopes.append('manager') return dict(id=self.id, username=self.username, first_name=self.first_name, last_name=self.last_name, email=self.email, scopes=scopes)
class Competition(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False, index=True) test_id = db.Column(db.Integer, db.ForeignKey('test.id')) start_date = db.Column(db.Float, index=True) end_date = db.Column(db.Float, index=True) description = db.Column(db.Text, nullable=True) participants = db.relationship('User', secondary=competition_participants, backref='competition', lazy='joined') user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def asdict(self): return dict(id=self.id, title=self.title, test_id=self.test_id, start_date=self.start_date, end_date=self.end_date, description=self.description)
from testar import db from testar.utils import mix test_questions = db.Table( 'test_questions', db.Column('test_id', db.Integer, db.ForeignKey('test.id'), primary_key=True), db.Column('question_id', db.Integer, db.ForeignKey('question.id'), primary_key=True)) class Test(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False, index=True) description = db.Column(db.Text, nullable=True) owner = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) questions = db.relationship('Question', secondary=test_questions, backref=db.backref('tests', lazy=True), lazy=True) competitions = db.relationship('Competition', lazy=True) def asdict(self): return dict(id=self.id, title=self.title, description=self.description) def mixed_questions(self):
from testar import db competition_participants = db.Table( 'competition_participants', db.Column('competition', db.Integer, db.ForeignKey('competition.id'), primary_key=True), db.Column('user', db.Integer, db.ForeignKey('user.id'), primary_key=True)) class Competition(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False, index=True) test_id = db.Column(db.Integer, db.ForeignKey('test.id')) start_date = db.Column(db.Float, index=True) end_date = db.Column(db.Float, index=True) description = db.Column(db.Text, nullable=True) participants = db.relationship('User', secondary=competition_participants, backref='competition', lazy='joined') user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def asdict(self): return dict(id=self.id, title=self.title, test_id=self.test_id, start_date=self.start_date, end_date=self.end_date, description=self.description)
class GroupsGroups(db.Model): id = db.Column(db.Integer, index=True, primary_key=True) group = db.Column(db.Integer, index=True) entry = db.Column(db.Integer, index=True)
from testar import db questions_groups = db.Table( 'questions_groups', db.Column('question', db.Integer, db.ForeignKey('question.id'), primary_key=True), db.Column('group', db.Integer, db.ForeignKey('group.id'), primary_key=True)) users_groups = db.Table( 'users_groups', db.Column('user', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('group', db.Integer, db.ForeignKey('group.id'), primary_key=True)) class GroupsGroups(db.Model): id = db.Column(db.Integer, index=True, primary_key=True) group = db.Column(db.Integer, index=True) entry = db.Column(db.Integer, index=True) class Group(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False, index=True) description = db.Column(db.Text, nullable=True) # TODO: many-to-many field, group_id -> array # group_id = db.Column(db.Integer, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)