Skip to content
Snippets Groups Projects
Commit 54957d2f authored by vlorentz's avatar vlorentz
Browse files

Make OriginVisit use datetime for its date.

But keep support for deserializing from str, like
swh-storage does.
parent 01a5d4ce
No related branches found
No related tags found
No related merge requests found
......@@ -112,7 +112,6 @@ class OriginVisit(BaseModel):
"""Serializes the date as a string and omits the visit id if it is
`None`."""
ov = super().to_dict()
ov['date'] = str(self.date)
if ov['visit'] is None:
del ov['visit']
return ov
......@@ -120,9 +119,13 @@ class OriginVisit(BaseModel):
@classmethod
def from_dict(cls, d):
"""Parses the date from a string, and accepts missing visit ids."""
d = d.copy()
date = d.pop('date')
return cls(
origin=Origin.from_dict(d['origin']),
date=dateutil.parser.parse(d['date']),
origin=Origin.from_dict(d.pop('origin')),
date=(date
if isinstance(date, datetime.datetime)
else dateutil.parser.parse(date)),
visit=d.get('visit'))
......
......@@ -3,6 +3,8 @@
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
import datetime
import attr
from hypothesis import given
......@@ -30,7 +32,8 @@ def assert_nested_dict(obj):
elif isinstance(obj, list):
for value in obj:
assert_nested_dict(value)
elif isinstance(obj, (int, float, str, bytes, bool, type(None))):
elif isinstance(obj, (int, float, str, bytes, bool, type(None),
datetime.datetime)):
pass
else:
assert False, obj
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment