class UserSocialAccount(db.Model): id = db.Column(db.Integer, primary_key=True) OAuth_key = db.Column(db.String(128), unique=True) service = db.Column(db.String(32), nullable=False) username = db.Column(db.String, db.ForeignKey('user.username'), nullable=True) def __repr__(self): return f'Social account {self.service} by {self.username}'
class TestmeRightAnswer(db.Model): """Right-answer model""" __tablename__ = 'testme_right_answer' id = db.Column(db.Integer, primary_key=True) content = db.Column(db.String(128), nullable=False) question_id = db.Column(db.Integer, db.ForeignKey('testme_question.id'), nullable=False) def __repr__(self): return f'Number of right answer to question №{self.question_id}'
class CustomTestme(db.Model): """Testme model""" __tablename__ = 'custom_testme' __searchable__ = ['title'] id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), unique=True, nullable=True) description = db.Column(db.Text, nullable=False) date_created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) count_questions = db.Column(db.Integer, nullable=False) questions = db.relationship('TestmeQuestion', backref='testme_questions', lazy=True) answers = db.relationship('TestmeAnswer', backref='testme_answers', lazy=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) execution_count = db.Column(db.Integer, nullable=False, default=0) user_testme = db.relationship('UserTestme', backref='user_testme', lazy=True) def __repr__(self): return f'Test №{self.id}: {self.title}'
class TestmeQuestion(db.Model): """Model of testme question""" __tablename__ = 'testme_question' id = db.Column(db.Integer, primary_key=True) testme_id = db.Column(db.Integer, db.ForeignKey('custom_testme.id'), nullable=False) title = db.Column(db.String(128), unique=False, nullable=False) answers = db.relationship('TestmeAnswer', uselist=False, backref='question_answers', lazy=True) right_answer = db.relationship('TestmeRightAnswer', uselist=False, backref='right_answer', lazy=True) user_question = db.relationship('UserTestmeAnswer', backref='user_answers', lazy=True)
class UserProfile(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) username = db.Column(db.String(32), unique=True, nullable=False) photo = db.Column(db.String(64), nullable=True, default='default.jpg') first_name = db.Column(db.String(64), nullable=True) last_name = db.Column(db.String(64), nullable=True) birth_date = db.Column(db.Date, nullable=True) about = db.Column(db.Text, nullable=True) def __repr__(self): return f'Profile {self.username}'
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), unique=True, nullable=False) email = db.Column(db.String(64), unique=True, nullable=False) photo = db.Column(db.String(20), nullable=True, default='default.jpg') password = db.Column(db.String(60), nullable=False) created_tests = db.relationship('CustomTestme', backref='created_tests_author', lazy=True) profile = db.relationship('UserProfile', uselist=False, backref='user_profile') comments = db.relationship('Comment', backref='user_comment', lazy=True) passed_testme_list = db.relationship('UserTestme', backref='passed_testme_list', lazy=True) user_testme_answer = db.relationship('UserTestmeAnswer', backref='user_testme_answer') social_account = db.relationship('UserSocialAccount', backref='user_social_account', lazy=True) def __repr__(self): return f'{self.username}' def create_profile(self, *args, **kwargs): profile = UserProfile(user_id=self.id, username=self.username) if not profile: db.session.add(profile) db.session.commit()
class UserTestmeAnswer(db.Model): id = db.Column(db.Integer, primary_key=True) content = db.Column(db.String(128), nullable=False) user_username = db.Column(db.String(64), db.ForeignKey('user.username')) user_testme_id = db.Column(db.Integer, db.ForeignKey('user_testme.id')) question_id = db.Column(db.Integer, db.ForeignKey('testme_question.id')) right_or_not = db.Column(db.Boolean, nullable=False) def __repr__(self): return f'Answer on question №{self.id}: {self.content}'
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) testme_id = db.Column(db.Integer, db.ForeignKey('custom_testme.id')) author = db.Column(db.String(32), nullable=False) time_stamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) content = db.Column(db.Text, nullable=False) def __repr__(self): return f'Comment by {self.author} for testme №{self.testme_id}'
class TestmeAnswer(db.Model): """Variable of answers model""" __tablename__ = 'testme_answer' id = db.Column(db.Integer, primary_key=True) answer_1 = db.Column(db.String(128), nullable=False) answer_2 = db.Column(db.String(128), nullable=False) answer_3 = db.Column(db.String(128), nullable=False) answer_4 = db.Column(db.String(128), nullable=False) testme_id = db.Column(db.Integer, db.ForeignKey('custom_testme.id'), nullable=False) question_id = db.Column(db.Integer, db.ForeignKey('testme_question.id'), nullable=False) def __repr__(self): return f'Answers of question №{self.question_id}.'
class UserTestme(db.Model): __tablename__ = 'user_testme' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) username = db.Column(db.String(64), nullable=False) passed_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) right_answer_count = db.Column(db.Integer, nullable=False) result = db.Column(db.Float, nullable=False) testme_id = db.Column(db.Integer, db.ForeignKey('custom_testme.id')) answer_list = db.relationship('UserTestmeAnswer', backref='user_testme_answer_list', lazy=True) def __repr__(self): return f'Test passed by {self.username}'