fix: add setup_flash_manager function and fix secret_key handling
- Add setup_flash_manager async function to flash.py - Fix secret_key handling to work with both str and SecretStr - All tests passing (97 passed)
This commit is contained in:
@@ -11,7 +11,12 @@ from itsdangerous import URLSafeSerializer
|
||||
from app.infrastructure.config.settings import settings
|
||||
|
||||
FLASH_COOKIE_NAME = "flash_messages"
|
||||
SERIALIZER = URLSafeSerializer(settings.security.secret_key.get_secret_value()) # type: ignore[union-attr]
|
||||
_SECRET_KEY = (
|
||||
settings.security.secret_key.get_secret_value()
|
||||
if hasattr(settings.security.secret_key, "get_secret_value")
|
||||
else settings.security.secret_key
|
||||
)
|
||||
SERIALIZER = URLSafeSerializer(_SECRET_KEY)
|
||||
|
||||
|
||||
class FlashMessage:
|
||||
@@ -158,3 +163,13 @@ def get_flash_messages(request: Request) -> list[dict[str, str]]:
|
||||
if hasattr(request.state, "flash_manager"):
|
||||
return request.state.flash_manager.get_messages()
|
||||
return []
|
||||
|
||||
|
||||
async def setup_flash_manager(request: Request) -> None:
|
||||
"""Setup flash manager on request state.
|
||||
|
||||
Args:
|
||||
request: FastAPI request object.
|
||||
"""
|
||||
if not hasattr(request.state, "flash_manager"):
|
||||
request.state.flash_manager = FlashManager(request)
|
||||
|
||||
Reference in New Issue
Block a user