Skip to content

Commit

Permalink
Merge pull request #331 from phenobarbital/dev
Browse files Browse the repository at this point in the history
fix when used without configure
  • Loading branch information
phenobarbital authored Jan 7, 2025
2 parents c5ac732 + 5315ca4 commit 2d27ab6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion navigator/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__description__ = (
"Navigator Web Framework based on aiohttp, " "with batteries included."
)
__version__ = "2.12.8"
__version__ = "2.12.9"
__author__ = "Jesus Lara"
__author_email__ = "[email protected]"
__license__ = "BSD"
67 changes: 36 additions & 31 deletions navigator/views/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def model_url(
) -> list:
_path = handler.path
name = getattr(handler, 'name', name)
model = _path if _path else name
model = _path or name
url = f"/api/{version}/{model}"
return [
path("", r"{}/{{id:.*}}".format(url), handler, name=f"{name}_{model}_id"),
Expand Down Expand Up @@ -72,11 +72,10 @@ async def __call__(self, request: web.Request):
return self

async def __aenter__(self):
if self._default is True:
self._connection = await self._db.acquire()
return self._connection
else:
if self._default is not True:
return await self._db.connection()
self._connection = await self._db.acquire()
return self._connection

async def default_connection(self, request: web.Request):
if self._dbname in request.app:
Expand Down Expand Up @@ -177,9 +176,18 @@ def __init__(self, request, *args, **kwargs):
if not self.get_model:
self.get_model = self.model
super().__init__(request, *args, **kwargs)
# Database Connection Handler
# backwards compatibility with old configuration method.
self.handler = ConnectionHandler(
driver=self.driver,
dsn=self.dsn,
dbname=self.dbname,
credentials=self.credentials,
model_kwargs=self.model_kwargs
)

@classmethod
def configure(cls, app: WebApp, path: str = None, **kwargs) -> WebApp:
def configure(cls, app: WebApp = None, path: str = None, **kwargs) -> WebApp:
"""configure.
Expand All @@ -195,33 +203,30 @@ def configure(cls, app: WebApp, path: str = None, **kwargs) -> WebApp:
app = app.get_app()
elif isinstance(app, WebApp):
app = app # register the app into the Extension
else:
raise TypeError(
f"Invalid type for Application Setup: {app}:{type(app)}"
)
# startup operations over extension backend
if callable(cls.on_startup):
app.on_startup.append(cls.on_startup)
if callable(cls.on_shutdown):
app.on_shutdown.append(cls.on_shutdown)
### added routers:
try:
model_path = cls.path
except AttributeError:
model_path = None
if not model_path:
model_path = path
if not model_path:
raise ConfigError(
"Wrong Model Configuration: URI path must be provided."
if app:
if callable(cls.on_startup):
app.on_startup.append(cls.on_startup)
if callable(cls.on_shutdown):
app.on_shutdown.append(cls.on_shutdown)
### added routers:
try:
model_path = cls.path
except AttributeError:
model_path = None
if not model_path:
model_path = path
if not model_path:
raise ConfigError(
"Wrong Model Configuration: URI path must be provided."
)
url = f"{model_path}"
app.router.add_view(
r"{url}/{{id:.*}}".format(url=url), cls
)
app.router.add_view(
r"{url}{{meta:(:.*)?}}".format(url=url), cls
)
url = f"{model_path}"
app.router.add_view(
r"{url}/{{id:.*}}".format(url=url), cls
)
app.router.add_view(
r"{url}{{meta:(:.*)?}}".format(url=url), cls
)
# Use kwargs to reconfigure the connection handler if needed
if 'driver' in kwargs:
cls.driver = kwargs['driver']
Expand Down

0 comments on commit 2d27ab6

Please sign in to comment.