From d8100b129e40a6319fa0264628b74c4315c5ecac Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 2 Nov 2020 16:30:10 +0100 Subject: [PATCH] [Plugin] schedule: Allow creating JobSlots from POST --- flaschengeist/plugins/schedule/__init__.py | 19 ++++++++++++------- .../plugins/schedule/event_controller.py | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/flaschengeist/plugins/schedule/__init__.py b/flaschengeist/plugins/schedule/__init__.py index 38cb80a..4b2a4de 100644 --- a/flaschengeist/plugins/schedule/__init__.py +++ b/flaschengeist/plugins/schedule/__init__.py @@ -213,14 +213,19 @@ def get_events(current_session, year=datetime.now().year, month=datetime.now().m raise BadRequest("Invalid date given") -def _event_slot_from_data(data): +def _add_event_slot(event, data): + end = None try: start = from_iso_format(data["start"]) - end = from_iso_format(data["end"]) if "end" in data else None - # jobs = ... - except (NotFound, KeyError, ValueError): + except (KeyError, ValueError): raise BadRequest("Missing POST parameter") - return {"start": start, "end": end} + if "end" in data: + end = from_iso_format(data["end"]) + event_slot = event_controller.add_event_slot(event, start, end) + if "jobs" in data: + for job_data in data["jobs"]: + job_type = event_controller.get_job_type(job_data["type"]) + event_controller.add_job_slot(event_slot, job_type, job_data["required_jobs"]) @schedule_bp.route("/events", methods=["POST"]) @@ -251,7 +256,7 @@ def create_event(current_session): ) if "slots" in data: for slot in data["slots"]: - event_controller.add_event_slot(event, **_event_slot_from_data(slot)) + _add_event_slot(event, slot) return jsonify(event) @@ -323,7 +328,7 @@ def add_event_slot(event_id, current_session): if not data: raise BadRequest("Missing POST parameters") - event_controller.add_event_slot(event, **_event_slot_from_data(data)) + _add_event_slot(event, data) return jsonify(event) diff --git a/flaschengeist/plugins/schedule/event_controller.py b/flaschengeist/plugins/schedule/event_controller.py index 0124ab5..8968049 100644 --- a/flaschengeist/plugins/schedule/event_controller.py +++ b/flaschengeist/plugins/schedule/event_controller.py @@ -144,6 +144,7 @@ def add_event_slot(event, start, end=None): raise BadRequest("Start before event start") db.session.add(event_slot) db.session.commit() + return event_slot def remove_event_slot(event, slot_id):