[Plugin] schedule: Restructure plugin

This commit is contained in:
Ferdinand Thiessen 2020-11-01 18:43:13 +01:00
parent 67fb895cf4
commit 63660743bd
4 changed files with 41 additions and 32 deletions

View File

@ -3,31 +3,21 @@ from datetime import datetime, timedelta
from flask import Blueprint, request, jsonify
from werkzeug.exceptions import BadRequest, NotFound
from flaschengeist.plugins import Plugin
from flaschengeist.database import db
from flaschengeist.models.event import EventKind
from flaschengeist.plugins import Plugin
from flaschengeist.decorator import login_required
from flaschengeist.controller import eventController
from .models import EventKind
from . import event_controller, permissions
schedule_bp = Blueprint("schedule", __name__, url_prefix="/schedule")
_permission_edit_type = "schedule_edit_type"
_permission_edit = "schedule_edit"
_permission_create = "schedule_create"
_permission_delete = "schedule_delete"
_permission_assign = "schedule_assign_other"
class SchedulePlugin(Plugin):
def __init__(self, config):
super().__init__(
blueprint=schedule_bp,
permissions=[
_permission_create,
_permission_edit,
_permission_edit_type,
_permission_delete,
_permission_assign,
],
permissions=permissions.permissions,
)
@ -56,7 +46,7 @@ class SchedulePlugin(Plugin):
@schedule_bp.route("/events/<int:eid>", methods=["GET"])
@login_required()
def __get_event(eid, **kwargs):
event = eventController.get_event(eid)
event = event_controller.get_event(eid)
if not event:
raise NotFound
return jsonify(event)
@ -91,7 +81,7 @@ def __get_events(year=datetime.now().year, month=datetime.now().month, day=None,
else:
end = datetime(year=year, month=month + 1, day=1)
events = eventController.get_events(begin, end)
events = event_controller.get_events(begin, end)
return jsonify(events)
except ValueError:
raise BadRequest("Invalid date given")
@ -103,7 +93,7 @@ def __new_event_kind(**kwargs):
data = request.get_json()
if "name" not in data:
raise BadRequest
kind = eventController.create_event_kind(data["name"])
kind = event_controller.create_event_kind(data["name"])
return jsonify({"ok": "ok", "id": kind.id})
@ -113,7 +103,7 @@ def __new_slot_kind(**kwargs):
data = request.get_json()
if not data or "name" not in data:
raise BadRequest
kind = eventController.create_job_kind(data["name"])
kind = event_controller.create_job_kind(data["name"])
return jsonify({"ok": "ok", "id": kind.id})
@ -121,7 +111,7 @@ def __new_slot_kind(**kwargs):
@login_required(permission=_permission_create)
def __new_event(**kwargs):
data = request.get_json()
event = eventController.create_event(
event = event_controller.create_event(
begin=parser.isoparse(data["begin"]),
end=parser.isoparse(data["end"]),
description=data["description"],
@ -133,7 +123,7 @@ def __new_event(**kwargs):
@schedule_bp.route("/events/<int:event_id>", methods=["DELETE"])
@login_required(permission=_permission_delete)
def __delete_event(event_id, **kwargs):
if not eventController.delete_event(event_id):
if not event_controller.delete_event(event_id):
raise NotFound
db.session.commit()
return jsonify({"ok": "ok"})
@ -145,14 +135,14 @@ def __edit_event_kind(event_id, **kwargs):
data = request.get_json()
if not data or "name" not in data:
raise BadRequest
eventController.rename_event_kind(event_id, data["name"])
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):
event = eventController.get_event(event_id)
event = event_controller.get_event(event_id)
if not event:
raise NotFound
return jsonify({event.slots})
@ -161,7 +151,7 @@ def __get_slots(event_id, **kwargs):
@schedule_bp.route("/events/<int:event_id>/slots/<int:slot_id>", methods=["GET"])
@login_required()
def __get_slot(event_id, slot_id, **kwargs):
slot = eventController.get_event_slot(slot_id, event_id)
slot = event_controller.get_event_slot(slot_id, event_id)
if slot:
return jsonify(slot)
raise NotFound
@ -170,7 +160,7 @@ def __get_slot(event_id, slot_id, **kwargs):
@schedule_bp.route("/events/<int:event_id>/slots/<int:slot_id>", methods=["DELETE"])
@login_required(permission=_permission_delete)
def __delete_slot(event_id, slot_id, **kwargs):
if eventController.delete_event_slot(slot_id, event_id):
if event_controller.delete_event_slot(slot_id, event_id):
return jsonify({"ok": "ok"})
raise NotFound
@ -183,8 +173,8 @@ def __update_slot(event_id, slot_id, **kwargs):
raise BadRequest
for job in data["jobs"]:
eventController.add_job(job.kind, job.user)
if eventController.delete_event_slot(slot_id, event_id):
event_controller.add_job(job.kind, job.user)
if event_controller.delete_event_slot(slot_id, event_id):
return jsonify({"ok": "ok"})
raise NotFound
@ -192,7 +182,7 @@ def __update_slot(event_id, slot_id, **kwargs):
@schedule_bp.route("/events/<int:event_id>/slots", methods=["POST"])
@login_required(permission=_permission_edit)
def __add_slot(event_id, **kwargs):
event = eventController.get_event(event_id)
event = event_controller.get_event(event_id)
if not event:
raise NotFound
data = request.get_json()
@ -206,7 +196,7 @@ def __add_slot(event_id, **kwargs):
attr["job_slots"].append({"needed_persons": job["needed_persons"], "kind": job["kind"]})
except KeyError:
raise BadRequest("Missing data in request")
eventController.add_slot(event, **attr)
event_controller.add_slot(event, **attr)
return jsonify({"ok": "ok"})

View File

@ -3,7 +3,7 @@ from sqlalchemy.exc import IntegrityError
from flaschengeist import logger
from flaschengeist.database import db
from flaschengeist.models.event import EventKind, Event, EventSlot, JobSlot, JobKind
from flaschengeist.plugins.schedule.models import EventKind, Event, EventSlot, JobSlot, JobKind
def get_event(id):

View File

@ -1,8 +1,8 @@
from datetime import datetime
from typing import Optional
from . import ModelSerializeMixin
from .user import User
from flaschengeist.models import ModelSerializeMixin
from flaschengeist.models.user import User
from flaschengeist.database import db

View File

@ -0,0 +1,19 @@
CREATE = "schedule_create"
"""Can create events"""
EDIT = "schedule_edit"
"""Can edit events"""
DELETE = "schedule_delete"
"""Can delete events"""
EVENT_TYPE = "schedule_event_type"
"""Can create and edit EventTypes"""
ASSIGN = "schedule_assign"
"""Can self assign to jobs"""
ASSIGN_OTHER = "schedule_assign_other"
"""Can assign other users to jobs"""
permissions = [value for key, value in globals().items() if not key.startswith("_")]