refactor search method for models and update create group endpoint for coordinator view
This commit is contained in:
parent
ddb9133f6c
commit
558fac2c7e
@ -5,8 +5,8 @@ from flask_sqlalchemy import get_debug_queries
|
|||||||
from ...students.models import Group, Student
|
from ...students.models import Group, Student
|
||||||
from ...project_supervisor.models import ProjectSupervisor
|
from ...project_supervisor.models import ProjectSupervisor
|
||||||
from ..schemas import GroupSchema, GroupEditSchema, GroupsPaginationSchema, \
|
from ..schemas import GroupSchema, GroupEditSchema, GroupsPaginationSchema, \
|
||||||
GroupCreateSchema, MessageSchema, FileSchema, GroupQuerySchema
|
GroupCreateSchema, MessageSchema, GroupQuerySchema
|
||||||
from ...dependencies import db, ma
|
from ...dependencies import db
|
||||||
from ...base.utils import paginate_models
|
from ...base.utils import paginate_models
|
||||||
|
|
||||||
bp = APIBlueprint("groups", __name__, url_prefix="/groups")
|
bp = APIBlueprint("groups", __name__, url_prefix="/groups")
|
||||||
@ -36,29 +36,36 @@ def list_groups(query: dict) -> dict:
|
|||||||
@bp.output(MessageSchema)
|
@bp.output(MessageSchema)
|
||||||
def create_group(data: dict) -> dict:
|
def create_group(data: dict) -> dict:
|
||||||
name = data['name']
|
name = data['name']
|
||||||
students = data['students']
|
students_indexes = data['students']
|
||||||
project_supervisor_id = data['project_supervisor_id']
|
project_supervisor_id = data['project_supervisor_id']
|
||||||
group = Group.query.filter_by(name=name).first()
|
|
||||||
if group is not None:
|
|
||||||
abort(400, "Group has already exists!")
|
|
||||||
|
|
||||||
project_supervisor = ProjectSupervisor.query.filter_by(id=project_supervisor_id).first()
|
# can assign a new group to project_supervisor
|
||||||
|
result = db.session.query(ProjectSupervisor.count_groups - db.func.count(ProjectSupervisor.id)). \
|
||||||
|
join(Group).filter(ProjectSupervisor.id == project_supervisor_id). \
|
||||||
|
group_by(ProjectSupervisor.id).scalar()
|
||||||
|
|
||||||
if project_supervisor is None:
|
if result is None:
|
||||||
abort(404, f"Student with id {project_supervisor_id} doesn't exist!")
|
abort(400, "Project Supervisor doesnt exist")
|
||||||
|
elif result <= 0:
|
||||||
|
abort(400, "Can't create new group, project supervisor achieved a limit of groups")
|
||||||
|
|
||||||
group = Group(name=name, project_supervisor_id = data['project_supervisor_id'])
|
group = Group(name=name, project_supervisor_id=project_supervisor_id)
|
||||||
|
|
||||||
|
students_without_groups = db.session.query(Student).join(Group, isouter=True) \
|
||||||
|
.filter(Group.id.is_(None)).filter(Student.index.in_(students_indexes)).count()
|
||||||
|
|
||||||
|
if students_without_groups != len(students_indexes):
|
||||||
|
abort(400, "One or more students have already belonged to group!")
|
||||||
|
|
||||||
|
students = db.session.query(Student).filter(Student.index.in_(students_indexes)).all()
|
||||||
for student in students:
|
for student in students:
|
||||||
st = Student.query(index = student.index).first()
|
student.group_id = group.id
|
||||||
if st is None:
|
|
||||||
abort(404, 'Not found student!')
|
|
||||||
st.group_id = group.id
|
|
||||||
|
|
||||||
|
db.session.add_all(students)
|
||||||
db.session.add(group)
|
db.session.add(group)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return {"message": "Student was created!"}
|
return {"message": "Project Supervisor was created!"}
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/<int:id>/", methods=["GET"])
|
@bp.route("/<int:id>/", methods=["GET"])
|
||||||
|
@ -3,6 +3,7 @@ from ..students.models import Student, Group
|
|||||||
from ..project_supervisor.models import ProjectSupervisor
|
from ..project_supervisor.models import ProjectSupervisor
|
||||||
from marshmallow import fields, validate, ValidationError
|
from marshmallow import fields, validate, ValidationError
|
||||||
|
|
||||||
|
|
||||||
def validate_index(index):
|
def validate_index(index):
|
||||||
if len(str(index)) > 6:
|
if len(str(index)) > 6:
|
||||||
raise ValidationError("Length of index is too long!")
|
raise ValidationError("Length of index is too long!")
|
||||||
@ -81,8 +82,8 @@ class GroupsPaginationSchema(ma.Schema):
|
|||||||
|
|
||||||
class GroupCreateSchema(ma.Schema):
|
class GroupCreateSchema(ma.Schema):
|
||||||
name = fields.Str(validate=validate.Length(min=1, max=255), required=True)
|
name = fields.Str(validate=validate.Length(min=1, max=255), required=True)
|
||||||
project_supervisor_id = fields.Integer(validate=validate_index, required=True)
|
project_supervisor_id = fields.Integer(required=True)
|
||||||
students = fields.List(fields.Nested(StudentSchema), validate=validate.Length(min=1, max=255), required=True)
|
students = fields.List(fields.Integer(validate=validate_index, required=True))
|
||||||
|
|
||||||
|
|
||||||
class GroupEditSchema(ma.Schema):
|
class GroupEditSchema(ma.Schema):
|
||||||
|
@ -6,6 +6,7 @@ from sqlalchemy import or_
|
|||||||
from sqlalchemy.sql import text
|
from sqlalchemy.sql import text
|
||||||
from ..base.utils import order_by_column_name
|
from ..base.utils import order_by_column_name
|
||||||
|
|
||||||
|
|
||||||
class ProjectSupervisor(Base, Person):
|
class ProjectSupervisor(Base, Person):
|
||||||
__tablename__ = "project_supervisors"
|
__tablename__ = "project_supervisors"
|
||||||
|
|
||||||
@ -21,16 +22,15 @@ class ProjectSupervisor(Base, Person):
|
|||||||
project_supervisors_query = cls.query
|
project_supervisors_query = cls.query
|
||||||
|
|
||||||
if mode is not None:
|
if mode is not None:
|
||||||
project_supervisors_query = project_supervisors_query.filter(mode != 1-mode)
|
project_supervisors_query = project_supervisors_query.filter(mode != 1 - mode)
|
||||||
|
|
||||||
if fullname is not None:
|
if fullname is not None:
|
||||||
"""This works only for sqlite3 database - concat function doesn't exist so i used builtin concat
|
|
||||||
operator specific only for sqlite db - || """
|
|
||||||
project_supervisors_query = project_supervisors_query.filter(
|
project_supervisors_query = project_supervisors_query.filter(
|
||||||
text("project_supervisorss_first_name || ' ' || project_supervisorss_last_name LIKE :fullname ")
|
(ProjectSupervisor.first_name + ' ' + ProjectSupervisor.last_name).like(f'{fullname}%'))
|
||||||
).params(fullname=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.first_name,
|
||||||
project_supervisors_query = order_by_column_name(project_supervisors_query, ProjectSupervisor.last_name, order_by_last_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
|
return project_supervisors_query
|
||||||
|
@ -23,15 +23,11 @@ class Group(Base):
|
|||||||
group_query = cls.query
|
group_query = cls.query
|
||||||
|
|
||||||
if search_name is not None:
|
if search_name is not None:
|
||||||
group_query = group_query.filter(
|
group_query = group_query.filter(Group.name.like(f'{search_name}%'))
|
||||||
text("groups_name LIKE :search_name ")
|
|
||||||
).params(search_name=f'{search_name}%')
|
|
||||||
|
|
||||||
return group_query
|
return group_query
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Student(Person):
|
class Student(Person):
|
||||||
__tablename__ = "students"
|
__tablename__ = "students"
|
||||||
|
|
||||||
@ -52,11 +48,7 @@ class Student(Person):
|
|||||||
student_query = student_query.filter_by(mode=mode)
|
student_query = student_query.filter_by(mode=mode)
|
||||||
|
|
||||||
if fullname is not None:
|
if fullname is not None:
|
||||||
"""This works only for sqlite3 database - concat function doesn't exist so i used builtin concat
|
student_query = student_query.filter((Student.first_name + ' ' + Student.last_name).like(f'{fullname}%'))
|
||||||
operator specific only for sqlite db - || """
|
|
||||||
student_query = student_query.filter(
|
|
||||||
text("students_first_name || ' ' || students_last_name LIKE :fullname ")
|
|
||||||
).params(fullname=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.first_name, order_by_first_name)
|
||||||
student_query = order_by_column_name(student_query, Student.last_name, order_by_last_name)
|
student_query = order_by_column_name(student_query, Student.last_name, order_by_last_name)
|
||||||
|
Loading…
Reference in New Issue
Block a user