Skip to content

Commit f90dda6

Browse files
committed
renamed context to route_context #119
1 parent 693dc9d commit f90dda6

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

ninja_extra/controllers/route/route_functions.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __call__(
5050
*args,
5151
**kwargs,
5252
)
53-
return self.as_view(request, *args, context=context, **kwargs)
53+
return self.as_view(request, *args, route_context=context, **kwargs)
5454

5555
def _get_required_api_func_signature(self) -> Tuple:
5656
skip_parameters = ["self", "request"]
@@ -77,12 +77,14 @@ def _resolve_api_func_signature_(self, context_func: Callable) -> Callable:
7777
def get_view_function(self) -> Callable:
7878
def as_view(
7979
request: HttpRequest,
80-
context: Optional[RouteContext] = None,
80+
route_context: Optional[RouteContext] = None,
8181
*args: Any,
8282
**kwargs: Any,
8383
) -> Any:
84-
context = context or cast(RouteContext, service_resolver(RouteContext))
85-
with self._prep_controller_route_execution(context, **kwargs) as ctx:
84+
_route_context = route_context or cast(
85+
RouteContext, service_resolver(RouteContext)
86+
)
87+
with self._prep_controller_route_execution(_route_context, **kwargs) as ctx:
8688
ctx.controller_instance.check_permissions()
8789
result = self.route.view_func(
8890
ctx.controller_instance, *args, **ctx.view_func_kwargs
@@ -134,14 +136,14 @@ def get_route_execution_context(
134136

135137
@contextmanager
136138
def _prep_controller_route_execution(
137-
self, context: RouteContext, **kwargs: Any
139+
self, route_context: RouteContext, **kwargs: Any
138140
) -> Iterator[RouteFunctionContext]:
139141
controller_instance = self._get_controller_instance()
140-
controller_instance.context = context
142+
controller_instance.context = route_context
141143

142-
api_func_kwargs = dict(**kwargs)
144+
api_func_kwargs = dict(kwargs)
143145
if self.has_request_param:
144-
api_func_kwargs.update(request=context.request)
146+
api_func_kwargs.update(request=route_context.request)
145147
try:
146148
yield RouteFunctionContext(
147149
controller_instance=controller_instance, **api_func_kwargs
@@ -164,14 +166,16 @@ class AsyncRouteFunction(RouteFunction):
164166
def get_view_function(self) -> Callable:
165167
async def as_view(
166168
request: HttpRequest,
167-
context: Optional[RouteContext] = None,
169+
route_context: Optional[RouteContext] = None,
168170
*args: Any,
169171
**kwargs: Any,
170172
) -> Any:
171173
from asgiref.sync import sync_to_async
172174

173-
context = context or cast(RouteContext, service_resolver(RouteContext))
174-
with self._prep_controller_route_execution(context, **kwargs) as ctx:
175+
_route_context = route_context or cast(
176+
RouteContext, service_resolver(RouteContext)
177+
)
178+
with self._prep_controller_route_execution(_route_context, **kwargs) as ctx:
175179
await sync_to_async(ctx.controller_instance.check_permissions)()
176180
result = await self.route.view_func(
177181
ctx.controller_instance, *args, **ctx.view_func_kwargs
@@ -201,4 +205,4 @@ async def __call__(
201205
*args,
202206
**kwargs,
203207
)
204-
return await self.as_view(request, *args, context=context, **kwargs)
208+
return await self.as_view(request, *args, route_context=context, **kwargs)

tests/test_route.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ def test_route_is_protected_by_its_permissions_paramater(self):
394394
def test_route_prep_controller_route_execution_context_works(self):
395395
route_function: RouteFunction = get_route_function(SomeTestController().example)
396396
context = get_route_execution_context(request=anonymous_request)
397-
with route_function._prep_controller_route_execution(context=context) as ctx:
397+
with route_function._prep_controller_route_execution(
398+
route_context=context
399+
) as ctx:
398400
assert isinstance(ctx.controller_instance, SomeTestController)
399401
assert ctx.controller_instance.context
400402
assert ctx.controller_instance.context is None
@@ -404,7 +406,9 @@ def test_route_prep_controller_route_execution_context_cleans_controller_after_r
404406
):
405407
route_function: RouteFunction = get_route_function(SomeTestController().example)
406408
context = get_route_execution_context(request=anonymous_request)
407-
with route_function._prep_controller_route_execution(context=context) as ctx:
409+
with route_function._prep_controller_route_execution(
410+
route_context=context
411+
) as ctx:
408412
assert isinstance(ctx.controller_instance, SomeTestController)
409413
assert ctx.controller_instance.context
410414

0 commit comments

Comments
 (0)