system-pri/backend/app/project_supervisor/routes/project_grade_sheet.py

71 lines
2.5 KiB
Python

from apiflask import APIBlueprint
from flask import abort
from ...base.schemas import MessageSchema
from ...students.models import Group
from ...students.schemas import (
ProjectGradeSheetDetailFirstTermSchema,
ProjectGradeSheetDetailSecondTermSchema,
ProjectGradeSheetEditFirstTermSchema,
ProjectGradeSheetEditSecondTermSchema,
)
from ..models import ProjectSupervisor
from ..query.project_grade_sheet import update_project_grade_sheet
from ..schemas import ProjectSupervisorTermQuerySchema, TemporaryProjectSupervisorSchema
bp = APIBlueprint(
"project_grade_sheet_for_project_supervisor",
__name__,
url_prefix="/project-grade-sheet",
)
@bp.get("/group/<int:group_id>/")
@bp.input(ProjectSupervisorTermQuerySchema, location="query")
def detail_project_grade_sheet(group_id: int, query: dict) -> dict:
project_supervisor_id = query.get("id")
project_supervisor = ProjectSupervisor.query.filter(
ProjectSupervisor.id == project_supervisor_id
).first()
if project_supervisor is None:
abort(404, "Not found project supervisor!")
####################################
term = query.get("term")
group = Group.query.filter(
Group.project_supervisor_id == project_supervisor_id, Group.id == group_id
).first()
if group is None or len(group.project_grade_sheet) == 0:
abort(
400, "Group doesn't exist or you are not project supervisor of that group!"
)
pgs = group.project_grade_sheet[0]
if term == 1:
schema = ProjectGradeSheetDetailFirstTermSchema()
else:
schema = ProjectGradeSheetDetailSecondTermSchema()
return schema.dump(pgs)
@bp.patch("/group/<int:group_id>/first-term/")
@bp.input(TemporaryProjectSupervisorSchema, location="query")
@bp.input(ProjectGradeSheetEditFirstTermSchema, location="json")
@bp.output(MessageSchema)
def update_project_grade_sheet_for_first_term(
group_id: int, query: dict, data: dict
) -> dict:
update_project_grade_sheet(group_id, query, data)
return {"message": "Your project grade sheet was updated!"}
@bp.patch("/group/<int:group_id>/second-term/")
@bp.input(TemporaryProjectSupervisorSchema, location="query")
@bp.input(ProjectGradeSheetEditSecondTermSchema, location="json")
@bp.output(MessageSchema)
def update_project_grade_sheet_for_second_term(
group_id: int, query: dict, data: dict
) -> dict:
update_project_grade_sheet(group_id, query, data)
return {"message": "Your project grade sheet was updated!"}