66
77from django .http import HttpRequest , HttpResponse
88
9- from ...dependency_resolver import get_injector , service_resolver
9+ from ninja_extra .dependency_resolver import get_injector , service_resolver
10+
1011from .context import RouteContext , get_route_execution_context
1112
1213if TYPE_CHECKING : # pragma: no cover
14+ from ninja_extra .controllers .base import APIController , ControllerBase
15+ from ninja_extra .controllers .route import Route
1316 from ninja_extra .operation import Operation
1417
15- from ...controllers .base import APIController , ControllerBase
16- from ...controllers .route import Route
17-
1818
1919class RouteFunctionContext :
2020 def __init__ (
@@ -74,6 +74,13 @@ def _resolve_api_func_signature_(self, context_func: Callable) -> Callable:
7474 context_func .__signature__ = sig_replaced # type: ignore
7575 return context_func
7676
77+ def run_permission_check (self , route_context : RouteContext ) -> None :
78+ _route_context = route_context or cast (
79+ RouteContext , service_resolver (RouteContext )
80+ )
81+ with self ._prep_controller_route_execution (_route_context ) as ctx :
82+ ctx .controller_instance .check_permissions ()
83+
7784 def get_view_function (self ) -> Callable :
7885 def as_view (
7986 request : HttpRequest ,
@@ -85,23 +92,30 @@ def as_view(
8592 RouteContext , service_resolver (RouteContext )
8693 )
8794 with self ._prep_controller_route_execution (_route_context , ** kwargs ) as ctx :
88- ctx .controller_instance .check_permissions ()
95+ # ctx.controller_instance.check_permissions()
8996 result = self .route .view_func (
9097 ctx .controller_instance , * args , ** ctx .view_func_kwargs
9198 )
92- return self . _process_view_function_result ( result )
99+ return result
93100
94101 as_view .get_route_function = lambda : self # type:ignore
95102 return as_view
96103
97104 def _process_view_function_result (self , result : Any ) -> Any :
98105 """
99- This process any an returned value from view_func
100- and creates an api response if result is ControllerResponseSchema
101- """
106+ This process any a returned value from view_func
107+ and creates an api response if a result is ControllerResponseSchema
102108
103- # if result and isinstance(result, ControllerResponse):
104- # return result.status_code, result.convert_to_schema()
109+ deprecated:: 0.21.5
110+ This method is deprecated and will be removed in a future version.
111+ The result processing should be handled by the response handlers.
112+ """
113+ warnings .warn (
114+ "_process_view_function_result() is deprecated and will be removed in a future version. "
115+ "The result processing should be handled by the response handlers." ,
116+ DeprecationWarning ,
117+ stacklevel = 2 ,
118+ )
105119 return result
106120
107121 def _get_controller_instance (self ) -> "ControllerBase" :
@@ -163,24 +177,27 @@ def __repr__(self) -> str: # pragma: no cover
163177
164178
165179class AsyncRouteFunction (RouteFunction ):
180+ async def async_run_check_permissions (self , route_context : RouteContext ) -> None :
181+ from asgiref .sync import sync_to_async
182+
183+ await sync_to_async (self .run_permission_check )(route_context )
184+
166185 def get_view_function (self ) -> Callable :
167186 async def as_view (
168187 request : HttpRequest ,
169188 route_context : Optional [RouteContext ] = None ,
170189 * args : Any ,
171190 ** kwargs : Any ,
172191 ) -> Any :
173- from asgiref .sync import sync_to_async
174-
175192 _route_context = route_context or cast (
176193 RouteContext , service_resolver (RouteContext )
177194 )
178195 with self ._prep_controller_route_execution (_route_context , ** kwargs ) as ctx :
179- await sync_to_async (ctx .controller_instance .check_permissions )()
196+ # await sync_to_async(ctx.controller_instance.check_permissions)()
180197 result = await self .route .view_func (
181198 ctx .controller_instance , * args , ** ctx .view_func_kwargs
182199 )
183- return self . _process_view_function_result ( result )
200+ return result
184201
185202 as_view .get_route_function = lambda : self # type:ignore
186203 return as_view
0 commit comments