Skip to content

Commit

Permalink
Fix datetime.utcnow() deprecations in SQLAlchemy backend pallets-eco#253
Browse files Browse the repository at this point in the history
  • Loading branch information
GammaGames committed Jan 9, 2025
1 parent bc2fe67 commit 95f479e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/flask_session/dynamodb/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Provides a Session Interface to DynamoDB"""

import warnings
from datetime import datetime
from datetime import datetime, timezone
from datetime import timedelta as TimeDelta
from decimal import Decimal
from typing import Optional
Expand Down Expand Up @@ -144,7 +144,7 @@ def _create_table(self):
def _retrieve_session_data(self, store_id: str) -> Optional[dict]:
# Get the saved session (document) from the database
document = self.store.get_item(Key={"id": store_id}).get("Item")
session_is_not_expired = Decimal(datetime.utcnow().timestamp()) <= document.get(
session_is_not_expired = Decimal(datetime.now(timezone.utc).timestamp()) <= document.get(
"expiration"
)
if document and session_is_not_expired:
Expand All @@ -158,7 +158,7 @@ def _delete_session(self, store_id: str) -> None:
def _upsert_session(
self, session_lifetime: TimeDelta, session: ServerSideSession, store_id: str
) -> None:
storage_expiration_datetime = datetime.utcnow() + session_lifetime
storage_expiration_datetime = datetime.now(timezone.utc) + session_lifetime
# Serialize the session data
serialized_session_data = self.serializer.dumps(dict(session))

Expand Down
4 changes: 2 additions & 2 deletions src/flask_session/mongodb/mongodb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from datetime import datetime
from datetime import datetime, timezone
from datetime import timedelta as TimeDelta
from typing import Optional

Expand Down Expand Up @@ -89,7 +89,7 @@ def _delete_session(self, store_id: str) -> None:
def _upsert_session(
self, session_lifetime: TimeDelta, session: ServerSideSession, store_id: str
) -> None:
storage_expiration_datetime = datetime.utcnow() + session_lifetime
storage_expiration_datetime = datetime.now(timezone.utc) + session_lifetime

# Serialize the session data
serialized_session_data = self.serializer.dumps(dict(session))
Expand Down
8 changes: 4 additions & 4 deletions src/flask_session/sqlalchemy/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from datetime import datetime
from datetime import datetime, timezone
from datetime import timedelta as TimeDelta
from typing import Any, Optional

Expand Down Expand Up @@ -125,7 +125,7 @@ def __init__(
def _delete_expired_sessions(self) -> None:
try:
self.client.session.query(self.sql_session_model).filter(
self.sql_session_model.expiry <= datetime.utcnow()
self.sql_session_model.expiry <= datetime.now(timezone.utc)
).delete(synchronize_session=False)
self.client.session.commit()
except Exception:
Expand All @@ -138,7 +138,7 @@ def _retrieve_session_data(self, store_id: str) -> Optional[dict]:
record = self.sql_session_model.query.filter_by(session_id=store_id).first()

# "Delete the session record if it is expired as SQL has no TTL ability
if record and (record.expiry is None or record.expiry <= datetime.utcnow()):
if record and (record.expiry is None or record.expiry <= datetime.now(timezone.utc)):
try:
self.client.session.delete(record)
self.client.session.commit()
Expand All @@ -165,7 +165,7 @@ def _delete_session(self, store_id: str) -> None:
def _upsert_session(
self, session_lifetime: TimeDelta, session: ServerSideSession, store_id: str
) -> None:
storage_expiration_datetime = datetime.utcnow() + session_lifetime
storage_expiration_datetime = datetime.now(timezone.utc) + session_lifetime

# Serialize session data
serialized_session_data = self.serializer.dumps(dict(session))
Expand Down

0 comments on commit 95f479e

Please sign in to comment.