change upload to new csv format

This commit is contained in:
Patryk Drzewiński 2022-06-12 21:20:11 +02:00
parent 9902e382e1
commit a4a2807fd2
3 changed files with 15 additions and 10 deletions

View File

@ -102,11 +102,9 @@ def create_student(data: dict) -> dict:
@bp.output(MessageSchema)
def upload_students(file: dict) -> dict:
uploaded_file = file.get('file')
mode = file.get('mode') == '0'
if uploaded_file and is_allowed_extensions(uploaded_file.filename):
try:
students = parse_csv(uploaded_file, mode)
students = parse_csv(uploaded_file, mode=True)
while True:
sliced_students = islice(students, 5)
list_of_students = list(sliced_students)
@ -120,6 +118,7 @@ def upload_students(file: dict) -> dict:
except IntegrityError as e:
# in the future create sql query checks index and add only these students, which didn't exist in db
abort(400, "These students have already exist!")
else:
abort(400, "Invalid extension of file")

View File

@ -9,12 +9,13 @@ from ..students.models import Student
def check_columns(df: pd.DataFrame) -> bool:
headers = set(df.keys().values)
columns = ['first_name', 'last_name', 'index', 'mode']
columns = ['NAZWISKO','IMIE','INDEKS','PESEL','EMAIL']
if len(headers - set(columns)) != 0:
return False
flag = True
col_types = ['object', 'object', 'int', 'int']
col_types = ['object', 'object', 'int', 'float64', 'object']
for name, col_type in zip(columns, col_types):
if not str(df.dtypes[name]).startswith(col_type):
@ -25,11 +26,17 @@ def check_columns(df: pd.DataFrame) -> bool:
def parse_csv(file, mode) -> Generator[Student, Any, None]:
df = pd.read_csv(file)
df = pd.read_csv(file, mode)
if not check_columns(df):
raise InvalidNameOrTypeHeaderException
# if not check_columns(df):
# raise InvalidNameOrTypeHeaderException
students = (Student(**dict(item.items(), mode=mode))
students = (Student(last_name = dict(item.items())['NAZWISKO'],
first_name = dict(item.items())['NAZWISKO'],
index = dict(item.items())['INDEKS'],
pesel = str(int(dict(item.items())['PESEL'])) if not pd.isna(dict(item.items())['PESEL']) else '',
email = dict(item.items())['EMAIL'],
mode=mode)
for _, item in df.iterrows())
return students

View File

@ -50,7 +50,6 @@ const Students = () => {
const file = event.target.files[0]
const payload = new FormData()
payload.append('file', file, file.name)
payload.append('mode', mode ? '0' : '1')
mutateUpload(payload)
}