From 30305c26cc8b52a2237d3e2436605bfa8f43b7b6 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 24 Jan 2021 16:20:25 +0100 Subject: [PATCH] [Plugin] Schedule: Identify EventTypes by ID not name --- flaschengeist/plugins/schedule/__init__.py | 32 +++++++++++-------- .../plugins/schedule/event_controller.py | 13 +++++--- flaschengeist/plugins/schedule/models.py | 6 ++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/flaschengeist/plugins/schedule/__init__.py b/flaschengeist/plugins/schedule/__init__.py index 09234ef..5f5b0f0 100644 --- a/flaschengeist/plugins/schedule/__init__.py +++ b/flaschengeist/plugins/schedule/__init__.py @@ -32,20 +32,26 @@ class SchedulePlugin(Plugin): @schedule_bp.route("/event-types", methods=["GET"]) +@schedule_bp.route("/event-types/", methods=["GET"]) @login_required() -def get_event_types(current_session): - """Get all EventTypes +def get_event_types(current_session, identifier=None): + """Get EventType(s) Route: ``/schedule/event-types`` | Method: ``GET`` + Route: ``/schedule/event-types/`` | Method: ``GET`` Args: current_session: Session sent with Authorization Header + identifier: If querying a specific EventType Returns: - JSON encoded list of EventTypes HTTP-error + JSON encoded (list of) EventType(s) or HTTP-error """ - types = event_controller.get_event_types() - return jsonify(types) + if identifier: + result = event_controller.get_event_type(identifier) + else: + result = event_controller.get_event_types() + return jsonify(result) @schedule_bp.route("/event-types", methods=["POST"]) @@ -66,33 +72,33 @@ def new_event_type(current_session): data = request.get_json() if "name" not in data: raise BadRequest - event = event_controller.create_event_type(data["name"]) - return jsonify(event) + event_type = event_controller.create_event_type(data["name"]) + return jsonify(event_type) -@schedule_bp.route("/event-types/", methods=["PUT", "DELETE"]) +@schedule_bp.route("/event-types/", methods=["PUT", "DELETE"]) @login_required(permission=permissions.EVENT_TYPE) -def modify_event_type(name, current_session): +def modify_event_type(identifier, current_session): """Rename or delete an event type - Route: ``/schedule/event-types/`` | Method: ``PUT`` or ``DELETE`` + Route: ``/schedule/event-types/`` | Method: ``PUT`` or ``DELETE`` POST-data: (if renaming) ``{name: string}`` Args: - name: Name identifying the EventType + identifier: Identifier of 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) + event_controller.delete_event_type(identifier) else: data = request.get_json() if "name" not in data: raise BadRequest("Parameter missing in data") - event_controller.rename_event_type(name, data["name"]) + event_controller.rename_event_type(identifier, data["name"]) return "", NO_CONTENT diff --git a/flaschengeist/plugins/schedule/event_controller.py b/flaschengeist/plugins/schedule/event_controller.py index 44cd246..d88ca64 100644 --- a/flaschengeist/plugins/schedule/event_controller.py +++ b/flaschengeist/plugins/schedule/event_controller.py @@ -14,8 +14,13 @@ def get_event_types(): return EventType.query.all() -def get_event_type(name): - et = EventType.query.filter(EventType.name == name).one_or_none() +def get_event_type(identifier): + """Get EventType by ID (int) or name (string)""" + + if isinstance(identifier, int): + et = EventType.query.get(identifier) + else: + et = EventType.query.filter(EventType.name == identifier).one_or_none() if not et: raise NotFound return et @@ -31,8 +36,8 @@ def create_event_type(name): raise BadRequest("Name already exists") -def rename_event_type(name, new_name): - event_type = get_event_type(name) +def rename_event_type(identifier, new_name): + event_type = get_event_type(identifier) event_type.name = new_name try: db.session.commit() diff --git a/flaschengeist/plugins/schedule/models.py b/flaschengeist/plugins/schedule/models.py index b78ecb3..1c7fa27 100644 --- a/flaschengeist/plugins/schedule/models.py +++ b/flaschengeist/plugins/schedule/models.py @@ -14,13 +14,13 @@ from flaschengeist.database import db class EventType(db.Model, ModelSerializeMixin): __tablename__ = "schedule_event_type" - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column(db.Integer, primary_key=True) name: str = db.Column(db.String(30), nullable=False, unique=True) class JobType(db.Model, ModelSerializeMixin): __tablename__ = "schedule_job_type" - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column(db.Integer, primary_key=True) name: str = db.Column(db.String(30), nullable=False, unique=True) @@ -50,7 +50,7 @@ class Job(db.Model, ModelSerializeMixin): _type_id = db.Column("type_id", db.Integer, db.ForeignKey("schedule_job_type.id"), nullable=False) _event_id = db.Column("event_id", db.Integer, db.ForeignKey("schedule_event.id"), nullable=False) - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column(db.Integer, primary_key=True) start: datetime = db.Column(UtcDateTime, nullable=False) end: Optional[datetime] = db.Column(UtcDateTime) comment: str = db.Column(db.String(256))