2022-05-17 22:16:45 +02:00
|
|
|
from random import randint
|
2022-05-19 18:15:11 +02:00
|
|
|
from itertools import islice
|
2022-06-13 00:16:07 +02:00
|
|
|
from typing import List
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-13 00:16:07 +02:00
|
|
|
from flask import Response, abort
|
2022-06-06 21:30:30 +02:00
|
|
|
from apiflask import APIBlueprint
|
2022-05-19 18:15:11 +02:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2022-05-18 17:36:16 +02:00
|
|
|
from flask_sqlalchemy import get_debug_queries
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-13 00:16:07 +02:00
|
|
|
from ...students.models import Student, Group
|
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
2022-06-06 21:30:30 +02:00
|
|
|
from ..schemas import StudentSchema, StudentEditSchema, StudentsPaginationSchema, \
|
|
|
|
StudentCreateSchema, MessageSchema, FileSchema, StudentQuerySchema
|
2022-05-17 22:16:45 +02:00
|
|
|
from ...dependencies import db
|
2022-06-13 00:16:07 +02:00
|
|
|
from ..utils import parse_csv, generate_csv
|
2022-05-17 22:16:45 +02:00
|
|
|
from ..exceptions import InvalidNameOrTypeHeaderException
|
2022-05-19 18:15:11 +02:00
|
|
|
from ...base.utils import paginate_models, is_allowed_extensions
|
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
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/", methods=["GET"])
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.input(StudentQuerySchema, location='query')
|
|
|
|
@bp.output(StudentsPaginationSchema)
|
|
|
|
def list_students(query: dict) -> dict:
|
|
|
|
fullname = query.get('fullname')
|
|
|
|
order_by_first_name = query.get('order_by_first_name')
|
|
|
|
order_by_last_name = query.get('order_by_last_name')
|
2022-06-10 18:18:15 +02:00
|
|
|
mode = query.get('mode')
|
2022-06-06 21:30:30 +02:00
|
|
|
page = query.get('page')
|
2022-06-08 21:48:49 +02:00
|
|
|
per_page = query.get('per_page')
|
2022-05-18 17:36:16 +02:00
|
|
|
|
2022-06-10 18:18:15 +02:00
|
|
|
student_query = Student.search_by_fullname_and_mode_and_order_by_first_name_or_last_name(
|
2022-06-11 12:53:55 +02:00
|
|
|
fullname, mode, 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])
|
2022-06-06 21:30:30 +02:00
|
|
|
return {
|
2022-06-13 18:04:30 +02:00
|
|
|
"students": data['items'],
|
|
|
|
"max_pages": data['max_pages']
|
2022-06-06 21:30:30 +02:00
|
|
|
}
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:index>/", methods=["GET"])
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.output(StudentSchema)
|
|
|
|
def detail_student(index: int) -> Student:
|
2022-05-17 22:16:45 +02:00
|
|
|
student = Student.query.filter_by(index=index).first()
|
2022-06-06 21:30:30 +02:00
|
|
|
if student is None:
|
|
|
|
abort(404, f"Student with {index} index doesn't exist!")
|
|
|
|
return student
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:index>/", methods=["DELETE"])
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.output(MessageSchema, status_code=202)
|
|
|
|
def delete_student(index: int) -> dict:
|
2022-05-17 22:16:45 +02:00
|
|
|
student = Student.query.filter_by(index=index).first()
|
2022-06-06 21:30:30 +02:00
|
|
|
if student is None:
|
|
|
|
abort(404, f"Student with {index} index doesn't exist!")
|
|
|
|
db.session.delete(student)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Student was deleted!"}
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:index>/", methods=["PUT"])
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.input(StudentEditSchema)
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def edit_student(index: int, data: dict) -> dict:
|
2022-06-11 12:53:55 +02:00
|
|
|
if not data:
|
|
|
|
abort(400, 'You have passed empty data!')
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-11 12:53:55 +02:00
|
|
|
student_query = Student.query.filter_by(index=index)
|
|
|
|
student = student_query.first()
|
2022-06-06 21:30:30 +02:00
|
|
|
|
2022-06-11 12:53:55 +02:00
|
|
|
if student is None:
|
|
|
|
abort(404, 'Not found student!')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/", methods=["POST"])
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.input(StudentCreateSchema)
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def create_student(data: dict) -> dict:
|
2022-06-11 12:53:55 +02:00
|
|
|
index = data['index']
|
|
|
|
student = Student.query.filter_by(index=index).first()
|
|
|
|
if student is not None:
|
|
|
|
abort(400, "Student has already exists!")
|
|
|
|
|
|
|
|
dummy_email = f'student{randint(1, 300_000)}@gmail.com'
|
|
|
|
student = Student(**data, email=dummy_email)
|
|
|
|
db.session.add(student)
|
|
|
|
db.session.commit()
|
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
return {"message": "Student was created!"}
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/upload/", methods=["POST"])
|
2022-06-06 21:30:30 +02:00
|
|
|
@bp.input(FileSchema, location='form_and_files')
|
|
|
|
@bp.output(MessageSchema)
|
2022-06-12 19:11:44 +02:00
|
|
|
def upload_students(file: dict) -> dict:
|
2022-06-06 21:30:30 +02: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:
|
2022-06-12 21:20:11 +02:00
|
|
|
students = parse_csv(uploaded_file, mode=True)
|
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
|
|
|
|
db.session.add_all(list_of_students)
|
|
|
|
db.session.commit()
|
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
|
|
|
except IntegrityError as e:
|
|
|
|
# in the future create sql query checks index and add only these students, which didn't exist in db
|
2022-06-06 21:30:30 +02:00
|
|
|
abort(400, "These students have already exist!")
|
2022-06-12 21:20:11 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/download/", methods=["POST"])
|
|
|
|
def download_students() -> Response:
|
|
|
|
students = db.session.query(Student).join(Group).join(ProjectSupervisor).all()
|
|
|
|
if len(students) == 0:
|
|
|
|
abort(404, "Not found students, which are assigned to group!")
|
|
|
|
csv_file = generate_csv(students)
|
|
|
|
response = Response(csv_file, mimetype='text/csv')
|
|
|
|
response.headers.set("Content-Disposition", "attachment", filename="students_list.csv")
|
|
|
|
return response
|