Skip to content

Commit

Permalink
Merge pull request #111 from stckme/peewee_txn_fix
Browse files Browse the repository at this point in the history
Fixed peewee dbtransaction for fastapi
  • Loading branch information
gauravr authored Oct 7, 2024
2 parents 9eb952a + c1cdf32 commit de8f2ec
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 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")
26 changes: 19 additions & 7 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 Expand Up @@ -335,7 +346,8 @@ def __init__(self, sessiondb_conn=None, urls_prefix="", site_identifier=None):
self.honeybadger_wrapper = phony
if site_identifier:
self.enable_multi_site(site_identifier)
self.setup_session_db(sessiondb_conn)
if sessiondb_conn:
self.setup_session_db(sessiondb_conn)
self.router = APIRouter(route_class=Router)
self.secure_router = APIRouter(route_class=SecureRouter)

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 de8f2ec

Please sign in to comment.