Skip to content

Commit

Permalink
Merge pull request #120 from eadwinCode/route_execution_fix
Browse files Browse the repository at this point in the history
Context Parameter Renamed
  • Loading branch information
eadwinCode authored Apr 1, 2024
2 parents 693dc9d + 2c43904 commit 55ed642
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Install Flit
run: pip install flit
- name: Install Dependencies
run: flit install --symlink
run: make install
- name: Install build dependencies
run: pip install build
- name: Build distribution
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install Flit
run: pip install flit
- name: Install Dependencies
run: flit install --symlink
run: make install
- name: Test
run: pytest --cov=ninja_extra --cov-report=xml tests
- name: Coverage
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Install Flit
run: pip install flit
- name: Install Dependencies
run: flit install --symlink
run: make install
- name: Ruff Linting Check
run: ruff check ninja_extra tests
- name: mypy
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ clean: ## Removing cached python compiled files
find . -name .ruff_cache | xargs rm -rfv

install:clean ## Install dependencies
flit install --deps develop --symlink
pip install -r requirements.txt
flit install --symlink

install-full: ## Install dependencies
make install
pre-commit install -f

lint:fmt ## Run code linters
Expand Down
2 changes: 1 addition & 1 deletion docs/contribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Please take a moment to review the following guidelines before getting started.
4. **Install Dependencies:** Install development libraries and pre-commit hooks.

```bash
make install
make install-full
```

### **Code Style and Formatting**
Expand Down
2 changes: 1 addition & 1 deletion ninja_extra/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Django Ninja Extra - Class Based Utility and more for Django Ninja(Fast Django REST framework)"""

__version__ = "0.20.4"
__version__ = "0.20.5"

import django

Expand Down
28 changes: 16 additions & 12 deletions ninja_extra/controllers/route/route_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __call__(
*args,
**kwargs,
)
return self.as_view(request, *args, context=context, **kwargs)
return self.as_view(request, *args, route_context=context, **kwargs)

def _get_required_api_func_signature(self) -> Tuple:
skip_parameters = ["self", "request"]
Expand All @@ -77,12 +77,14 @@ def _resolve_api_func_signature_(self, context_func: Callable) -> Callable:
def get_view_function(self) -> Callable:
def as_view(
request: HttpRequest,
context: Optional[RouteContext] = None,
route_context: Optional[RouteContext] = None,
*args: Any,
**kwargs: Any,
) -> Any:
context = context or cast(RouteContext, service_resolver(RouteContext))
with self._prep_controller_route_execution(context, **kwargs) as ctx:
_route_context = route_context or cast(
RouteContext, service_resolver(RouteContext)
)
with self._prep_controller_route_execution(_route_context, **kwargs) as ctx:
ctx.controller_instance.check_permissions()
result = self.route.view_func(
ctx.controller_instance, *args, **ctx.view_func_kwargs
Expand Down Expand Up @@ -134,14 +136,14 @@ def get_route_execution_context(

@contextmanager
def _prep_controller_route_execution(
self, context: RouteContext, **kwargs: Any
self, route_context: RouteContext, **kwargs: Any
) -> Iterator[RouteFunctionContext]:
controller_instance = self._get_controller_instance()
controller_instance.context = context
controller_instance.context = route_context

api_func_kwargs = dict(**kwargs)
api_func_kwargs = dict(kwargs)
if self.has_request_param:
api_func_kwargs.update(request=context.request)
api_func_kwargs.update(request=route_context.request)
try:
yield RouteFunctionContext(
controller_instance=controller_instance, **api_func_kwargs
Expand All @@ -164,14 +166,16 @@ class AsyncRouteFunction(RouteFunction):
def get_view_function(self) -> Callable:
async def as_view(
request: HttpRequest,
context: Optional[RouteContext] = None,
route_context: Optional[RouteContext] = None,
*args: Any,
**kwargs: Any,
) -> Any:
from asgiref.sync import sync_to_async

context = context or cast(RouteContext, service_resolver(RouteContext))
with self._prep_controller_route_execution(context, **kwargs) as ctx:
_route_context = route_context or cast(
RouteContext, service_resolver(RouteContext)
)
with self._prep_controller_route_execution(_route_context, **kwargs) as ctx:
await sync_to_async(ctx.controller_instance.check_permissions)()
result = await self.route.view_func(
ctx.controller_instance, *args, **ctx.view_func_kwargs
Expand Down Expand Up @@ -201,4 +205,4 @@ async def __call__(
*args,
**kwargs,
)
return await self.as_view(request, *args, context=context, **kwargs)
return await self.as_view(request, *args, route_context=context, **kwargs)
24 changes: 0 additions & 24 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,6 @@ requires-python = ">=3.7"
[tool.flit.metadata.urls]
Documentation = "https://eadwincode.github.io/django-ninja-extra/"

[tool.flit.metadata.requires-extra]
test = [
"pytest",
"pytest-cov",
"pytest-django",
"pytest-asyncio==0.20.3",
"mypy == 1.7.1",
"ruff ==0.1.7",
"ninja-schema>=0.13.4",
"django-stubs",
]
doc = [
"mkdocs >=1.1.2,<2.0.0",
"mkdocs-material >=7.1.9,<8.0.0",
"mdx-include >=1.4.1,<2.0.0",
"mkdocs-markdownextradata-plugin >=0.1.7,<0.3.0",
"markdown-include",
"mkdocstrings"
]

dev = [
"pre-commit"
]

[tool.ruff]
select = [
"E", # pycodestyle errors
Expand Down
6 changes: 6 additions & 0 deletions requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
markdown-include
mdx-include >=1.4.1,<2.0.0
mkdocs >=1.1.2,<2.0.0
mkdocs-markdownextradata-plugin >=0.1.7,<0.3.0
mkdocs-material >=7.1.9,<8.0.0
mkdocstrings
8 changes: 8 additions & 0 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
django-stubs
mypy == 1.7.1
ninja-schema>=0.13.4
pytest
pytest-asyncio==0.20.3
pytest-cov
pytest-django
ruff ==0.1.7
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-e .
-r requirements-docs.txt
-r requirements-tests.txt

pre-commit
8 changes: 6 additions & 2 deletions tests/test_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ def test_route_is_protected_by_its_permissions_paramater(self):
def test_route_prep_controller_route_execution_context_works(self):
route_function: RouteFunction = get_route_function(SomeTestController().example)
context = get_route_execution_context(request=anonymous_request)
with route_function._prep_controller_route_execution(context=context) as ctx:
with route_function._prep_controller_route_execution(
route_context=context
) as ctx:
assert isinstance(ctx.controller_instance, SomeTestController)
assert ctx.controller_instance.context
assert ctx.controller_instance.context is None
Expand All @@ -404,7 +406,9 @@ def test_route_prep_controller_route_execution_context_cleans_controller_after_r
):
route_function: RouteFunction = get_route_function(SomeTestController().example)
context = get_route_execution_context(request=anonymous_request)
with route_function._prep_controller_route_execution(context=context) as ctx:
with route_function._prep_controller_route_execution(
route_context=context
) as ctx:
assert isinstance(ctx.controller_instance, SomeTestController)
assert ctx.controller_instance.context

Expand Down

0 comments on commit 55ed642

Please sign in to comment.