Skip to content

Commit

Permalink
Change: Message.time was float, becomes datetime
Browse files Browse the repository at this point in the history
Change the type if BaseMessage.time to a datetime. This allows easier manipulation of the field, and parsing from strings as well as floats.

New APIs introduced in PyAleph 0.5.0 store the message time as timestampz and return it as ISO strings. This changes is compatible with both the old and new APIs.
  • Loading branch information
hoh committed Jan 27, 2023
1 parent 8f70df2 commit eb6027e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion aleph_message/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import json
from copy import copy
from enum import Enum
Expand Down Expand Up @@ -192,7 +193,7 @@ class BaseMessage(BaseModel):
size: Optional[int] = Field(
default=None, description="Size of the content"
) # Almost always present
time: float = Field(description="Unix timestamp when the message was published")
time: datetime.datetime = Field(description="Unix timestamp or datetime when the message was published")
item_type: ItemType = Field(description="Storage method used for the content")
item_content: Optional[str] = Field(
default=None,
Expand Down
7 changes: 6 additions & 1 deletion aleph_message/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,14 @@ def test_create_new_message():
"signature": "0x123456789", # Signature validation requires using aleph-client
}

new_message_1 = create_new_message(message_dict, factory=PostMessage)
new_message_1: PostMessage = create_new_message(message_dict, factory=PostMessage)
assert new_message_1
assert new_message_1.type == MessageType.post
# Check that the time was converted to a datetime
assert new_message_1.time.isoformat() == '2021-07-07T10:04:47.017000+00:00'

# The time field can be either a float or a datetime as string
message_dict["time"] = '2021-07-07T10:04:47.017000+00:00'
new_message_2 = create_message_from_json(
json.dumps(message_dict), factory=PostMessage
)
Expand Down

0 comments on commit eb6027e

Please sign in to comment.