system-pri/backend/app/students/models.py

73 lines
3.0 KiB
Python

from datetime import datetime
from flask_sqlalchemy import BaseQuery
from ..dependencies import db
from ..base.models import Person, Base
from ..base.utils import order_by_column_name
from ..examination_schedule.models import TermOfDefence
class YearGroupStudents(Base):
__tablename__ = 'year_group_students'
year_group_id = db.Column(db.Integer, db.ForeignKey('year_groups.id', ondelete='CASCADE'))
student_index = db.Column(db.Integer, db.ForeignKey('students.index', ondelete='CASCADE'))
class YearGroup(Base):
__tablename__ = 'year_groups'
name = db.Column(db.String(50), unique=True, nullable=False)
mode = db.Column(db.String(1), unique=True, nullable=False)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
students = db.relationship("YearGroupStudents")
class Group(Base):
__tablename__ = "groups"
name = db.Column(db.String(60), nullable=False)
cdyd_kod = db.Column(db.String(60), default='2022/SZ')
prz_kod = db.Column(db.String(60), default='06-DPRILI0')
tzaj_kod = db.Column(db.String(60), default='LAB')
project_supervisor_id = db.Column(db.Integer, db.ForeignKey('project_supervisors.id'))
project_supervisor = db.relationship('ProjectSupervisor', backref='groups', lazy=True)
year_group_id = db.Column(db.Integer, db.ForeignKey('year_groups.id'))
year_group = db.relationship('YearGroup', backref='groups', lazy=True)
points_for_first_term = db.Column(db.Integer, default=0, nullable=False)
points_for_second_term = db.Column(db.Integer, default=0, nullable=False)
@classmethod
def search_by_name(cls, year_group_id: int, search_name: str = None) -> BaseQuery:
group_query = cls.query.filter(Group.year_group_id == year_group_id)
if search_name is not None:
group_query = group_query.filter(Group.name.like(f'{search_name}%'))
return group_query
class Student(Person):
__tablename__ = "students"
pesel = db.Column(db.String(11), default='')
index = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))
groups = db.relationship('Group', backref='students', lazy=True)
year_groups = db.relationship("YearGroupStudents")
@classmethod
def search_by_fullname_and_mode_and_order_by_first_name_or_last_name(cls, year_group_id: int, fullname: str = None,
order_by_first_name: str = None,
order_by_last_name: str = None) -> BaseQuery:
student_query = cls.query.filter(YearGroup.id == year_group_id)
if fullname is not None:
student_query = student_query.filter((Student.first_name + ' ' + Student.last_name).like(f'{fullname}%'))
student_query = order_by_column_name(student_query, Student.first_name, order_by_first_name)
student_query = order_by_column_name(student_query, Student.last_name, order_by_last_name)
return student_query