change upload to new csv format
This commit is contained in:
parent
9902e382e1
commit
a4a2807fd2
@ -102,11 +102,9 @@ def create_student(data: dict) -> dict:
|
|||||||
@bp.output(MessageSchema)
|
@bp.output(MessageSchema)
|
||||||
def upload_students(file: dict) -> dict:
|
def upload_students(file: dict) -> dict:
|
||||||
uploaded_file = file.get('file')
|
uploaded_file = file.get('file')
|
||||||
mode = file.get('mode') == '0'
|
|
||||||
|
|
||||||
if uploaded_file and is_allowed_extensions(uploaded_file.filename):
|
if uploaded_file and is_allowed_extensions(uploaded_file.filename):
|
||||||
try:
|
try:
|
||||||
students = parse_csv(uploaded_file, mode)
|
students = parse_csv(uploaded_file, mode=True)
|
||||||
while True:
|
while True:
|
||||||
sliced_students = islice(students, 5)
|
sliced_students = islice(students, 5)
|
||||||
list_of_students = list(sliced_students)
|
list_of_students = list(sliced_students)
|
||||||
@ -120,6 +118,7 @@ def upload_students(file: dict) -> dict:
|
|||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
# in the future create sql query checks index and add only these students, which didn't exist in db
|
# 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!")
|
abort(400, "These students have already exist!")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
abort(400, "Invalid extension of file")
|
abort(400, "Invalid extension of file")
|
||||||
|
|
||||||
|
@ -9,12 +9,13 @@ from ..students.models import Student
|
|||||||
|
|
||||||
def check_columns(df: pd.DataFrame) -> bool:
|
def check_columns(df: pd.DataFrame) -> bool:
|
||||||
headers = set(df.keys().values)
|
headers = set(df.keys().values)
|
||||||
columns = ['first_name', 'last_name', 'index', 'mode']
|
columns = ['NAZWISKO','IMIE','INDEKS','PESEL','EMAIL']
|
||||||
|
|
||||||
if len(headers - set(columns)) != 0:
|
if len(headers - set(columns)) != 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
flag = True
|
flag = True
|
||||||
col_types = ['object', 'object', 'int', 'int']
|
col_types = ['object', 'object', 'int', 'float64', 'object']
|
||||||
|
|
||||||
for name, col_type in zip(columns, col_types):
|
for name, col_type in zip(columns, col_types):
|
||||||
if not str(df.dtypes[name]).startswith(col_type):
|
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]:
|
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):
|
# if not check_columns(df):
|
||||||
raise InvalidNameOrTypeHeaderException
|
# 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())
|
for _, item in df.iterrows())
|
||||||
|
|
||||||
return students
|
return students
|
||||||
|
@ -50,7 +50,6 @@ const Students = () => {
|
|||||||
const file = event.target.files[0]
|
const file = event.target.files[0]
|
||||||
const payload = new FormData()
|
const payload = new FormData()
|
||||||
payload.append('file', file, file.name)
|
payload.append('file', file, file.name)
|
||||||
payload.append('mode', mode ? '0' : '1')
|
|
||||||
mutateUpload(payload)
|
mutateUpload(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user