async def number_of_available_mentors(): query = db.text(""" SELECT COUNT(*) FROM users WHERE is_mentor = true AND tg_id in (SELECT mentor_id FROM pairs WHERE created_at::date = CURRENT_DATE); """) total = await db.first(query) return total.count
async def get_current_mentor(mentee: User) -> User: query = db.text(""" SELECT mentor.tg_id, mentor.tg_username FROM pairs INNER JOIN users AS mentor ON mentor.tg_id = pairs.mentor_id WHERE pairs.mentee_id = :mentee_id AND pairs.created_at::date = ( SELECT MAX(created_at::date) FROM pairs ); """) mentor = await db.first(query, mentee_id=mentee.tg_id) return mentor
async def get_random_mentor(cls): mentors = await cls.get_mentors() query = db.text(""" SELECT COUNT(*) FROM users WHERE is_mentor = false; """) total = await db.first(query) weights = [ abs(total.count - await cls.get_number_of_mentees(mentor)) for mentor in mentors ] return random.choices(mentors, weights).pop()
class Produto(db.Model): __tablename__ = 'produtos' id = db.Column(db.Integer, primary_key=True) nome = db.Column(db.String(250), nullable=False) preco_unitario = db.Column(db.DECIMAL(18, 2), nullable=False) multiplo = db.Column(db.Integer, nullable=True, server_default=db.text('1')) def __init__(self, nome, preco_unitario, multiplo): self.nome = nome self.preco_unitario = preco_unitario self.multiplo = multiplo
async def get_number_of_mentees(mentor: User): query = db.text(""" SELECT mentor.tg_username AS mentor, COUNT(mentee.tg_id) FROM pairs INNER JOIN users AS mentor ON pairs.mentor_id = mentor.tg_id INNER JOIN users AS mentee ON pairs.mentee_id = mentee.tg_id WHERE mentor.tg_id = :mentor_id AND pairs.created_at::date = ( SELECT MAX(created_at::date) FROM pairs ) GROUP BY mentor.tg_username; """) data = await db.first(query, mentor_id=mentor.tg_id) return data.count if data is not None else 0