add download students list endpoint for coordinator view
This commit is contained in:
parent
f3f9d502d8
commit
ddb9133f6c
@ -1,16 +1,18 @@
|
|||||||
from random import randint
|
from random import randint
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from flask import abort
|
from flask import Response, abort
|
||||||
from apiflask import APIBlueprint
|
from apiflask import APIBlueprint
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from flask_sqlalchemy import get_debug_queries
|
from flask_sqlalchemy import get_debug_queries
|
||||||
|
|
||||||
from ...students.models import Student
|
from ...students.models import Student, Group
|
||||||
|
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
|
||||||
from ...dependencies import db
|
from ...dependencies import db
|
||||||
from ..utils import parse_csv
|
from ..utils import parse_csv, generate_csv
|
||||||
from ..exceptions import InvalidNameOrTypeHeaderException
|
from ..exceptions import InvalidNameOrTypeHeaderException
|
||||||
from ...base.utils import paginate_models, is_allowed_extensions
|
from ...base.utils import paginate_models, is_allowed_extensions
|
||||||
|
|
||||||
@ -123,3 +125,14 @@ def upload_students(file: dict) -> dict:
|
|||||||
abort(400, "Invalid extension of file")
|
abort(400, "Invalid extension of file")
|
||||||
|
|
||||||
return {"message": "Students was created by uploading csv file!"}
|
return {"message": "Students was created by uploading csv file!"}
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from typing import Generator, Any
|
from typing import Generator, Any, List
|
||||||
from random import randint
|
from collections import defaultdict
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
@ -28,15 +28,30 @@ def check_columns(df: pd.DataFrame) -> bool:
|
|||||||
def parse_csv(file, mode) -> Generator[Student, Any, None]:
|
def parse_csv(file, mode) -> Generator[Student, Any, None]:
|
||||||
df = pd.read_csv(file)
|
df = pd.read_csv(file)
|
||||||
|
|
||||||
# if not check_columns(df):
|
if not check_columns(df):
|
||||||
# raise InvalidNameOrTypeHeaderException
|
raise InvalidNameOrTypeHeaderException
|
||||||
|
|
||||||
students = (Student(last_name=dict(item.items())['NAZWISKO'],
|
students = (Student(last_name=dict(item.items())['NAZWISKO'],
|
||||||
first_name=dict(item.items())['IMIE'],
|
first_name=dict(item.items())['IMIE'],
|
||||||
index=dict(item.items())['INDEKS'],
|
index=dict(item.items())['INDEKS'],
|
||||||
pesel=str(int(dict(item.items())['PESEL'])) if not pd.isna(dict(item.items())['PESEL']) else None,
|
pesel=str(int(dict(item.items())['PESEL'])) if not pd.isna(
|
||||||
|
dict(item.items())['PESEL']) else None,
|
||||||
email=dict(item.items())['EMAIL'],
|
email=dict(item.items())['EMAIL'],
|
||||||
mode=mode)
|
mode=mode)
|
||||||
for _, item in df.iterrows())
|
for _, item in df.iterrows())
|
||||||
|
|
||||||
return students
|
return students
|
||||||
|
|
||||||
|
|
||||||
|
def generate_csv(students: List[Student]) -> str:
|
||||||
|
headers = ['PESEL', 'INDEKS', 'IMIE', 'NAZWISKO', 'EMAIL', 'CDYD_KOD', 'PRZ_KOD', 'TZAJ_KOD', 'GR_NR', 'PRG_KOD']
|
||||||
|
data = [(student.pesel, student.index, student.first_name, student.last_name, student.email,
|
||||||
|
student.group.cdyd_kod, student.group.prz_kod, student.group.tzaj_kod, student.group.project_supervisor_id,
|
||||||
|
None) for student in students]
|
||||||
|
dataframe = defaultdict(list)
|
||||||
|
for row in data:
|
||||||
|
for idx, item in enumerate(row):
|
||||||
|
dataframe[headers[idx]].append(item)
|
||||||
|
|
||||||
|
df = pd.DataFrame(dataframe)
|
||||||
|
return df.to_csv(index=False)
|
||||||
|
Loading…
Reference in New Issue
Block a user