system-pri/backend/app/coordinator/schemas.py

86 lines
2.7 KiB
Python

from ..dependencies import ma
from ..students.models import Student, Group
from marshmallow import fields, validate, ValidationError
from ..project_supervisor.schemas import ProjectSupervisorSchema
def validate_index(index):
if len(str(index)) > 6:
raise ValidationError("Length of index is too long!")
elif len(str(index)) < 6:
raise ValidationError("Length of index is too short!")
class GroupSchema(ma.SQLAlchemyAutoSchema):
project_supervisor = fields.Nested(ProjectSupervisorSchema)
class Meta:
model = Group
class StudentSchema(ma.SQLAlchemyAutoSchema):
group = fields.Nested(GroupSchema)
class Meta:
model = Student
class StudentsPaginationSchema(ma.Schema):
students = fields.List(fields.Nested(StudentSchema))
max_pages = fields.Integer()
class StudentCreateSchema(ma.Schema):
first_name = fields.Str(validate=validate.Length(min=1, max=255), required=True)
last_name = fields.Str(validate=validate.Length(min=1, max=255), required=True)
pesel = fields.Str(validate=validate.Length(min=0, max=11), required=True)
index = fields.Integer(validate=validate_index, required=True)
mode = fields.Boolean(required=True)
class StudentEditSchema(ma.Schema):
first_name = fields.Str(validate=validate.Length(min=1, max=255))
last_name = fields.Str(validate=validate.Length(min=1, max=255))
pesel = fields.Str(validate=validate.Length(min=0, max=11))
index = fields.Integer(validate=validate_index)
mode = fields.Boolean()
class MessageSchema(ma.Schema):
message = fields.Str(required=True)
class FileSchema(ma.Schema):
file = fields.Raw(type='file', required=True)
mode = fields.Bool(required=True)
class StudentQuerySchema(ma.Schema):
fullname = fields.Str()
order_by_first_name = fields.Str()
order_by_last_name = fields.Str()
page = fields.Integer()
per_page = fields.Integer()
mode = fields.Boolean()
class GroupQuerySchema(ma.Schema):
name = fields.Str()
page = fields.Integer()
per_page = fields.Integer()
class GroupsPaginationSchema(ma.Schema):
groups = fields.List(fields.Nested(GroupSchema))
max_pages = fields.Integer()
class GroupCreateSchema(ma.Schema):
name = fields.Str(validate=validate.Length(min=1, max=255), required=True)
project_supervisor_id = fields.Integer(validate=validate_index, required=True)
students = fields.List(fields.Nested(StudentSchema), validate=validate.Length(min=1, max=255), required=True)
class GroupEditSchema(ma.Schema):
name = fields.Str(validate=validate.Length(min=1, max=255))
project_supervisor_id = fields.Integer(validate=validate_index)
students = fields.List(fields.Nested(StudentSchema), validate=validate.Length(min=1, max=255))