Skip to content

Commit

Permalink
Fix IsoformatDateTime wrapper
Browse files Browse the repository at this point in the history
- Update custom datetime to add any missing timezone

- Fix: datetime wrapper should not make up a new JSONSchema type

- Use extended datetime for `time_added` in `File` model
  • Loading branch information
ml-evs committed Nov 4, 2024
1 parent b6aea3d commit 119918f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
12 changes: 6 additions & 6 deletions pydatalab/schemas/cell.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -91,7 +91,7 @@
},
"date": {
"title": "Date",
"type": "date",
"type": "string",
"format": "date-time"
},
"name": {
Expand Down Expand Up @@ -324,7 +324,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -415,7 +415,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -488,7 +488,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand All @@ -506,7 +506,7 @@
"last_modified_remote": {
"title": "Last Modified Remote",
"description": "The last date/time at which the remote file was modified.",
"type": "date",
"type": "string",
"format": "date-time"
},
"item_ids": {
Expand Down
12 changes: 6 additions & 6 deletions pydatalab/schemas/equipment.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -91,7 +91,7 @@
},
"date": {
"title": "Date",
"type": "date",
"type": "string",
"format": "date-time"
},
"name": {
Expand Down Expand Up @@ -279,7 +279,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -370,7 +370,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -443,7 +443,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand All @@ -461,7 +461,7 @@
"last_modified_remote": {
"title": "Last Modified Remote",
"description": "The last date/time at which the remote file was modified.",
"type": "date",
"type": "string",
"format": "date-time"
},
"item_ids": {
Expand Down
12 changes: 6 additions & 6 deletions pydatalab/schemas/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -91,7 +91,7 @@
},
"date": {
"title": "Date",
"type": "date",
"type": "string",
"format": "date-time"
},
"name": {
Expand Down Expand Up @@ -283,7 +283,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -374,7 +374,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -447,7 +447,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand All @@ -465,7 +465,7 @@
"last_modified_remote": {
"title": "Last Modified Remote",
"description": "The last date/time at which the remote file was modified.",
"type": "date",
"type": "string",
"format": "date-time"
},
"item_ids": {
Expand Down
14 changes: 7 additions & 7 deletions pydatalab/schemas/startingmaterial.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -92,7 +92,7 @@
"date": {
"title": "Date Acquired",
"description": "The date the item was acquired",
"type": "date",
"type": "string",
"format": "date-time"
},
"name": {
Expand Down Expand Up @@ -123,7 +123,7 @@
"date_opened": {
"title": "Date Opened",
"description": "The date the container was opened",
"type": "date",
"type": "string",
"format": "date-time"
},
"CAS": {
Expand Down Expand Up @@ -337,7 +337,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -428,7 +428,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand Down Expand Up @@ -501,7 +501,7 @@
},
"last_modified": {
"title": "Last Modified",
"type": "date",
"type": "string",
"format": "date-time"
},
"relationships": {
Expand All @@ -519,7 +519,7 @@
"last_modified_remote": {
"title": "Last Modified Remote",
"description": "The last date/time at which the remote file was modified.",
"type": "date",
"type": "string",
"format": "date-time"
},
"item_ids": {
Expand Down
3 changes: 1 addition & 2 deletions pydatalab/src/pydatalab/models/files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
from typing import Any, Dict, List, Optional

from pydantic import Field
Expand Down Expand Up @@ -37,7 +36,7 @@ class File(Entry, HasOwner, HasRevisionControl):
description="The source of the file, e.g. 'remote' or 'uploaded'."
)

time_added: datetime.datetime = Field(description="The timestamp for the original file upload.")
time_added: IsoformatDateTime = Field(description="The timestamp for the original file upload.")

metadata: Optional[Dict[Any, Any]] = Field(description="Any additional metadata.")

Expand Down
12 changes: 6 additions & 6 deletions pydatalab/src/pydatalab/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ def __get_validators__(cls):
yield cls.validate

@classmethod
def validate(cls, v):
def validate(cls, v) -> datetime.datetime | None:
"""Cast isoformat strings to datetimes and enforce UTC if tzinfo is missing."""
if isinstance(v, str):
if v in ["0", " "]:
return None
return datetime.datetime.fromisoformat(v)
v = datetime.datetime.fromisoformat(v)

return v
if v.tzinfo is None:
v = v.replace(tzinfo=datetime.timezone.utc)

@classmethod
def __modify_schema__(cls, field_schema):
field_schema.update(type="date")
return v


JSON_ENCODERS = {
Expand Down

0 comments on commit 119918f

Please sign in to comment.