Skip to content

Commit

Permalink
Async ModelController: Added support for async route function creatio…
Browse files Browse the repository at this point in the history
…n in ModelControllers
  • Loading branch information
eadwinCode committed Dec 31, 2023
1 parent 0862f7a commit 9b3182a
Show file tree
Hide file tree
Showing 12 changed files with 1,054 additions and 273 deletions.
2 changes: 2 additions & 0 deletions ninja_extra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ninja_extra.controllers import (
ControllerBase,
ModelAsyncEndpointFactory,
ModelConfig,
ModelControllerBase,
ModelControllerBuilder,
Expand Down Expand Up @@ -63,4 +64,5 @@
"ModelServiceBase",
"ModelControllerBase",
"ModelEndpointFactory",
"ModelAsyncEndpointFactory",
]
2 changes: 2 additions & 0 deletions ninja_extra/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .base import ControllerBase, ModelControllerBase, api_controller
from .model import (
ModelAsyncEndpointFactory,
ModelConfig,
ModelControllerBuilder,
ModelEndpointFactory,
Expand Down Expand Up @@ -49,4 +50,5 @@
"ModelPagination",
"ModelServiceBase",
"ModelEndpointFactory",
"ModelAsyncEndpointFactory",
]
3 changes: 2 additions & 1 deletion ninja_extra/controllers/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .builder import ModelControllerBuilder
from .endpoints import ModelEndpointFactory
from .endpoints import ModelAsyncEndpointFactory, ModelEndpointFactory
from .interfaces import ModelServiceBase
from .schemas import ModelConfig, ModelPagination, ModelSchemaConfig
from .service import ModelService
Expand All @@ -12,4 +12,5 @@
"ModelPagination",
"ModelControllerBuilder",
"ModelEndpointFactory",
"ModelAsyncEndpointFactory",
]
20 changes: 13 additions & 7 deletions ninja_extra/controllers/model/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ninja_extra.constants import ROUTE_FUNCTION

from .endpoints import ModelEndpointFactory
from .endpoints import ModelAsyncEndpointFactory, ModelEndpointFactory
from .schemas import ModelConfig

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -42,6 +42,12 @@ def __init__(
self._update_schema = self._config.update_schema
self._patch_schema = self._config.patch_schema

self._route_factory: ModelEndpointFactory = (
ModelAsyncEndpointFactory()
if base_cls.model_config.async_routes
else ModelEndpointFactory()
)

def _add_to_controller(self, func: t.Callable) -> None:
route_function = getattr(func, ROUTE_FUNCTION)
route_function.api_controller = self._api_controller_instance
Expand All @@ -54,7 +60,7 @@ def _register_create_endpoint(self) -> None:
"summary": "Create an item",
}
kw.update(self._config.create_route_info)
create_item = ModelEndpointFactory.create(
create_item = self._route_factory.create(
schema_in=self._create_schema, # type:ignore[arg-type]
schema_out=self._retrieve_schema, # type:ignore[arg-type]
**kw, # type:ignore[arg-type]
Expand All @@ -74,7 +80,7 @@ def _register_update_endpoint(self) -> None:
}
kw.update(self._config.update_route_info)

update_item = ModelEndpointFactory.update(
update_item = self._route_factory.update(
path=_path,
lookup_param=self._model_pk_name,
schema_in=self._update_schema, # type:ignore[arg-type]
Expand All @@ -98,7 +104,7 @@ def _register_patch_endpoint(self) -> None:
}
kw.update(self._config.patch_route_info)

patch_item = ModelEndpointFactory.patch(
patch_item = self._route_factory.patch(
path=_path,
lookup_param=self._model_pk_name,
schema_out=self._retrieve_schema, # type:ignore[arg-type]
Expand All @@ -120,7 +126,7 @@ def _register_find_one_endpoint(self) -> None:
}
kw.update(self._config.find_one_route_info)

get_item = ModelEndpointFactory.find_one(
get_item = self._route_factory.find_one(
path=_path,
lookup_param=self._model_pk_name,
schema_out=self._retrieve_schema, # type:ignore[arg-type]
Expand Down Expand Up @@ -148,7 +154,7 @@ def _register_list_endpoint(self) -> None:
if self._config.pagination.paginator_kwargs: # pragma: no cover
paginate_kwargs.update(self._config.pagination.paginator_kwargs)

list_items = ModelEndpointFactory.list(
list_items = self._route_factory.list(
path="/",
schema_out=self._retrieve_schema, # type:ignore[arg-type]
**kw, # type:ignore[arg-type]
Expand All @@ -169,7 +175,7 @@ def _register_delete_endpoint(self) -> None:
}
kw.update(self._config.delete_route_info)

delete_item = ModelEndpointFactory.delete(
delete_item = self._route_factory.delete(
path=_path,
lookup_param=self._model_pk_name,
**kw, # type:ignore[arg-type]
Expand Down
Loading

0 comments on commit 9b3182a

Please sign in to comment.