update download student endpoint for coordinator view - generate list by mode of students

This commit is contained in:
dominik24c 2022-06-14 09:42:48 +02:00
parent 3914d19138
commit 7a90c84178
2 changed files with 12 additions and 3 deletions

View File

@ -10,7 +10,7 @@ from flask_sqlalchemy import get_debug_queries
from ...students.models import Student, Group from ...students.models import Student, Group
from ...project_supervisor.models import ProjectSupervisor from ...project_supervisor.models import ProjectSupervisor
from ..schemas import StudentSchema, StudentEditSchema, StudentsPaginationSchema, \ from ..schemas import StudentSchema, StudentEditSchema, StudentsPaginationSchema, \
StudentCreateSchema, MessageSchema, FileSchema, StudentQuerySchema StudentCreateSchema, MessageSchema, FileSchema, StudentQuerySchema, StudentListFileDownloaderSchema
from ...dependencies import db from ...dependencies import db
from ..utils import parse_csv, generate_csv from ..utils import parse_csv, generate_csv
from ..exceptions import InvalidNameOrTypeHeaderException from ..exceptions import InvalidNameOrTypeHeaderException
@ -126,11 +126,16 @@ def upload_students(file: dict) -> dict:
@bp.route("/download/", methods=["POST"]) @bp.route("/download/", methods=["POST"])
def download_students() -> Response: @bp.input(StudentListFileDownloaderSchema, location='query')
students = db.session.query(Student).join(Group).join(ProjectSupervisor).all() def download_students(query: dict) -> Response:
mode = query.get('mode') or True
students = db.session.query(Student).join(Group).\
join(ProjectSupervisor).filter(Student.mode == mode).all()
if len(students) == 0: if len(students) == 0:
abort(404, "Not found students, which are assigned to group!") abort(404, "Not found students, which are assigned to group!")
csv_file = generate_csv(students) csv_file = generate_csv(students)
response = Response(csv_file, mimetype='text/csv') response = Response(csv_file, mimetype='text/csv')
response.headers.set("Content-Disposition", "attachment", filename="students_list.csv") response.headers.set("Content-Disposition", "attachment", filename="students_list.csv")
print(get_debug_queries())
return response return response

View File

@ -36,6 +36,10 @@ class StudentsPaginationSchema(ma.Schema):
max_pages = fields.Integer() max_pages = fields.Integer()
class StudentListFileDownloaderSchema(ma.Schema):
mode = fields.Integer()
class StudentCreateSchema(ma.Schema): class StudentCreateSchema(ma.Schema):
first_name = fields.Str(validate=validate.Length(min=1, max=255), required=True) 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) last_name = fields.Str(validate=validate.Length(min=1, max=255), required=True)