feature/events #18

Merged
crimsen merged 4 commits from feature/events into develop 2021-11-25 17:00:41 +00:00
2 changed files with 11 additions and 60 deletions
Showing only changes of commit 1c091311de - Show all commits

View File

@ -147,7 +147,7 @@ def get_events(
offset: Optional[int] = None, offset: Optional[int] = None,
descending: Optional[bool] = False, descending: Optional[bool] = False,
with_backup=False, with_backup=False,
): ) -> Tuple[int, list[Event]]:
"""Query events which start from begin until end """Query events which start from begin until end
Args: Args:
start (datetime): Earliest start start (datetime): Earliest start
@ -161,10 +161,14 @@ def get_events(
query = query.filter(start <= Event.start) query = query.filter(start <= Event.start)
if end is not None: if end is not None:
query = query.filter(Event.start < end) 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: if descending:
query = query.order_by(Event.start.desc()) query = query.order_by(Event.start.desc())
else: else:
query = query.order_by(Event.start) query = query.order_by(Event.start)
count = query.count()
if limit is not None: if limit is not None:
query = query.limit(limit) query = query.limit(limit)
if offset is not None and offset > 0: if offset is not None and offset > 0:
@ -173,7 +177,7 @@ def get_events(
if not with_backup: if not with_backup:
for event in events: for event in events:
clear_backup(event) clear_backup(event)
return events return count, events
def delete_event(event_id): def delete_event(event_id):

View File

@ -1,8 +1,6 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from http.client import NO_CONTENT from http.client import NO_CONTENT
from re import template
from flask import request, jsonify from flask import request, jsonify
from sqlalchemy import exc
from werkzeug.exceptions import BadRequest, NotFound, Forbidden from werkzeug.exceptions import BadRequest, NotFound, Forbidden
from flaschengeist.models.session import Session from flaschengeist.models.session import Session
@ -12,7 +10,7 @@ from flaschengeist.utils.datetime import from_iso_format
from flaschengeist.controller import userController from flaschengeist.controller import userController
from . import event_controller, permissions, EventPlugin 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): 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"]) @EventPlugin.blueprint.route("/events", methods=["GET"])
@login_required() @login_required()
def get_filtered_events(current_session): def get_events(current_session):
begin = request.args.get("from", type=from_iso_format) count, result = event_controller.get_events(
end = request.args.get("to", type=from_iso_format) *get_filter_args(),
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,
with_backup=current_session.user_.has_permission(permissions.SEE_BACKUP), with_backup=current_session.user_.has_permission(permissions.SEE_BACKUP),
) )
) return jsonify({"count": count, "result": result})
@EventPlugin.blueprint.route("/events/<int:year>/<int:month>", methods=["GET"])
@EventPlugin.blueprint.route("/events/<int:year>/<int:month>/<int:day>", 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[/<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)
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")
def _add_job(event, data): def _add_job(event, data):