class RequestsThemesModel(db.Model): __tablename__ = 'request_themes' id = db.Column(db.Text(length=36), default=lambda: str(uuid.uuid4()), primary_key=True) tag = db.Column(db.String(80), unique=True, nullable=False) requests = db.relationship("RequestModel", lazy="dynamic", cascade="all, delete-orphan") @classmethod def find_by_id(cls, _id: int) -> "RequestsThemesModel": return cls.query.filter_by(id=_id).first() @classmethod def find_by_tag(cls, tag: str) -> "RequestsThemesModel": return cls.query.filter_by(tag=tag).first() def turn_to_json(self) -> dict: return { "id": self.id, "tag": self.tag, "requests": [request.turn_to_json() for request in self.requests.all()] } def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Answer(BaseDateTimeModel): __tablename__ = 'answers' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) # noqa: A003 text = db.Column(db.Text(), nullable=False) is_approve = db.Column(db.Boolean, default=False) question_id = db.Column(db.Integer, db.ForeignKey('questions.id')) likes_count = db.Column(db.Integer, default=0, nullable=False) user_relation = db.relationship( 'AnswerUsersRelations', back_populates='answers', ) owner_id = db.Column(db.Integer, db.ForeignKey('users.id')) users = db.relationship('User', back_populates='answers') def update_likes_count(self, like_status: int, on_created=False): if on_created and like_status: self.likes_count += 1 elif on_created and not like_status: self.likes_count -= 1 elif not on_created and like_status: self.likes_count += 2 else: self.likes_count -= 2
class Answer(BaseDateTimeModel): __tablename__ = 'answers' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) # noqa: A003 text = db.Column(db.Text(), nullable=False) is_approve = db.Column(db.Boolean, default=False) question_id = db.Column(db.Integer, db.ForeignKey('questions.id'))
class Section(db.Model): __tablename__ = 'sections' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer(), primary_key=True) # noqa: A003 name = db.Column(db.Text(), nullable=False) order_number = db.Column(db.Integer, nullable=False) chapters = db.relationship('Chapter', back_populates='section')
class Chapter(db.Model): __tablename__ = 'chapters' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer(), primary_key=True) # noqa: A003 name = db.Column(db.Text(), nullable=False) order_number = db.Column(db.Integer, nullable=False) questions = db.relationship('Question', back_populates='chapter') section_id = db.Column(db.Integer, db.ForeignKey('sections.id')) section = db.relationship('Section', back_populates='chapters')
class RequestModel(db.Model): __tablename__ = "requests" id = db.Column(db.Text(length=36), default=lambda: str(uuid.uuid4()), primary_key=True) theme = db.Column(db.Text, db.ForeignKey("request_themes.id")) title = db.Column(db.String(80), nullable=False) body = db.Column(db.String(190), nullable=False) creator = db.Column(db.Text(length=36), db.ForeignKey("users.id"), nullable=False) # executor = db.Column(db.Text(length=36), db.ForeignKey("users.id"), nullable=True) status = db.Column(db.Integer, nullable=False) @classmethod def find_by_id(cls, _id: int) -> "RequestModel": return cls.query.filter_by(id=_id).first() @classmethod def find_by_theme(cls, theme: str) -> "RequestModel": return cls.query.filter_by(theme=theme).first() def turn_to_json(self) -> dict: return { "id": self.id, "theme": self.theme, "title": self.title, "body": self.body, "status": self.status, "creator": self.creator } def save_to_db(self): db.session.add(self) db.session.commit() def delete_from(self): db.session.delete(self) db.session.commit()
class Answer(BaseDateTimeModel): __tablename__ = 'answers' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) # noqa: A003 text = db.Column(db.Text(), nullable=False) is_approve = db.Column(db.Boolean, default=False) question_id = db.Column(db.Integer, db.ForeignKey('questions.id')) likes_count = db.Column(db.Integer, default=0, nullable=False) user_relation = db.relationship( 'AnswerUsersRelations', back_populates='answers', )
class Question(BaseDateTimeModel): __tablename__ = 'questions' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer(), primary_key=True) # noqa: A003 text = db.Column(db.Text(), nullable=False) order_number = db.Column(db.Integer, nullable=False) user = db.Column(db.Integer, db.ForeignKey('users.id')) answers = db.relationship( 'Answer', backref='questions', lazy='dynamic', ) chapter_id = db.Column(db.Integer, db.ForeignKey('chapters.id')) chapter = db.relationship( 'Chapter', back_populates='questions') test_case = db.relationship( 'TestCase', back_populates='question', uselist=False) def __str__(self): return f'Вопрос #{self.order_number} {self.text}'
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Text(length=36), default=lambda: str(uuid.uuid4()), primary_key=True) username = db.Column(db.String(80), nullable=False, unique=True, index=True) email = db.Column(db.String(120), nullable=False, unique=True) password_hash = db.Column(db.String(80), nullable=False) password_salt = db.Column(db.String(80), nullable=False) session_key = db.Column(db.String(256)) # just for development balance = db.Column(db.Integer, default=0) second_fa_enabled = db.Column( db.Boolean, nullable=False, default=False) # MAY BE NOT PRODUCTION IMPLEMENTING token_2fa = db.Column(db.String(120)) name = db.Column(db.String(80)) surname = db.Column(db.String(80)) profile_pic = db.Column(db.String(64), nullable=False, default="gg_default_profile_pic.png") locality = db.Column(db.String(120)) confirmation = db.relationship("ConfirmationModel", lazy="dynamic", cascade="all, delete-orphan") user_requests = db.relationship("RequestModel", lazy="dynamic", cascade="all, delete-orphan") # executing_requests = db.relationship( # "RequestModel", # lazy="dynamic", # cascade="all, delete-orphan" # ) @property def most_recent_confirmation(self) -> "ConfirmationModel": return self.confirmation.order_by(db.desc( ConfirmationModel.expire_at)).first() def turn_to_json(self): return { "username": self.username, "id": self.id, "locality": self.locality, "profile_pic": self.profile_pic } @classmethod def find_by_id(cls, _id: str) -> "UserModel": return cls.query.get(_id) @classmethod def find_by_email(cls, email: str) -> "UserModel": return cls.query.filter_by(email=email).first() @classmethod def find_by_username(cls, username: str) -> "UserModel": return cls.query.filter_by(username=username).first() @classmethod # TODO: TESTING VERSION MAYBE def find_by_token_2fa(cls, token: str) -> "UserModel": return cls.query.filter_by(token_2fa=token).first() @classmethod def find_by_session_key(cls, key: str) -> "UserModel": return cls.query.filter_by(session_key=key).first() @classmethod def find_by_locality(cls, locality: str): pass def confirm(self) -> Response: link = request.url_root[0:-1] + url_for( "confirmation", confirmation_id=self.most_recent_confirmation.id) subject = f"{response_quote('user_registration_confirmation_subject')}" text = f"{response_quote('user_registration_confirmation_text')} {link}" html = f'<html>{response_quote("user_registration_confirmation_text")}: <a href="{link}">{link}</a></html>' return MailGun.send_email_message([self.email], subject, text, html) def password_reset_request(self, code: str) -> Response: subject = f"{response_quote('user_password_restore_subject').format(self.email)}" text = f"{response_quote('user_password_restore_text').format(code)}" html = f'<html>{response_quote("user_password_restore_text").format(code)}</html>' return MailGun.send_email_message([self.email], subject, text, html) def send_email_2fa_code(self, code: str) -> Response: subject = response_quote("email2fa_code_mail_subject") text = response_quote("email2fa_code_mail_text") html = f'<html>' \ f'{response_quote("email2fa_code_mail_text").format(EmailSecondFA.token_expiration_delta().seconds // 60)} ' \ f'<h4>{code}</h4></html>' return MailGun.send_email_message([self.email], subject, text, html) def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()