Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix when used without configure #331

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__ = "jesuslarag@gmail.com"
__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
Loading