From 22499b7ece5591d4109381c3898523621fecf275 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 2 Nov 2020 13:32:44 +0100 Subject: [PATCH] [System][Plugin] Allow military timezone names for datetime --- flaschengeist/controller/messageController.py | 2 +- flaschengeist/controller/userController.py | 2 +- flaschengeist/plugins/__init__.py | 2 +- flaschengeist/plugins/balance/__init__.py | 8 ++++---- flaschengeist/utils/__init__.py | 0 flaschengeist/utils/datetime.py | 11 +++++++++++ flaschengeist/{ => utils}/hook.py | 0 7 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 flaschengeist/utils/__init__.py create mode 100644 flaschengeist/utils/datetime.py rename flaschengeist/{ => utils}/hook.py (100%) diff --git a/flaschengeist/controller/messageController.py b/flaschengeist/controller/messageController.py index 16976d3..f43afc8 100644 --- a/flaschengeist/controller/messageController.py +++ b/flaschengeist/controller/messageController.py @@ -1,4 +1,4 @@ -from flaschengeist.hook import Hook +from flaschengeist.utils.hook import Hook from flaschengeist.models.user import User, Role diff --git a/flaschengeist/controller/userController.py b/flaschengeist/controller/userController.py index d1ff44a..9106032 100644 --- a/flaschengeist/controller/userController.py +++ b/flaschengeist/controller/userController.py @@ -1,7 +1,7 @@ from flask import current_app from werkzeug.exceptions import NotFound, BadRequest -from flaschengeist.hook import Hook +from flaschengeist.utils.hook import Hook from flaschengeist.models.user import User, Role from flaschengeist.database import db from flaschengeist import logger diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index 7e13aca..349cc32 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -1,7 +1,7 @@ import pkg_resources from werkzeug.exceptions import MethodNotAllowed -from flaschengeist.hook import HookCall +from flaschengeist.utils.hook import HookCall send_message_hook = HookCall("send_message") """Hook decorator for sending messages, register to send the message diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index 1b89504..295342f 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -7,17 +7,16 @@ from http.client import NO_CONTENT from datetime import datetime, timezone from flask import Blueprint, jsonify, request from werkzeug.exceptions import Forbidden, BadRequest -from backports.datetime_fromisoformat import MonkeyPatch from flaschengeist import logger from flaschengeist.models.session import Session +from flaschengeist.utils.datetime import from_iso_format from flaschengeist.decorator import login_required from flaschengeist.controller import userController from flaschengeist.plugins import Plugin, update_user_hook from . import balance_controller, permissions, models -MonkeyPatch.patch_fromisoformat() balance_bp = Blueprint("balance", __name__) @@ -36,6 +35,7 @@ class BalancePlugin(Plugin): def install(self): from flaschengeist.database import db + db.create_all() @@ -115,13 +115,13 @@ def get_balance(userid, current_session: Session): start = request.args.get("from") if start: - start = datetime.fromisoformat(start).replace(tzinfo=timezone.utc) + start = from_iso_format(start) else: start = datetime.fromtimestamp(0, tz=timezone.utc) end = request.args.get("to") if end: - end = datetime.fromisoformat(end).replace(tzinfo=timezone.utc) + end = from_iso_format(end) else: end = datetime.now(tz=timezone.utc) diff --git a/flaschengeist/utils/__init__.py b/flaschengeist/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flaschengeist/utils/datetime.py b/flaschengeist/utils/datetime.py new file mode 100644 index 0000000..eb2ba82 --- /dev/null +++ b/flaschengeist/utils/datetime.py @@ -0,0 +1,11 @@ +import datetime +from backports.datetime_fromisoformat import MonkeyPatch +MonkeyPatch.patch_fromisoformat() + + +def from_iso_format(date_str): + """Z-suffix aware version of `datetime.datetime.fromisoformat`""" + time = datetime.datetime.fromisoformat(date_str.replace('Z', '+00:00')) + if time.tzinfo: + return time.astimezone(tzinfo=datetime.timezone.utc) + return time.replace(tzinfo=datetime.timezone.utc) diff --git a/flaschengeist/hook.py b/flaschengeist/utils/hook.py similarity index 100% rename from flaschengeist/hook.py rename to flaschengeist/utils/hook.py