Skip to content

Commit

Permalink
Fixed peewee
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravr committed Jan 10, 2025
1 parent de8f2ec commit a873671
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions apphelpers/rest/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,32 @@ async def get_user_ip(request: Request):

if peewee_enabled:

def dbtransaction(engine):
async def dependency():
with dbtransaction_ctx(engine):
yield
def dbtransaction(db):
"""
wrapper that make db transactions automic
note db connections are used only when it is needed (hence there is no
usual connection open/close)
"""

return Depends(dependency)
def wrapper(f):
if inspect.iscoroutinefunction(f):

@wraps(f)
async def async_wrapper(*ar, **kw):
with dbtransaction_ctx(db):
return await f(*ar, **kw)

return async_wrapper
else:

@wraps(f)
async def sync_wrapper(*ar, **kw):
with dbtransaction_ctx(db):
return f(*ar, **kw)

return sync_wrapper

return wrapper

else:

Expand Down Expand Up @@ -343,6 +363,7 @@ def __init__(self, sessiondb_conn=None, urls_prefix="", site_identifier=None):
self.multi_site_enabled = False
self.site_identifier = site_identifier
self.urls_prefix = urls_prefix
self.db_tr_wrapper = phony
self.honeybadger_wrapper = phony
if site_identifier:
self.enable_multi_site(site_identifier)
Expand All @@ -356,7 +377,7 @@ def enable_multi_site(self, site_identifier: str):
self.site_identifier = site_identifier

def setup_db_transaction(self, db):
self.router.dependencies.append(dbtransaction(db))
self.db_tr_wrapper = dbtransaction(db)

def setup_honeybadger_monitoring(self):
api_key = settings.HONEYBADGER_API_KEY
Expand Down Expand Up @@ -558,7 +579,9 @@ def build(self, method, method_args, method_kw, f):
f"[{method.__name__.upper()}] => {f.__module__}:{f.__name__}",
)
m = method(*method_args, **method_kw)
f = self.access_wrapper(self.honeybadger_wrapper(raise_not_found_on_none(f)))
f = self.access_wrapper(
self.honeybadger_wrapper(raise_not_found_on_none(self.db_tr_wrapper(f)))
)
# NOTE: ^ wrapper ordering is important. access_wrapper needs request which
# others don't. If access_wrapper comes late in the order it won't be passed
# request parameter.
Expand Down

0 comments on commit a873671

Please sign in to comment.