Skip to content

Commit

Permalink
fix pydantic bug for postgres tzs
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-genson committed Jul 4, 2024
1 parent 4ff1a4d commit 33986b3
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mealie/schema/_mealie/mealie_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ class MealieModel(BaseModel):
"""
model_config = ConfigDict(alias_generator=camelize, populate_by_name=True)

@model_validator(mode="before")
@classmethod
def fix_hour_only_tz(cls, data: T) -> T:
"""
Fixes datetimes with timezones that only have the hour portion.
Pydantic assumes timezones are in the format +HH:MM, but postgres returns +HH.
https://github.com/pydantic/pydantic/issues/8609
"""
for field, field_info in cls.model_fields.items():
if field_info.annotation != datetime:
continue
try:
if not isinstance(val := getattr(data, field), str):
continue
except AttributeError:
continue
# this only works for UTC, but we enforce UTC throughout the entire application
setattr(data, field, val.replace("+00", "+00:00"))

return data

@model_validator(mode="after")
def set_tz_info(self) -> Self:
"""
Expand Down

0 comments on commit 33986b3

Please sign in to comment.