From 48a15f55fa8d971c623220292d1528cad8a54f12 Mon Sep 17 00:00:00 2001 From: ReimarBauer Date: Tue, 14 May 2024 13:44:07 +0200 Subject: [PATCH] Use isoformat for mscolab timestamps (#2345) --- mslib/mscolab/chat_manager.py | 2 +- mslib/mscolab/file_manager.py | 2 +- mslib/mscolab/server.py | 2 +- mslib/mscolab/utils.py | 2 +- mslib/msui/mscolab_chat.py | 2 +- mslib/msui/mscolab_version_history.py | 2 +- tests/_test_mscolab/test_sockets_manager.py | 10 ++++------ tests/_test_mscolab/test_utils.py | 7 +++++-- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/mslib/mscolab/chat_manager.py b/mslib/mscolab/chat_manager.py index 95fb3da10..475ed5200 100644 --- a/mslib/mscolab/chat_manager.py +++ b/mslib/mscolab/chat_manager.py @@ -64,7 +64,7 @@ def get_messages(self, op_id, timestamp=None): if timestamp is None: timestamp = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc) else: - timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%d, %H:%M:%S.%f %z") + timestamp = datetime.datetime.fromisoformat(timestamp) messages = Message.query \ .filter(Message.op_id == op_id) \ .filter(Message.reply_id.is_(None)) \ diff --git a/mslib/mscolab/file_manager.py b/mslib/mscolab/file_manager.py index 202d57072..e1a61f979 100644 --- a/mslib/mscolab/file_manager.py +++ b/mslib/mscolab/file_manager.py @@ -399,7 +399,7 @@ def get_all_changes(self, op_id, user, named_version=False): 'comment': change.comment, 'version_name': change.version_name, 'username': change.user.username, - 'created_at': change.created_at.strftime("%Y-%m-%d, %H:%M:%S.%f %z") + 'created_at': change.created_at.isoformat() }, changes)) def get_change_content(self, ch_id, user): diff --git a/mslib/mscolab/server.py b/mslib/mscolab/server.py index c7e36747b..568e78af4 100644 --- a/mslib/mscolab/server.py +++ b/mslib/mscolab/server.py @@ -369,7 +369,7 @@ def messages(): user = g.user op_id = request.args.get("op_id", request.form.get("op_id", None)) if fm.is_member(user.id, op_id): - timestamp = request.args.get("timestamp", request.form.get("timestamp", "1970-01-01, 00:00:00.000000 +00:00")) + timestamp = request.args.get("timestamp", request.form.get("timestamp", "1970-01-01T00:00:00+00:00")) chat_messages = cm.get_messages(op_id, timestamp) return jsonify({"messages": chat_messages}) return "False" diff --git a/mslib/mscolab/utils.py b/mslib/mscolab/utils.py index 5a5e3b1e7..865297939 100644 --- a/mslib/mscolab/utils.py +++ b/mslib/mscolab/utils.py @@ -55,7 +55,7 @@ def get_message_dict(message): "message_type": message.message_type, "reply_id": message.reply_id, "replies": [], - "time": message.created_at.strftime("%Y-%m-%d, %H:%M:%S.%f %z") + "time": message.created_at.isoformat() } diff --git a/mslib/msui/mscolab_chat.py b/mslib/msui/mscolab_chat.py index e1e7e8d1c..bb74e314a 100644 --- a/mslib/msui/mscolab_chat.py +++ b/mslib/msui/mscolab_chat.py @@ -350,7 +350,7 @@ def load_all_messages(self): "token": self.token, "op_id": self.op_id, "timestamp": datetime.datetime(1970, 1, 1, - tzinfo=datetime.timezone.utc).strftime("%Y-%m-%d, %H:%M:%S.%f %z") + tzinfo=datetime.timezone.utc).isoformat() } # returns an array of messages url = urljoin(self.mscolab_server_url, "messages") diff --git a/mslib/msui/mscolab_version_history.py b/mslib/msui/mscolab_version_history.py index 1890f5994..f2b4b9cd9 100644 --- a/mslib/msui/mscolab_version_history.py +++ b/mslib/msui/mscolab_version_history.py @@ -144,7 +144,7 @@ def load_all_changes(self): changes = json.loads(r.text)["changes"] self.changes.clear() for change in changes: - created_at = datetime.strptime(change["created_at"], "%Y-%m-%d, %H:%M:%S.%f %z") + created_at = datetime.fromisoformat(change["created_at"]) local_time = utc_to_local_datetime(created_at) date = local_time.strftime('%d/%m/%Y') time = local_time.strftime('%I:%M %p') diff --git a/tests/_test_mscolab/test_sockets_manager.py b/tests/_test_mscolab/test_sockets_manager.py index 283c416ab..cdffb9303 100644 --- a/tests/_test_mscolab/test_sockets_manager.py +++ b/tests/_test_mscolab/test_sockets_manager.py @@ -192,11 +192,11 @@ def test_get_messages(self): assert len(messages) == 2 assert messages[0]["u_id"] == self.user.id timestamp = datetime.datetime(1970, 1, 1, - tzinfo=datetime.timezone.utc).strftime("%Y-%m-%d, %H:%M:%S.%f %z") + tzinfo=datetime.timezone.utc).isoformat() messages = self.cm.get_messages(1, timestamp) assert len(messages) == 2 assert messages[0]["u_id"] == self.user.id - timestamp = datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y-%m-%d, %H:%M:%S.%f %z") + timestamp = datetime.datetime.now(tz=datetime.timezone.utc).isoformat() messages = self.cm.get_messages(1, timestamp) assert len(messages) == 0 @@ -222,8 +222,7 @@ def test_get_messages_api(self): data = { "token": token, "op_id": self.operation.id, - "timestamp": datetime.datetime(1970, 1, 1, - tzinfo=datetime.timezone.utc).strftime("%Y-%m-%d, %H:%M:%S.%f %z") + "timestamp": datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc).isoformat() } # returns an array of messages url = urljoin(self.url, 'messages') @@ -259,8 +258,7 @@ def test_edit_message(self): data = { "token": token, "op_id": self.operation.id, - "timestamp": datetime.datetime(1970, 1, 1, - tzinfo=datetime.timezone.utc).strftime("%Y-%m-%d, %H:%M:%S.%f %z") + "timestamp": datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc).isoformat() } # returns an array of messages url = urljoin(self.url, 'messages') diff --git a/tests/_test_mscolab/test_utils.py b/tests/_test_mscolab/test_utils.py index 518c46b8f..f02540542 100644 --- a/tests/_test_mscolab/test_utils.py +++ b/tests/_test_mscolab/test_utils.py @@ -31,7 +31,9 @@ from mslib.mscolab.conf import mscolab_settings from mslib.mscolab.models import Operation, MessageType from mslib.mscolab.seed import add_user, get_user -from mslib.mscolab.utils import get_recent_op_id, get_session_id, get_message_dict, create_files, os_fs_create_dir +from mslib.mscolab.utils import (get_recent_op_id, get_session_id, + get_message_dict, create_files, + os_fs_create_dir) class Message: @@ -46,7 +48,8 @@ class user: replies = [] class created_at: - def strftime(value): + @staticmethod + def isoformat(): pass