add the functionality for students to assign and remove to selected exam date

This commit is contained in:
dominik24c 2022-10-27 16:59:12 +02:00
parent 350d9d2a32
commit 12e15043e3
5 changed files with 90 additions and 9 deletions

View File

@ -1,10 +1,8 @@
import datetime
from apiflask import APIBlueprint
from flask import abort, current_app
from ..schemas import MessageSchema, CommitteeCreateSchema, TemporaryProjectSupervisorSchema
from ...examination_schedule.models import Enrollment, Committee, ExaminationSchedule
from ...examination_schedule.models import Committee
from ...dependencies import db
from ..models import ProjectSupervisor
from ..query import get_enrollment_by_enrollment_and_examination_schedule_ids, \

View File

@ -0,0 +1,10 @@
import datetime
from flask import abort
from ..examination_schedule.models import ExaminationSchedule
def check_the_enrollments_has_just_started(es: ExaminationSchedule) -> None:
now = datetime.datetime.utcnow()
if es.start_date is None or es.end_date is None or not (es.start_date < now < es.end_date):
abort(403, "Forbidden! Enrollment hasn't started yet.")

View File

@ -1,7 +1,9 @@
from flask import Blueprint
from .enrollments import bp as enrollments_bp
from .registrations import bp as registrations_bp
bp = Blueprint("students", __name__, url_prefix="/students")
bp.register_blueprint(registrations_bp)
bp.register_blueprint(registrations_bp)
bp.register_blueprint(enrollments_bp)

View File

@ -0,0 +1,64 @@
import datetime
from apiflask import APIBlueprint
from flask import abort
from ..schemas import MessageSchema, TemporaryStudentSchema
from ...dependencies import db
from ..models import Student, Group
from ...project_supervisor.models import ProjectSupervisor
from ...project_supervisor.query import get_enrollment_by_enrollment_and_examination_schedule_ids, \
check_the_project_supervisor_is_in_committee
from ..query import check_the_enrollments_has_just_started
bp = APIBlueprint("enrollments", __name__, url_prefix="/")
@bp.post('/<int:examination_schedule_id>/enrollments/<int:enrollment_id>/')
@bp.input(TemporaryStudentSchema)
@bp.output(MessageSchema)
def assign_group_for_this_exam_date(examination_schedule_id: int, enrollment_id: int, data: dict) -> dict:
# this code will be removed
student = Student.query.filter(Student.index == data['student_index']).first()
if student is None:
abort(404, "Student doesn't exist!")
################
enrollment = get_enrollment_by_enrollment_and_examination_schedule_ids(examination_schedule_id, enrollment_id)
check_the_enrollments_has_just_started(enrollment.examination_schedule)
st = Student.query.join(Group).join(ProjectSupervisor).filter(Student.index == student.index).first()
if st is None or st.group.project_supervisor is None:
abort(400, "You don't have a group or your group doesn't have an assigned project supervisor!")
if not check_the_project_supervisor_is_in_committee(enrollment_id, st.group.project_supervisor.id):
abort(400, "Your project supervisor is not in committee!")
enrollment.group_id = st.group.id
db.session.add(enrollment)
db.session.commit()
return {"message": "You have just assigned the group for this exam date!"}
@bp.delete('/<int:examination_schedule_id>/enrollments/<int:enrollment_id>/')
@bp.input(TemporaryStudentSchema)
@bp.output(MessageSchema)
def delete_group_for_this_exam_date(examination_schedule_id: int, enrollment_id: int, data: dict) -> dict:
# this code will be removed
student = Student.query.filter(Student.index == data['student_index']).first()
if student is None:
abort(404, "Student doesn't exist!")
################
enrollment = get_enrollment_by_enrollment_and_examination_schedule_ids(examination_schedule_id, enrollment_id)
if student.group.id != enrollment.group_id:
abort(400, "You are not assigned to this committee!")
check_the_enrollments_has_just_started(enrollment.examination_schedule)
enrollment.group = None
db.session.add(enrollment)
db.session.commit()
return {"message": "You have just removed the group for this exam date!"}

View File

@ -1,8 +1,7 @@
from ..dependencies import ma
from marshmallow import fields
from marshmallow import fields, Schema
class ProjectSupervisorSchema(ma.Schema):
class ProjectSupervisorSchema(Schema):
first_name = fields.Str()
last_name = fields.Str()
email = fields.Str()
@ -10,12 +9,20 @@ class ProjectSupervisorSchema(ma.Schema):
available_groups = fields.Integer()
class ProjectSupervisorPaginationSchema(ma.Schema):
class ProjectSupervisorPaginationSchema(Schema):
project_supervisors = fields.List(fields.Nested(ProjectSupervisorSchema))
max_pages = fields.Integer()
class ProjectSupervisorQuerySchema(ma.Schema):
class ProjectSupervisorQuerySchema(Schema):
page = fields.Integer()
per_page = fields.Integer()
mode = fields.Boolean()
class TemporaryStudentSchema(Schema):
student_index = fields.Integer(required=True)
class MessageSchema(Schema):
message = fields.Str()