Skip to content

Commit c2e78ad

Browse files
committed
Refactored HTMLResponseModel to work with ITemplateRenderingService
1 parent 5b97863 commit c2e78ad

File tree

1 file changed

+14
-18
lines changed
  • ellar/common/responses/models

1 file changed

+14
-18
lines changed
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import typing as t
22

3-
from ellar.common.interfaces import IExecutionContext
3+
from ellar.common.interfaces import IExecutionContext, ITemplateRenderingService
44
from ellar.common.logging import request_logger
5-
from ellar.common.templating import (
6-
Environment,
7-
TemplateResponse,
8-
get_template_name,
9-
process_view_model,
10-
)
5+
from ellar.common.templating import TemplateResponse
116

127
from ..response_types import Response
138
from .base import ResponseModel
@@ -18,7 +13,7 @@ class HTMLResponseModelRuntimeError(RuntimeError):
1813

1914

2015
class HTMLResponseModel(ResponseModel):
21-
response_type: t.Type[Response] = TemplateResponse
16+
response_type: t.Type[TemplateResponse] = TemplateResponse
2217

2318
def __init__(
2419
self,
@@ -35,18 +30,19 @@ def create_response(
3530
request_logger.debug(
3631
f"Creating Response from returned Handler value - '{self.__class__.__name__}'"
3732
)
38-
jinja_environment = context.get_service_provider().get(Environment)
3933
template_name = self._get_template_name(ctx=context)
40-
template_context = {
41-
"request": context.switch_to_http_connection().get_request()
42-
}
43-
template_context.update(**process_view_model(response_obj))
44-
template = jinja_environment.get_template(template_name)
34+
rendering_service: ITemplateRenderingService = (
35+
context.get_service_provider().get(ITemplateRenderingService)
36+
)
4537

4638
response_args, headers = self.get_context_response(context=context)
47-
response_args.update(template=template, context=template_context)
48-
response = self._response_type(**response_args, headers=headers)
49-
return response
39+
return rendering_service.render_template(
40+
template_name=template_name,
41+
template_context=response_obj,
42+
headers=headers,
43+
**response_args,
44+
response_type=self.response_type,
45+
)
5046

5147
def _get_template_name(self, ctx: IExecutionContext) -> str:
5248
template_name = self.template_name
@@ -57,4 +53,4 @@ def _get_template_name(self, ctx: IExecutionContext) -> str:
5753
"cannot find Controller in request context"
5854
)
5955
template_name = controller_class.full_view_name(self.template_name)
60-
return get_template_name(template_name)
56+
return template_name

0 commit comments

Comments
 (0)