from apiflask import APIBlueprint from flask import abort from ...dependencies import db from ..models import ProjectSupervisor from ..schemas import ProjectSupervisorTermQuerySchema, MessageSchema, TemporaryProjectSupervisorSchema from ...students.schemas import ProjectGradeSheetDetailFirstTermSchema, ProjectGradeSheetDetailSecondTermSchema, \ ProjectGradeSheetEditFirstTermSchema, ProjectGradeSheetEditSecondTermSchema from ...students.models import Group, ProjectGradeSheet bp = APIBlueprint("project_grade_sheet_for_project_supervisor", __name__, url_prefix="/project-grade-sheet") @bp.get('/group//') @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, "ProjectSupervisor doesn't exist!") #################################### 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!") pgs = group.project_grade_sheet[0] if term == 1: schema = ProjectGradeSheetDetailFirstTermSchema() else: schema = ProjectGradeSheetDetailSecondTermSchema() return schema.dump(pgs) def update_project_grade_sheet(group_id: int, query: dict, data: dict) -> None: project_supervisor_id = query.get('id') project_supervisor = ProjectSupervisor.query.filter(ProjectSupervisor.id == project_supervisor_id).first() if project_supervisor is None: abort(404, "ProjectSupervisor doesn't exist!") #################################### if len(data) == 0: abort(400, "You passed empty data!") pgs_query = ProjectGradeSheet.query.filter(ProjectGradeSheet.group_id == group_id) if pgs_query.first() is None: abort(404, "Not found project grade sheet!") pgs_query.update(data) db.session.commit() @bp.patch('/group//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//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!"}