update upload students list endpoint and add registrations endpoint for students view

This commit is contained in:
dominik24c 2022-06-12 22:20:10 +02:00
parent a4a2807fd2
commit 30d8b7c0ef
7 changed files with 78 additions and 8 deletions

View File

@ -1,9 +1,11 @@
from flask import Blueprint
from .coordinator.routes import bp as coordinator_bp
from .project_supervisor.routes import bp as project_supervisor_bp
from .students.routes import bp as students_bp
api_bp = Blueprint('api', __name__, url_prefix='/api')
# register blueprints here
api_bp.register_blueprint(coordinator_bp)
api_bp.register_blueprint(project_supervisor_bp)
api_bp.register_blueprint(students_bp)

View File

@ -1,7 +1,9 @@
from flask import Blueprint
from .students import bp as students_bp
from .groups import bp as groups_bp
bp = Blueprint("coordinator", __name__, url_prefix="/coordinator")
bp.register_blueprint(students_bp)
bp.register_blueprint(groups_bp)

View File

@ -9,7 +9,7 @@ from ..students.models import Student
def check_columns(df: pd.DataFrame) -> bool:
headers = set(df.keys().values)
columns = ['NAZWISKO','IMIE','INDEKS','PESEL','EMAIL']
columns = ['NAZWISKO', 'IMIE', 'INDEKS', 'PESEL', 'EMAIL']
if len(headers - set(columns)) != 0:
return False
@ -26,17 +26,17 @@ def check_columns(df: pd.DataFrame) -> bool:
def parse_csv(file, mode) -> Generator[Student, Any, None]:
df = pd.read_csv(file, mode)
df = pd.read_csv(file)
# if not check_columns(df):
# raise InvalidNameOrTypeHeaderException
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'],
students = (Student(last_name=dict(item.items())['NAZWISKO'],
first_name=dict(item.items())['IMIE'],
index=dict(item.items())['INDEKS'],
pesel=str(int(dict(item.items())['PESEL'])) if not pd.isna(dict(item.items())['PESEL']) else None,
email=dict(item.items())['EMAIL'],
mode=mode)
for _, item in df.iterrows())
return students

View File

@ -0,0 +1,7 @@
from flask import Blueprint
from .registrations import bp as registrations_bp
bp = Blueprint("students", __name__, url_prefix="/students")
bp.register_blueprint(registrations_bp)

View File

@ -0,0 +1,39 @@
from apiflask import APIBlueprint
from flask import abort
from ...project_supervisor.models import ProjectSupervisor
from ..models import Group
from ...dependencies import db
from ..schemas import ProjectSupervisorPaginationSchema, ProjectSupervisorQuerySchema
from ...base.utils import paginate_models
bp = APIBlueprint("registrations", __name__, url_prefix="/registrations")
@bp.route('/', methods=['GET'])
@bp.input(ProjectSupervisorQuerySchema, location='query')
@bp.output(ProjectSupervisorPaginationSchema)
def list_available_groups(query: dict) -> dict:
mode = query.get('mode')
page = query.get('page')
per_page = query.get('per_page')
ps_query = db.session.query(ProjectSupervisor,
(ProjectSupervisor.count_groups - db.func.count(Group.id))).join(Group)
if mode is not None:
ps_query = ps_query.filter(ProjectSupervisor.mode == mode)
ps_query = ps_query.group_by(Group.id)
response = paginate_models(page, ps_query, per_page)
if (message := response.get('message')) is not None:
abort(response['status_code'], message)
project_supervisors = []
for project_supervisor, available_groups in response['items']:
setattr(project_supervisor, 'available_groups', available_groups)
project_supervisors.append(project_supervisor)
return {
"project_supervisors": project_supervisors,
"max_pages": response['max_pages']
}

View File

@ -0,0 +1,20 @@
from ..dependencies import ma
from marshmallow import fields
class ProjectSupervisorSchema(ma.Schema):
first_name = fields.Str()
last_name = fields.Str()
mode = fields.Boolean()
available_groups = fields.Integer()
class ProjectSupervisorPaginationSchema(ma.Schema):
project_supervisors = fields.List(fields.Nested(ProjectSupervisorSchema))
max_pages = fields.Integer()
class ProjectSupervisorQuerySchema(ma.Schema):
page = fields.Integer()
per_page = fields.Integer()
mode = fields.Boolean()