from flask_sqlalchemy import BaseQuery from ..dependencies import db from ..base.models import Person, Base from ..base.utils import order_by_column_name from ..students.models import YearGroup class YearGroupProjectSupervisors(Base): __tablename__ = 'year_group_project_supervisors' project_supervisor_id = db.Column(db.Integer, db.ForeignKey('project_supervisors.id'), nullable=False) year_group_id = db.Column(db.Integer, db.ForeignKey('year_groups.id'), nullable=False) limit_group = db.Column(db.Integer, default=3, nullable=False) class ProjectSupervisor(Base, Person): __tablename__ = "project_supervisors" year_groups = db.relationship('YearGroupProjectSupervisors', lazy=True) @classmethod def search_by_fullname_and_mode_and_order_by_first_name_or_last_name(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(YearGroup.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