From 363ec6530be7842e987566573ea39f6f605cc432 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 2 Nov 2020 03:59:18 +0100 Subject: [PATCH] [Plugin] schedule: Added EventType and JobType support --- flaschengeist/plugins/schedule/__init__.py | 173 ++++++++++++++---- .../plugins/schedule/event_controller.py | 39 ++++ flaschengeist/plugins/schedule/permissions.py | 3 + 3 files changed, 182 insertions(+), 33 deletions(-) diff --git a/flaschengeist/plugins/schedule/__init__.py b/flaschengeist/plugins/schedule/__init__.py index a4dcaa1..7c539c4 100644 --- a/flaschengeist/plugins/schedule/__init__.py +++ b/flaschengeist/plugins/schedule/__init__.py @@ -2,6 +2,8 @@ Provides duty schedule / duty roster functions """ +import http + from dateutil import parser from datetime import datetime, timedelta from flask import Blueprint, request, jsonify @@ -25,6 +27,137 @@ class SchedulePlugin(Plugin): ) +@schedule_bp.route("/event-types", methods=["GET"]) +@login_required() +def get_event_types(current_session): + """Get all EventTypes + + Route: ``/event-types`` | Method: ``GET`` + + Args: + current_session: Session sent with Authorization Header + + Returns: + JSON encoded list of EventTypes HTTP-error + """ + types = event_controller.get_event_types() + return jsonify(types) + + +@schedule_bp.route("/event-types", methods=["POST"]) +@login_required(permission=permissions.EVENT_TYPE) +def new_event_type(current_session): + """Create a new EventType + + Route: ``/event-types`` | Method: ``POST`` + + POST-data: ``{name: string}`` + + Args: + current_session: Session sent with Authorization Header + + Returns: + HTTP-Created or HTTP-error + """ + data = request.get_json() + if "name" not in data: + raise BadRequest + event_controller.create_event_type(data["name"]) + return "", http.HTTPStatus.CREATED + + +@schedule_bp.route("/event-types/", methods=["PUT", "DELETE"]) +@login_required(permission=permissions.EVENT_TYPE) +def modify_event_type(name, current_session): + """Rename or delete an event type + + Route: ``/event-types/`` | Method: ``PUT`` or ``DELETE`` + + POST-data: (if renaming) ``{name: string}`` + + Args: + name: Name identifying the EventType + current_session: Session sent with Authorization Header + + Returns: + HTTP-NoContent or HTTP-error + """ + if request.method == "DELETE": + event_controller.delete_event_type(name) + else: + data = request.get_json() + if "name" not in data: + raise BadRequest("Parameter missing in data") + event_controller.rename_event_type(name, data["name"]) + return "", http.HTTPStatus.NO_CONTENT + + +@schedule_bp.route("/job-types", methods=["GET"]) +@login_required() +def get_job_types(current_session): + """Get all JobTypes + + Route: ``/job-types`` | Method: ``GET`` + + Args: + current_session: Session sent with Authorization Header + + Returns: + JSON encoded list of JobType HTTP-error + """ + types = event_controller.get_job_types() + return jsonify(types) + + +@schedule_bp.route("/job-types", methods=["POST"]) +@login_required(permission=permissions.JOB_TYPE) +def new_job_type(current_session): + """Create a new JobType + + Route: ``/job-types`` | Method: ``POST`` + + POST-data: ``{name: string}`` + + Args: + current_session: Session sent with Authorization Header + + Returns: + HTTP-Created or HTTP-error + """ + data = request.get_json() + if "name" not in data: + raise BadRequest + event_controller.create_job_type(data["name"]) + return "", http.HTTPStatus.CREATED + + +@schedule_bp.route("/job-types/", methods=["PUT", "DELETE"]) +@login_required(permission=permissions.JOB_TYPE) +def modify_job_type(name, current_session): + """Rename or delete a JobType + + Route: ``/job-types/`` | Method: ``PUT`` or ``DELETE`` + + POST-data: (if renaming) ``{name: string}`` + + Args: + name: Name identifying the JobType + current_session: Session sent with Authorization Header + + Returns: + HTTP-NoContent or HTTP-error + """ + if request.method == "DELETE": + event_controller.delete_name_type(name) + else: + data = request.get_json() + if "name" not in data: + raise BadRequest("Parameter missing in data") + event_controller.rename_name_type(name, data["name"]) + return "", http.HTTPStatus.NO_CONTENT + + +########### TODO: Ab hier ############ @schedule_bp.route("/events/", methods=["GET"]) @login_required() def get_event(event_id, current_session): @@ -49,17 +182,21 @@ def get_event(event_id, current_session): @schedule_bp.route("/events//", methods=["GET"]) @schedule_bp.route("/events///", methods=["GET"]) @login_required() -def __get_events(year=datetime.now().year, month=datetime.now().month, day=None, **kwargs): +def get_events(current_session, year=datetime.now().year, month=datetime.now().month, day=None): """Get Event objects for specified date (or month or year), if nothing set then events for current month are returned + Route: ``/events[//[/]]`` | Method: ``GET`` + Args: year (int, optional): year to query, defaults to current year month (int, optional): month to query (if set), defaults to current month day (int, optional): day to query events for (if set) - **kwargs: contains at least current_session (see flaschengeist.decorator) + current_session: Session sent with Authorization Header + Returns: - JSON list containing events found + JSON encoded list containing events found or HTTP-error + Raises: BadRequest: If date is invalid """ @@ -80,26 +217,6 @@ def __get_events(year=datetime.now().year, month=datetime.now().month, day=None, raise BadRequest("Invalid date given") -@schedule_bp.route("/event-types", methods=["POST"]) -@login_required(permission=permissions.EVENT_TYPE) -def __new_event_kind(**kwargs): - data = request.get_json() - if "name" not in data: - raise BadRequest - kind = event_controller.create_event_kind(data["name"]) - return jsonify({"ok": "ok", "id": kind.id}) - - -@schedule_bp.route("/slotKinds", methods=["POST"]) -@login_required(permission=permissions.EVENT_TYPE) -def __new_slot_kind(**kwargs): - data = request.get_json() - if not data or "name" not in data: - raise BadRequest - kind = event_controller.create_job_kind(data["name"]) - return jsonify({"ok": "ok", "id": kind.id}) - - @schedule_bp.route("/events", methods=["POST"]) @login_required(permission=permissions.CREATE) def __new_event(**kwargs): @@ -122,16 +239,6 @@ def __delete_event(event_id, **kwargs): return jsonify({"ok": "ok"}) -@schedule_bp.route("/eventKinds/", methods=["PUT"]) -@login_required(permission=permissions.EVENT_TYPE) -def __edit_event_kind(event_id, **kwargs): - data = request.get_json() - if not data or "name" not in data: - raise BadRequest - event_controller.rename_event_kind(event_id, data["name"]) - return jsonify({"ok": "ok"}) - - @schedule_bp.route("/events//slots", methods=["GET"]) @login_required() def __get_slots(event_id, **kwargs): diff --git a/flaschengeist/plugins/schedule/event_controller.py b/flaschengeist/plugins/schedule/event_controller.py index aa086af..8436285 100644 --- a/flaschengeist/plugins/schedule/event_controller.py +++ b/flaschengeist/plugins/schedule/event_controller.py @@ -45,6 +45,45 @@ def delete_event_type(name): raise BadRequest("Type still in use") +def get_job_types(): + return JobType.query.all() + + +def get_job_type(name): + job_type = JobType.query.filter(JobType.name == name).one_or_none() + if not job_type: + raise NotFound + return job_type + + +def create_job_type(name): + try: + job_type = JobType(name=name) + db.session.add(job_type) + db.session.commit() + return job_type + except IntegrityError: + raise BadRequest("Name already exists") + + +def rename_job_type(name, new_name): + job_type = get_job_type(name) + job_type.name = new_name + try: + db.session.commit() + except IntegrityError: + raise BadRequest("Name already exists") + + +def delete_job_type(name): + job_type = get_job_type(name) + db.session.delete(job_type) + try: + db.session.commit() + except IntegrityError: + raise BadRequest("Type still in use") + + def get_event(id): return Event.query.get(id) diff --git a/flaschengeist/plugins/schedule/permissions.py b/flaschengeist/plugins/schedule/permissions.py index b73873a..f889793 100644 --- a/flaschengeist/plugins/schedule/permissions.py +++ b/flaschengeist/plugins/schedule/permissions.py @@ -10,6 +10,9 @@ DELETE = "schedule_delete" EVENT_TYPE = "schedule_event_type" """Can create and edit EventTypes""" +JOB_TYPE = "schedule_job_type" +"""Can create and edit JobTypes""" + ASSIGN = "schedule_assign" """Can self assign to jobs"""