From 1c091311de7d3250b596d8b2ed9e6053c53d07a0 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 25 Nov 2021 15:43:02 +0100 Subject: [PATCH] [events] Use new pagination responses, drop unused api endpoint --- .../plugins/events/event_controller.py | 8 ++- flaschengeist/plugins/events/routes.py | 63 ++----------------- 2 files changed, 11 insertions(+), 60 deletions(-) diff --git a/flaschengeist/plugins/events/event_controller.py b/flaschengeist/plugins/events/event_controller.py index 4cd2d03..8de2ed7 100644 --- a/flaschengeist/plugins/events/event_controller.py +++ b/flaschengeist/plugins/events/event_controller.py @@ -147,7 +147,7 @@ def get_events( offset: Optional[int] = None, descending: Optional[bool] = False, with_backup=False, -): +) -> Tuple[int, list[Event]]: """Query events which start from begin until end Args: start (datetime): Earliest start @@ -161,10 +161,14 @@ def get_events( query = query.filter(start <= Event.start) if end is not None: query = query.filter(Event.start < end) + elif start is None: + # Neither start nor end was given + query = query.filter(datetime.now() <= Event.start) if descending: query = query.order_by(Event.start.desc()) else: query = query.order_by(Event.start) + count = query.count() if limit is not None: query = query.limit(limit) if offset is not None and offset > 0: @@ -173,7 +177,7 @@ def get_events( if not with_backup: for event in events: clear_backup(event) - return events + return count, events def delete_event(event_id): diff --git a/flaschengeist/plugins/events/routes.py b/flaschengeist/plugins/events/routes.py index 2fb9b60..71c18d9 100644 --- a/flaschengeist/plugins/events/routes.py +++ b/flaschengeist/plugins/events/routes.py @@ -1,8 +1,6 @@ from datetime import datetime, timedelta, timezone from http.client import NO_CONTENT -from re import template from flask import request, jsonify -from sqlalchemy import exc from werkzeug.exceptions import BadRequest, NotFound, Forbidden from flaschengeist.models.session import Session @@ -12,7 +10,7 @@ from flaschengeist.utils.datetime import from_iso_format from flaschengeist.controller import userController from . import event_controller, permissions, EventPlugin -from ...utils.HTTP import no_content +from ...utils.HTTP import get_filter_args, no_content def dict_get(self, key, default=None, type=None): @@ -194,63 +192,12 @@ def get_event(event_id, current_session): @EventPlugin.blueprint.route("/events", methods=["GET"]) @login_required() -def get_filtered_events(current_session): - begin = request.args.get("from", type=from_iso_format) - end = request.args.get("to", type=from_iso_format) - limit = request.args.get("limit", type=int) - offset = request.args.get("offset", type=int) - descending = "descending" in request.args - if begin is None and end is None: - begin = datetime.now() - return jsonify( - event_controller.get_events( - start=begin, - end=end, - limit=limit, - offset=offset, - descending=descending, +def get_events(current_session): + count, result = event_controller.get_events( + *get_filter_args(), with_backup=current_session.user_.has_permission(permissions.SEE_BACKUP), - ) ) - - -@EventPlugin.blueprint.route("/events//", methods=["GET"]) -@EventPlugin.blueprint.route("/events///", methods=["GET"]) -@login_required() -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) - current_session: Session sent with Authorization Header - - Returns: - JSON encoded list containing events found or HTTP-error - """ - try: - begin = datetime(year=year, month=month, day=1, tzinfo=timezone.utc) - if day: - begin += timedelta(days=day - 1) - end = begin + timedelta(days=1) - else: - if month == 12: - end = datetime(year=year + 1, month=1, day=1, tzinfo=timezone.utc) - else: - end = datetime(year=year, month=month + 1, day=1, tzinfo=timezone.utc) - - events = event_controller.get_events( - begin, - end, - with_backup=current_session.user_.has_permission(permissions.SEE_BACKUP), - ) - return jsonify(events) - except ValueError: - raise BadRequest("Invalid date given") + return jsonify({"count": count, "result": result}) def _add_job(event, data):