Skip to content

Commit

Permalink
Fixed peewee dbtransaction for fastapi
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravr committed Oct 7, 2024
1 parent 9eb952a commit 1339ae5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion apphelpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__author__ = """Scroll Tech"""
__email__ = "[email protected]"
__version__ = "0.96.2"
__version__ = "0.96.3"
3 changes: 3 additions & 0 deletions apphelpers/db/__init__.py
Original file line number Diff line number Diff line change
@@ -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")
23 changes: 17 additions & 6 deletions apphelpers/rest/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.96.2
current_version = 0.96.3
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 1339ae5

Please sign in to comment.