Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use isoformat for mscolab timestamps #2345

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mslib/mscolab/chat_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)) \
Expand Down
3 changes: 2 additions & 1 deletion mslib/mscolab/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import git
import threading
from sqlalchemy.exc import IntegrityError
from mslib.mscolab.utils import created_at_isoformat
from mslib.mscolab.models import db, Operation, Permission, User, Change, Message
from mslib.mscolab.conf import mscolab_settings

Expand Down Expand Up @@ -399,7 +400,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': created_at_isoformat(change.created_at)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is wrong with change.created_at.isoformat()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems I looked only on that with the None state.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What None state?

Copy link
Member Author

@ReimarBauer ReimarBauer May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on a test: test_get_message_dict

The function strftime returns None when created_at is initialized. But isoformat crashes.
But in that case isoformat gives an AttributeError

and that is because I need to change the test too.

}, changes))

def get_change_content(self, ch_id, user):
Expand Down
2 changes: 1 addition & 1 deletion mslib/mscolab/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion mslib/mscolab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,19 @@ 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": created_at_isoformat(message.created_at)
}


def created_at_isoformat(created_at):
if created_at is None:
return None
time_zone = created_at.strftime("%z")
formatted_tz = time_zone[:3] + ':' + time_zone[3:]
iso_format = created_at.strftime("%Y-%m-%dT%H:%M:%S.%f")
return f"{iso_format}{formatted_tz}"


def os_fs_create_dir(directory_path):
if '://' in directory_path:
try:
Expand Down
2 changes: 1 addition & 1 deletion mslib/msui/mscolab_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion mslib/msui/mscolab_version_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 4 additions & 6 deletions tests/_test_mscolab/test_sockets_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
9 changes: 8 additions & 1 deletion tests/_test_mscolab/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
import os
import pytest
import json
import datetime

from fs.tempfs import TempFS
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, created_at_isoformat, create_files,
os_fs_create_dir)


class Message:
matrss marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -80,6 +83,10 @@ def test_get_message_dict(self):
result = get_message_dict(Message())
assert result["message_type"] == MessageType.TEXT

def test_created_at_isoformat(self):
now = datetime.datetime.now(datetime.timezone.utc)
assert now.isoformat() == created_at_isoformat(now)

def test_os_fs_create_dir(self):
_fs = TempFS(identifier="msui")
_dir = _fs.getsyspath("")
Expand Down
Loading