diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 66bc0f2..1413d1e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ History ======= +0.96.3 (2024-10-07) +------------------- +* Fixed peewee dbtransaction for fastapi. + 0.96.2 (2024-10-07) ------------------- * Removed deprecated peewee autorollback option. diff --git a/apphelpers/__init__.py b/apphelpers/__init__.py index c1230b5..7a46185 100644 --- a/apphelpers/__init__.py +++ b/apphelpers/__init__.py @@ -4,4 +4,4 @@ __author__ = """Scroll Tech""" __email__ = "engg@stck.me" -__version__ = "0.96.2" +__version__ = "0.96.3" diff --git a/apphelpers/db/__init__.py b/apphelpers/db/__init__.py index 0589f79..a399455 100644 --- a/apphelpers/db/__init__.py +++ b/apphelpers/db/__init__.py @@ -1,8 +1,11 @@ try: from apphelpers.db.piccolo import * # noqa: F401, F403 + peewee_enabled = False print("apphelpers: using Piccolo") + except ImportError: from apphelpers.db.peewee import * # noqa: F401, F403 + peewee_enabled = True print("apphelpers: using Peewee") diff --git a/apphelpers/rest/fastapi.py b/apphelpers/rest/fastapi.py index c592eab..bd51773 100644 --- a/apphelpers/rest/fastapi.py +++ b/apphelpers/rest/fastapi.py @@ -7,7 +7,7 @@ from fastapi.routing import APIRoute from starlette.requests import Request -from apphelpers.db import dbtransaction_ctx +from apphelpers.db import dbtransaction_ctx, peewee_enabled from apphelpers.errors.fastapi import ( BaseError, HTTP401Unauthorized, @@ -168,12 +168,23 @@ async def get_user_ip(request: Request): header = Annotated[str, Header()] -def dbtransaction(engine, allow_nested=True): - async def dependency(): - async with dbtransaction_ctx(engine, allow_nested=allow_nested): - yield +if peewee_enabled: - return Depends(dependency) + def dbtransaction(engine): + async def dependency(): + with dbtransaction_ctx(engine): + yield + + return Depends(dependency) + +else: + + def dbtransaction(engine, allow_nested=True): + async def dependency(): + async with dbtransaction_ctx(engine, allow_nested=allow_nested): + yield + + return Depends(dependency) class SecureRouter(APIRoute): diff --git a/setup.cfg b/setup.cfg index c209e22..9b7248b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.96.2 +current_version = 0.96.3 commit = True tag = True diff --git a/setup.py b/setup.py index 00e5bf9..2a987a0 100644 --- a/setup.py +++ b/setup.py @@ -42,6 +42,6 @@ test_suite="tests", tests_require=test_requirements, url="https://github.com/scrolltech/apphelpers", - version="0.96.2", + version="0.96.3", zip_safe=False, )