Skip to content

Commit

Permalink
Fix timezone bug (SEA-1360) realted to wrong DateTimeWithoutTz impl.
Browse files Browse the repository at this point in the history
Avoid error with sedate NotTimezoneAware, where a timestamp was actually
timezone aware, even though it's annotation implied the opposite.
  • Loading branch information
cyrillkuettel committed Jun 27, 2024
1 parent 08a53ce commit 40d1ebb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/privatim/models/password_change_token.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import secrets
import uuid
from datetime import datetime, timezone
from datetime import datetime
from datetime import timedelta

from sedate import utcnow
from sqlalchemy import ForeignKey
from sqlalchemy import String
from sqlalchemy.orm import Mapped
Expand Down Expand Up @@ -31,7 +30,7 @@ class PasswordChangeToken(Base):
token: Mapped[str] = mapped_column(String)
ip_address: Mapped[str] = mapped_column(String)

user: Mapped[User] = relationship(lazy='joined')
user: Mapped['User'] = relationship(lazy='joined')

def __init__(
self,
Expand All @@ -50,7 +49,7 @@ def __init__(

if time_requested is None:
time_requested = datetime.utcnow()
self.time_requested = time_requested.replace(tzinfo=timezone.utc)
self.time_requested = time_requested.replace(tzinfo=None)
self.time_consumed = None
self.token = secrets.token_urlsafe()

Expand All @@ -70,7 +69,7 @@ def consume(self, email: str) -> None:
raise PasswordException(f'Token "{self.token}" has expired')

time_consumed = datetime.now()
time_consumed = time_consumed.replace(tzinfo=timezone.utc)
time_consumed = time_consumed.replace(tzinfo=None)
self.time_consumed = time_consumed

@property
Expand All @@ -93,6 +92,6 @@ def expired(self) -> bool:
return True

expiring_time = self.time_requested + timedelta(hours=48)
if utcnow() > expiring_time:
if datetime.utcnow() > expiring_time:
return True
return False
5 changes: 4 additions & 1 deletion src/privatim/orm/meta.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import uuid
from datetime import datetime
from typing import Annotated
from typing import TYPE_CHECKING

from sqlalchemy import BigInteger
from sqlalchemy import Integer
Expand All @@ -12,10 +11,13 @@
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import registry
from sqlalchemy.schema import MetaData
from sqlalchemy import DateTime
from sqlalchemy_file import FileField, File
from .utcdatetime_type import UTCDateTime
from .uuid_type import UUIDStr as UUIDStrType


from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.sql.schema import _NamingSchemaTD

Expand Down Expand Up @@ -74,6 +76,7 @@ class Base(DeclarativeBase):
str_64: String(length=64),
str_128: String(length=128),
str_256: String(length=256),
DateTimeWithoutTz: DateTime(timezone=False),
Text: TextType,
FileContents: LargeBinary,
AttachedFile: FileField,
Expand Down
4 changes: 2 additions & 2 deletions src/privatim/orm/utcdatetime_type.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from datetime import datetime
from typing import TYPE_CHECKING

from sedate import standardize_date, to_timezone
from sqlalchemy.types import DateTime, TypeDecorator


from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.engine.interfaces import Dialect

Expand Down

0 comments on commit 40d1ebb

Please sign in to comment.