2022-06-13 00:16:07 +02:00
|
|
|
from typing import Generator, Any, List
|
|
|
|
from collections import defaultdict
|
2022-05-17 22:16:45 +02:00
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
from .exceptions import InvalidNameOrTypeHeaderException
|
|
|
|
from ..students.models import Student
|
|
|
|
|
|
|
|
|
2022-05-19 18:15:11 +02:00
|
|
|
def check_columns(df: pd.DataFrame) -> bool:
|
|
|
|
headers = set(df.keys().values)
|
2022-06-12 22:20:10 +02:00
|
|
|
columns = ['NAZWISKO', 'IMIE', 'INDEKS', 'PESEL', 'EMAIL']
|
2022-06-12 21:20:11 +02:00
|
|
|
|
2022-05-19 18:15:11 +02:00
|
|
|
if len(headers - set(columns)) != 0:
|
|
|
|
return False
|
|
|
|
|
2022-05-17 22:16:45 +02:00
|
|
|
flag = True
|
2022-06-12 21:20:11 +02:00
|
|
|
col_types = ['object', 'object', 'int', 'float64', 'object']
|
2022-05-19 18:15:11 +02:00
|
|
|
|
2022-05-17 22:16:45 +02:00
|
|
|
for name, col_type in zip(columns, col_types):
|
2022-05-19 18:15:11 +02:00
|
|
|
if not str(df.dtypes[name]).startswith(col_type):
|
2022-05-17 22:16:45 +02:00
|
|
|
flag = False
|
|
|
|
break
|
|
|
|
|
|
|
|
return flag
|
|
|
|
|
|
|
|
|
2022-06-12 19:01:16 +02:00
|
|
|
def parse_csv(file, mode) -> Generator[Student, Any, None]:
|
2022-06-12 22:20:10 +02:00
|
|
|
df = pd.read_csv(file)
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-13 00:16:07 +02:00
|
|
|
if not check_columns(df):
|
|
|
|
raise InvalidNameOrTypeHeaderException
|
2022-05-17 22:16:45 +02:00
|
|
|
|
2022-06-12 22:20:10 +02:00
|
|
|
students = (Student(last_name=dict(item.items())['NAZWISKO'],
|
|
|
|
first_name=dict(item.items())['IMIE'],
|
|
|
|
index=dict(item.items())['INDEKS'],
|
2022-06-13 00:16:07 +02:00
|
|
|
pesel=str(int(dict(item.items())['PESEL'])) if not pd.isna(
|
|
|
|
dict(item.items())['PESEL']) else None,
|
2022-06-12 22:20:10 +02:00
|
|
|
email=dict(item.items())['EMAIL'],
|
2022-06-12 21:20:11 +02:00
|
|
|
mode=mode)
|
2022-05-19 18:15:11 +02:00
|
|
|
for _, item in df.iterrows())
|
2022-06-12 22:20:10 +02:00
|
|
|
|
2022-05-17 22:16:45 +02:00
|
|
|
return students
|
2022-06-13 00:16:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|