def gerom(app, manager, user_ds, db): """ Create a test user. Gerom has a confirmed account, but is not logged in. """ gerom = user_ds.create_user( email="*****@*****.**", password=hash_password("password"), confirmed_at=datetime.datetime.now(), active=True, ) schedule = md.Schedule( schd.Schedule(manager.get_default_project_id(), label="GEROM'S SCHEDULE"), user=gerom, ) db.session.add(schedule) db.session.commit() yield gerom db.session.delete(schedule) db.session.delete(gerom) db.session.commit()
def delete_schedule(id): id = int(id) schedule = current_user.get_schedule(id=id) if schedule is None and id != -1: return "", 403 # Requested id is not in this user's schedule list. if schedule is not None: current_user.remove_schedule(schedule) if id == session["current_schedule"].id or id == -1: mng = app.config["MANAGER"] session["current_schedule"] = schd.Schedule(mng.get_default_project_id()) session["current_schedule_modified"] = True return ( jsonify( { "current_schedule": { "id": session["current_schedule"].id, "project_id": session["current_schedule"].project_id, "label": session["current_schedule"].label, "color_palette": session["current_schedule"].color_palette, }, "unsaved": session["current_schedule_modified"], } ), 200, )
def clear(): mng = app.config['MANAGER'] session['current_schedule'] = schd.Schedule(mng.get_default_project_id()) session['current_schedule_modified'] = False return jsonify({ 'label': _(session['current_schedule'].label), }), 200
def jyl(app, manager, user_ds, db): """ Create a test user. JYL has a confirmed account and is logged in. """ jyl = user_ds.create_user( email="*****@*****.**", password=hash_password("password"), confirmed_at=datetime.datetime.now(), active=True, ) data = schd.Schedule(manager.get_default_project_id(), label="JYL'S SCHEDULE") data.add_course("LEPL1104") active_schedule = md.Schedule(data, user=jyl) db.session.add(active_schedule) old_schedule = md.Schedule(schd.Schedule(manager.get_default_project_id(), label="OLD SCHEDULE"), user=jyl) db.session.add(old_schedule) db.session.commit() # Login user @app.login_manager.request_loader def load_user_from_request(request): utl.init_session() session["current_schedule"] = copy.copy(jyl.get_schedules()[0].data) return jyl yield jyl # Logout & delete user @app.login_manager.request_loader def load_user_from_request(request): return None db.session.delete(active_schedule) db.session.delete(old_schedule) db.session.delete(jyl) db.session.commit()
def when_user_logged_out(sender, user): # When pressing confirmation link, somehow this function is triggered # without an initialised session... utl.init_session() if session["current_schedule"].id is not None: user.set_last_schedule_id(session["current_schedule"].id) session["current_schedule"] = schd.Schedule( manager.get_default_project_id()) session["current_schedule_modified"] = False
def get_events(): """ API endpoint to fetch the schedule matching the various arguments. The request accepts 3 arguments: - view: if set to True, shows the schedule in ADE-Scheduler's schedule viewer. By default, view = False the data is returned in JSON format. - year: defines the academical year. Accepted format is YYYY-YYYY where the second year is the first + 1. e.g.: 2020-20201 - code: the various code of the courses to be added to the schedule. e.g.: LELEC2885, LMECA2170, LEPL1104,... To specify a list of codes, use the following format: /schedule?code=CODE_1&code=CODE_2&code=CODE_3 and so on. - filtered events: the IDs of the events you want to filter. Say the course which code is "CODE" has two TPs with IDs "TP: CODE-Q1A" and "TP: CODE-Q1B" and you want to filter out the events of "TP: CODE-Q1A", you can specify such a filter by specifying: /schedule?code=CODE&CODE=TP: CODE-Q1A&... /!\\ To make sure to avoid any problems, respect the case-sensitiveness of the codes and event IDs. Generally, those are uppercase. /!\\ Example: https://ade-scheduler.info.ucl.ac.be/api/events?year=2020-2021&code=LMECA2170&code=LEPL1104&view=true or, with filtered events: https://ade-scheduler.info.ucl.ac.be/api/events?year=2020-2021&code=LEPL1104&LEPL1104=TP: LEPL1104_Q2B-APE&view=true """ mng = app.config["MANAGER"] year = request.args.get("year") codes = request.args.getlist("code") view = request.args.get("view") if view is None: view = False else: view = bool(strtobool(request.args.get("view"))) project_id = mng.get_project_ids(year=year) if year is None or project_id is None: project_id = mng.get_default_project_id() schedule = schd.Schedule(project_id=project_id) schedule.add_course([code.upper() for code in codes]) for code in codes: schedule.add_filter(code.upper(), request.args.getlist(code)) if view: session["current_schedule"] = schedule return redirect(url_for("calendar.index")) return (jsonify({"events": schedule.get_events(json=True)}), 200)
def init_session(): mng = app.config["MANAGER"] if not session.get("uuid"): session["uuid"] = uuid.uuid4() if not session.get("current_schedule"): session["current_schedule"] = schd.Schedule( mng.get_default_project_id()) session["current_schedule_modified"] = False project_ids = [int(year["id"]) for year in mng.get_project_ids()] if int(session["current_schedule"].project_id) not in project_ids: session["current_schedule"].project_id = mng.get_default_project_id()
def clear(): mng = app.config["MANAGER"] session["current_schedule"] = schd.Schedule(mng.get_default_project_id()) session["current_schedule_modified"] = False return ( jsonify({ "current_schedule": { "id": session["current_schedule"].id, "label": gettext(session["current_schedule"].label), "color_palette": session["current_schedule"].color_palette, }, "current_project_id": session["current_schedule"].project_id, }), 200, )
def autoload_schedule(): if current_user.is_authenticated and session[ "current_schedule"].id is not None: schedule = current_user.get_schedule(id=session["current_schedule"].id) if schedule is None: mng = app.config["MANAGER"] session["current_schedule"] = schd.Schedule( mng.get_default_project_id()) session["current_schedule_modified"] = False return if (schedule.last_modified_by != session["uuid"] and schedule.last_modified_by is not None): session["current_schedule"] = schedule.data schedule.update_last_modified_by(session["uuid"])
def delete_schedule(id): id = int(id) schedule = current_user.get_schedule(id=id) if schedule is None and id is not -1: return '', 403 # Requested id is not in this user's schedule list. if schedule is not None: current_user.remove_schedule(schedule) if id is session['current_schedule'].id or id is -1: mng = app.config['MANAGER'] session['current_schedule'] = schd.Schedule( mng.get_default_project_id()) session['current_schedule_modified'] = True return jsonify({ 'current_schedule': { 'id': session['current_schedule'].id, 'project_id': session['current_schedule'].project_id, 'label': session['current_schedule'].label, 'color_palette': session['current_schedule'].color_palette, }, 'unsaved': session['current_schedule_modified'], }), 200
def when_user_logged_out(sender, user): session['current_schedule'] = schd.Schedule( manager.get_default_project_id()) session['current_schedule_modified'] = False
def load_user_from_request(request): utl.init_session() session["current_schedule"] = schd.Schedule( manager.get_default_project_id(), label="LOUWI'S SCHEDULE") return None
def before_calendar_request(): if not session.get('current_schedule'): mng = app.config['MANAGER'] session['current_schedule'] = schd.Schedule(mng.get_default_project_id()) session['current_schedule_modified'] = False