[Plugin] schedule: Added EventType and JobType support

This commit is contained in:
Ferdinand Thiessen 2020-11-02 03:59:18 +01:00
parent 7dec0144d9
commit 363ec6530b
3 changed files with 182 additions and 33 deletions

View File

@ -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/<name>", 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/<name>`` | 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/<name>", methods=["PUT", "DELETE"])
@login_required(permission=permissions.JOB_TYPE)
def modify_job_type(name, current_session):
"""Rename or delete a JobType
Route: ``/job-types/<name>`` | 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/<int:event_id>", 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/<int:year>/<int:month>", methods=["GET"])
@schedule_bp.route("/events/<int:year>/<int:month>/<int:day>", 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[/<year>/<month>[/<int:day>]]`` | 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/<int:event_id>", 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/<int:event_id>/slots", methods=["GET"])
@login_required()
def __get_slots(event_id, **kwargs):

View File

@ -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)

View File

@ -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"""