From d6c0bd189b42bc1b44d61d917fabd6d735325ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Drzewi=C5=84ski?= Date: Sun, 12 Jun 2022 19:01:16 +0200 Subject: [PATCH] add mode info to upload and update test data --- backend/app/coordinator/routes/students.py | 4 ++-- backend/app/coordinator/schemas.py | 1 + backend/app/coordinator/utils.py | 4 ++-- data/students.csv | 18 +++++++++--------- frontend/src/api/students.ts | 3 ++- frontend/src/views/coordinator/Students.tsx | 2 +- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/backend/app/coordinator/routes/students.py b/backend/app/coordinator/routes/students.py index b640f78..e80ed76 100644 --- a/backend/app/coordinator/routes/students.py +++ b/backend/app/coordinator/routes/students.py @@ -100,12 +100,12 @@ def create_student(data: dict) -> dict: @bp.route("/upload/", methods=["POST"]) @bp.input(FileSchema, location='form_and_files') @bp.output(MessageSchema) -def upload_students(file: dict) -> dict: +def upload_students(file: dict, mode: bool) -> dict: uploaded_file = file.get('file') if uploaded_file and is_allowed_extensions(uploaded_file.filename): try: - students = parse_csv(uploaded_file) + students = parse_csv(uploaded_file, mode) while True: sliced_students = islice(students, 5) list_of_students = list(sliced_students) diff --git a/backend/app/coordinator/schemas.py b/backend/app/coordinator/schemas.py index 048128f..f4e5fe3 100644 --- a/backend/app/coordinator/schemas.py +++ b/backend/app/coordinator/schemas.py @@ -51,6 +51,7 @@ class MessageSchema(ma.Schema): class FileSchema(ma.Schema): file = fields.Raw(type='file', required=True) + mode = fields.Bool(required=True) class StudentQuerySchema(ma.Schema): diff --git a/backend/app/coordinator/utils.py b/backend/app/coordinator/utils.py index 07605a6..e3abe2f 100644 --- a/backend/app/coordinator/utils.py +++ b/backend/app/coordinator/utils.py @@ -24,12 +24,12 @@ def check_columns(df: pd.DataFrame) -> bool: return flag -def parse_csv(file) -> Generator[Student, Any, None]: +def parse_csv(file, mode) -> Generator[Student, Any, None]: df = pd.read_csv(file) if not check_columns(df): raise InvalidNameOrTypeHeaderException - students = (Student(**dict(item.items(), email=f'student{randint(1, 300_000)}@gmail.com')) + students = (Student(**dict(item.items(), mode=mode)) for _, item in df.iterrows()) return students diff --git a/data/students.csv b/data/students.csv index 8ba79b7..e6c49f3 100644 --- a/data/students.csv +++ b/data/students.csv @@ -1,9 +1,9 @@ -first_name,last_name,index,mode -Patryk,Drzewiński,452790,1 -Adam,Skowronek,452791,1 -Mariia,Kuzmenko,452792,1 -Dominik,Cupał,452793,1 -Natalia,Wasik,452794,0 -Michalina,Gaj,452795,0 -Jan,Kowalski,452796,0 -Adrian,Kowalski,452797,1 \ No newline at end of file +NAZWISKO,IMIE,INDEKS,PESEL,EMAIL +Drzewiński,Patryk,452790,11111111111,patdrz1@st.amu.edu.pl +Skowronek,Adam,452791,22222222222,adasko8@st.amu.edu.pl +Kuzmenko,Mariia,452792,33333333333,markuz5@st.amu.edu.pl +Cupał,Dominik,452793,44444444444,domcup@st.amu.edu.pl +Wasik,Natalia,452794,,wasna@st.amu.edu.pl +Gaj,Michalina,452795,,gajm@st.amu.edu.pl +Kowalski,Jan,452796,55555555555,kowj@st.amu.edu.pl +Kowalski,Adrian,452797,66666666666,kowa@st.amu.edu.pl \ No newline at end of file diff --git a/frontend/src/api/students.ts b/frontend/src/api/students.ts index 8e9d734..c518ee8 100644 --- a/frontend/src/api/students.ts +++ b/frontend/src/api/students.ts @@ -34,8 +34,9 @@ export const getStudents = ( export const createStudent = (payload: Student) => axiosInstance.post('http://127.0.0.1:5000/api/coordinator/students/', payload) -export const uploadStudents = (payload: FormData) => +export const uploadStudents = (payload: FormData, mode: Boolean) => axiosInstance.post( 'http://127.0.0.1:5000/api/coordinator/students/upload/', payload, + mode, ) diff --git a/frontend/src/views/coordinator/Students.tsx b/frontend/src/views/coordinator/Students.tsx index 322ebec..d4003ee 100644 --- a/frontend/src/views/coordinator/Students.tsx +++ b/frontend/src/views/coordinator/Students.tsx @@ -40,7 +40,7 @@ const Students = () => { const { mutate: mutateUpload } = useMutation( 'uploadStudents', - (payload: FormData) => uploadStudents(payload), + (payload: FormData) => uploadStudents(payload, mode), { onSuccess: () => refetchStudents(), },