from flask_sqlalchemy import BaseQuery from ..base.models import Base, Person from ..base.utils import order_by_column_name from ..dependencies import db from ..students.models import YearGroup class ProjectSupervisor(Base, Person): __tablename__ = "project_supervisors" limit_group = db.Column(db.Integer, default=3, nullable=False) is_coordinator = db.Column(db.Boolean, default=False, nullable=False) year_group_id = db.Column(db.Integer, db.ForeignKey("year_groups.id")) year_group = db.relationship("YearGroup", backref="project_supervisors") __table__args = db.UniqueConstraint( "email", "year_group_id", name="uc_email_year_group_id" ) @classmethod def search_by_fullname( cls, year_group_id: int = None, fullname: str = None, order_by_first_name: str = None, order_by_last_name: str = None, ) -> BaseQuery: project_supervisors_query = cls.query if year_group_id is not None: project_supervisors_query = project_supervisors_query.filter( ProjectSupervisor.year_group_id == year_group_id ) if fullname is not None: project_supervisors_query = project_supervisors_query.filter( (ProjectSupervisor.first_name + " " + ProjectSupervisor.last_name).like( f"{fullname}%" ) ) project_supervisors_query = order_by_column_name( project_supervisors_query, ProjectSupervisor.first_name, order_by_first_name ) project_supervisors_query = order_by_column_name( project_supervisors_query, ProjectSupervisor.last_name, order_by_last_name ) return project_supervisors_query