2022-05-19 18:15:11 +02:00
|
|
|
from itertools import islice
|
2023-01-14 17:38:03 +01:00
|
|
|
from random import randint
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
from apiflask import APIBlueprint
|
2023-01-14 17:38:03 +01:00
|
|
|
from flask import Response, abort
|
2022-11-17 14:56:19 +01:00
|
|
|
from sqlalchemy import or_
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...base.schemas import MessageSchema
|
|
|
|
from ...base.utils import is_allowed_extensions, paginate_models
|
2022-05-17 22:16:45 +02:00
|
|
|
from ...dependencies import db
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
|
|
|
from ...students.models import Group, Student, YearGroup
|
2022-05-17 22:16:45 +02:00
|
|
|
from ..exceptions import InvalidNameOrTypeHeaderException
|
2023-01-14 17:38:03 +01:00
|
|
|
from ..schemas.students import (
|
|
|
|
FileSchema,
|
|
|
|
StudentCreateSchema,
|
|
|
|
StudentEditSchema,
|
|
|
|
StudentListFileDownloaderSchema,
|
|
|
|
StudentQuerySchema,
|
|
|
|
StudentSchema,
|
|
|
|
StudentsPaginationSchema,
|
|
|
|
YearGroupInfoQuery,
|
|
|
|
)
|
|
|
|
from ..utils import generate_csv, parse_csv
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
bp = APIBlueprint("students", __name__, url_prefix="/students")
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.get("/<int:year_group_id>/")
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.input(StudentQuerySchema, location="query")
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.output(StudentsPaginationSchema)
|
2022-11-12 16:18:07 +01:00
|
|
|
def list_students(year_group_id: int, query: dict) -> dict:
|
|
|
|
# add filter by year group
|
2023-01-14 17:38:03 +01:00
|
|
|
fullname = query.get("fullname")
|
|
|
|
order_by_first_name = query.get("order_by_first_name")
|
|
|
|
order_by_last_name = query.get("order_by_last_name")
|
|
|
|
page = query.get("page")
|
|
|
|
per_page = query.get("per_page")
|
|
|
|
|
|
|
|
student_query = (
|
|
|
|
Student.search_by_fullname_and_mode_and_order_by_first_name_or_last_name(
|
|
|
|
year_group_id, fullname, order_by_first_name, order_by_last_name
|
|
|
|
)
|
|
|
|
)
|
2022-05-18 17:36:16 +02:00
|
|
|
|
2022-06-13 18:04:30 +02:00
|
|
|
data = paginate_models(page, student_query, per_page)
|
2022-05-19 18:15:11 +02:00
|
|
|
# print(get_debug_queries()[0])
|
2023-01-14 17:38:03 +01:00
|
|
|
return {"students": data["items"], "max_pages": data["max_pages"]}
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.get("/<int:student_id>/detail/")
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.output(StudentSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def detail_student(student_id: int) -> Student:
|
|
|
|
student = Student.query.filter_by(id=student_id).first()
|
2022-06-06 21:30:30 +02:00
|
|
|
if student is None:
|
2023-01-08 17:15:02 +01:00
|
|
|
abort(404, "Not found student!")
|
2022-06-06 21:30:30 +02:00
|
|
|
return student
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.delete("/<int:student_id>/")
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.output(MessageSchema, status_code=202)
|
2023-01-14 01:37:31 +01:00
|
|
|
def delete_student(student_id: int) -> dict:
|
|
|
|
student = Student.query.filter_by(id=student_id).first()
|
2022-06-06 21:30:30 +02:00
|
|
|
if student is None:
|
2023-01-08 17:15:02 +01:00
|
|
|
abort(404, "Not found student!")
|
2022-06-06 21:30:30 +02:00
|
|
|
db.session.delete(student)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Student was deleted!"}
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.put("/<int:student_id>/")
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.input(StudentEditSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def edit_student(student_id: int, data: dict) -> dict:
|
2022-06-11 12:53:55 +02:00
|
|
|
if not data:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(400, "You have passed empty data!")
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
student_query = Student.query.filter(Student.id == student_id)
|
2022-06-11 12:53:55 +02:00
|
|
|
student = student_query.first()
|
2022-06-06 21:30:30 +02:00
|
|
|
|
2022-06-11 12:53:55 +02:00
|
|
|
if student is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found student!")
|
2022-06-11 12:53:55 +02:00
|
|
|
|
|
|
|
student_query.update(data)
|
|
|
|
db.session.commit()
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
return {"message": "Student was updated!"}
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.post("/")
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.input(StudentCreateSchema)
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def create_student(data: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
index = data["index"]
|
|
|
|
yg_id = data["year_group_id"]
|
|
|
|
del data["year_group_id"]
|
2022-06-11 12:53:55 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
student = Student.query.filter(
|
|
|
|
Student.index == index, Student.year_group_id == yg_id
|
|
|
|
).first()
|
2023-01-14 01:37:31 +01:00
|
|
|
if student is not None:
|
|
|
|
abort(400, "Student has already assigned to this year group!")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
year_group = YearGroup.query.filter(YearGroup.id == yg_id).first()
|
|
|
|
if year_group is None:
|
2023-01-08 17:15:02 +01:00
|
|
|
abort(404, "Not found year group!")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
dummy_email = f"student{randint(1, 300_000)}@gmail.com"
|
2023-01-14 01:37:31 +01:00
|
|
|
student = Student(**data, email=dummy_email, year_group_id=yg_id)
|
|
|
|
db.session.add(student)
|
2022-06-11 12:53:55 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
return {"message": "Student was created!"}
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.post("/upload/")
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.input(YearGroupInfoQuery, location="query")
|
|
|
|
@bp.input(FileSchema, location="form_and_files")
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.output(MessageSchema)
|
2022-11-17 14:56:19 +01:00
|
|
|
def upload_students(query: dict, file: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
"""Add only Students to chosen year group if students exist in db and
|
|
|
|
assigned to correct year group, they will be omitted"""
|
|
|
|
year_group_id = query.get("year_group_id")
|
2022-11-17 14:56:19 +01:00
|
|
|
yg = YearGroup.query.filter(YearGroup.id == year_group_id).first()
|
|
|
|
if yg is None:
|
|
|
|
abort(404, "Not found year group!")
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
uploaded_file = file.get("file")
|
2022-05-19 18:15:11 +02:00
|
|
|
if uploaded_file and is_allowed_extensions(uploaded_file.filename):
|
|
|
|
try:
|
2023-01-14 01:37:31 +01:00
|
|
|
students = parse_csv(uploaded_file, year_group_id)
|
2022-05-19 18:15:11 +02:00
|
|
|
while True:
|
|
|
|
sliced_students = islice(students, 5)
|
|
|
|
list_of_students = list(sliced_students)
|
|
|
|
|
|
|
|
if len(list_of_students) == 0:
|
|
|
|
break
|
2022-11-17 14:56:19 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
students_in_db = (
|
|
|
|
Student.query.filter(
|
|
|
|
or_(Student.index == s.index for s in list_of_students)
|
|
|
|
)
|
|
|
|
.filter(Student.year_group_id == year_group_id)
|
|
|
|
.all()
|
|
|
|
)
|
2022-11-17 14:56:19 +01:00
|
|
|
student_index_in_db = [s.index for s in students_in_db]
|
2023-01-14 17:38:03 +01:00
|
|
|
students_not_exists_in_db = list(
|
|
|
|
filter(
|
|
|
|
lambda s: s.index not in student_index_in_db, list_of_students
|
|
|
|
)
|
|
|
|
)
|
2022-11-17 14:56:19 +01:00
|
|
|
db.session.add_all(students_not_exists_in_db)
|
2022-05-19 18:15:11 +02:00
|
|
|
db.session.commit()
|
2022-11-17 14:56:19 +01:00
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
except InvalidNameOrTypeHeaderException:
|
|
|
|
abort(400, "Invalid format of csv file!")
|
2022-05-19 18:15:11 +02:00
|
|
|
else:
|
2022-06-06 21:30:30 +02:00
|
|
|
abort(400, "Invalid extension of file")
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
return {"message": "Students was created by uploading csv file!"}
|
2022-06-13 00:16:07 +02:00
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.post("/download/")
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.input(StudentListFileDownloaderSchema, location="query")
|
2022-06-14 09:42:48 +02:00
|
|
|
def download_students(query: dict) -> Response:
|
2023-01-14 17:38:03 +01:00
|
|
|
year_group_id = query.get("year_group_id")
|
|
|
|
students_and_groups = (
|
|
|
|
db.session.query(Student, Group)
|
|
|
|
.join(Group, Student.groups)
|
|
|
|
.filter(Group.year_group_id == year_group_id)
|
|
|
|
.join(ProjectSupervisor)
|
|
|
|
.all()
|
|
|
|
)
|
2022-06-14 09:42:48 +02:00
|
|
|
|
2022-11-17 14:56:19 +01:00
|
|
|
if len(students_and_groups) == 0:
|
2023-01-14 01:37:31 +01:00
|
|
|
abort(404, "Not found students!")
|
2022-11-17 14:56:19 +01:00
|
|
|
|
|
|
|
csv_file = generate_csv(students_and_groups)
|
2023-01-14 17:38:03 +01:00
|
|
|
response = Response(csv_file, mimetype="text/csv")
|
|
|
|
response.headers.set(
|
|
|
|
"Content-Disposition", "attachment", filename="students_list.csv"
|
|
|
|
)
|
2022-06-13 00:16:07 +02:00
|
|
|
return response
|