From 9b71ab023360dfe9cc35295dad7a1a9c25b2dd71 Mon Sep 17 00:00:00 2001 From: Tobias Deiminger Date: Sat, 20 Jul 2024 23:21:12 +0200 Subject: [PATCH] Mark all template.render() output as Markup To be discussed... if ok, squash into previous commit. --- .../export/html/form_objects/form_object.py | 32 +- .../grammar_element_form_object.py | 14 +- .../html/form_objects/grammar_form_object.py | 15 +- .../included_document_form_object.py | 15 +- .../row_with_grammar_element_form_object.py | 18 +- .../html/generators/source_file_coverage.py | 10 +- .../diff_screen_results_view_object.py | 11 +- .../view_objects/diff_screen_view_object.py | 10 +- .../document_screen_view_object.py | 98 ++--- .../view_objects/nestor_view_object.py | 11 +- .../project_statistics_view_object.py | 10 +- .../view_objects/project_tree_view_object.py | 10 +- .../view_objects/search_screen_view_object.py | 11 +- .../view_objects/source_file_view_object.py | 10 +- .../traceability_matrix_view_object.py | 11 +- strictdoc/export/html/html_templates.py | 52 ++- strictdoc/server/helpers/turbo.py | 5 +- strictdoc/server/routers/main_router.py | 351 ++++++++---------- 18 files changed, 328 insertions(+), 366 deletions(-) diff --git a/strictdoc/export/html/form_objects/form_object.py b/strictdoc/export/html/form_objects/form_object.py index 506251e67..d8391124a 100644 --- a/strictdoc/export/html/form_objects/form_object.py +++ b/strictdoc/export/html/form_objects/form_object.py @@ -2,25 +2,25 @@ from dataclasses import dataclass from typing import Any, Dict, List -from jinja2 import Environment, Template +from strictdoc.export.html.html_templates import JinjaEnvironment @dataclass class RowWithReservedFieldFormObject: field: Any errors: Dict[str, List] - jinja_environment: Environment + jinja_environment: JinjaEnvironment def __post_init__(self): assert isinstance( - self.jinja_environment, Environment + self.jinja_environment, JinjaEnvironment ), self.jinja_environment def render(self): - template: Template = self.jinja_environment.get_template( - "components/grammar_form_element/row_with_reserved_field/index.jinja" + rendered_template = self.jinja_environment.render_template_as_markup( + "components/grammar_form_element/row_with_reserved_field/index.jinja", + form_object=self, ) - rendered_template = template.render(form_object=self) return rendered_template def get_errors(self, field_name) -> List: @@ -31,19 +31,19 @@ def get_errors(self, field_name) -> List: class RowWithCustomFieldFormObject: field: Any errors: Dict[str, List] - jinja_environment: Environment + jinja_environment: JinjaEnvironment def __post_init__(self): assert self.field is not None assert isinstance( - self.jinja_environment, Environment + self.jinja_environment, JinjaEnvironment ), self.jinja_environment def render(self): - template: Template = self.jinja_environment.get_template( - "components/grammar_form_element/row_with_custom_field/index.jinja" + rendered_template = self.jinja_environment.render_template_as_markup( + "components/grammar_form_element/row_with_custom_field/index.jinja", + form_object=self, ) - rendered_template = template.render(form_object=self) return rendered_template def get_errors(self, field_name) -> List: @@ -54,19 +54,19 @@ def get_errors(self, field_name) -> List: class RowWithRelationFormObject: relation: Any errors: Dict[str, List] - jinja_environment: Environment + jinja_environment: JinjaEnvironment def __post_init__(self): assert self.relation is not None assert isinstance( - self.jinja_environment, Environment + self.jinja_environment, JinjaEnvironment ), self.jinja_environment def render(self): - template: Template = self.jinja_environment.get_template( - "components/grammar_form_element/row_with_relation/index.jinja" + rendered_template = self.jinja_environment.render_template_as_markup( + "components/grammar_form_element/row_with_relation/index.jinja", + form_object=self, ) - rendered_template = template.render(form_object=self) return rendered_template def get_errors(self, field_name) -> List: diff --git a/strictdoc/export/html/form_objects/grammar_element_form_object.py b/strictdoc/export/html/form_objects/grammar_element_form_object.py index bf74916c2..829ac38a2 100644 --- a/strictdoc/export/html/form_objects/grammar_element_form_object.py +++ b/strictdoc/export/html/form_objects/grammar_element_form_object.py @@ -1,7 +1,7 @@ # mypy: disable-error-code="arg-type,no-any-return,no-untyped-call,no-untyped-def,union-attr,type-arg" from typing import Dict, List, Optional, Set, Tuple, Union -from jinja2 import Environment, Template +from jinja2 import Template from starlette.datastructures import FormData from strictdoc.backend.sdoc.models.document import SDocDocument @@ -23,6 +23,7 @@ RowWithRelationFormObject, RowWithReservedFieldFormObject, ) +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.helpers.auto_described import auto_described from strictdoc.helpers.cast import assert_cast from strictdoc.helpers.form_data import parse_form_data @@ -108,7 +109,7 @@ def __init__( fields: List[GrammarFormField], relations: List[GrammarFormRelation], project_config: ProjectConfig, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ): assert isinstance(document_mid, str), document_mid super().__init__() @@ -118,7 +119,7 @@ def __init__( self.fields: List[GrammarFormField] = fields self.relations: List[GrammarFormRelation] = relations self.project_config: ProjectConfig = project_config - self.jinja_environment: Environment = jinja_environment + self.jinja_environment: JinjaEnvironment = jinja_environment @staticmethod def create_from_request( @@ -126,7 +127,7 @@ def create_from_request( document: SDocDocument, request_form_data: FormData, project_config: ProjectConfig, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ) -> "GrammarElementFormObject": form_object_fields: List[GrammarFormField] = [] form_object_relations: List[GrammarFormRelation] = [] @@ -193,7 +194,7 @@ def create_from_document( document: SDocDocument, element_mid: str, project_config: ProjectConfig, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ) -> "GrammarElementFormObject": assert isinstance(document, SDocDocument) assert isinstance(document.grammar, DocumentGrammar) @@ -371,10 +372,9 @@ def render(self): ) def render_after_validation(self): - template: Template = self.jinja_environment.get_template( + rendered_template = self.jinja_environment.render_template_as_markup( "components/grammar_form_element/index.jinja" ) - rendered_template = template.render(form_object=self) return render_turbo_stream( content=rendered_template, action="update", target="modal" ) diff --git a/strictdoc/export/html/form_objects/grammar_form_object.py b/strictdoc/export/html/form_objects/grammar_form_object.py index 01d07d6fb..aa9801b96 100644 --- a/strictdoc/export/html/form_objects/grammar_form_object.py +++ b/strictdoc/export/html/form_objects/grammar_form_object.py @@ -1,7 +1,6 @@ # mypy: disable-error-code="arg-type,no-any-return,no-untyped-call,no-untyped-def,type-arg" from typing import Dict, List, Optional, Set -from jinja2 import Environment, Template from starlette.datastructures import FormData from strictdoc.backend.sdoc.models.document import SDocDocument @@ -13,6 +12,7 @@ from strictdoc.export.html.form_objects.rows.row_with_grammar_element_form_object import ( RowWithGrammarElementFormObject, ) +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.helpers.auto_described import auto_described from strictdoc.helpers.cast import assert_cast from strictdoc.helpers.form_data import parse_form_data @@ -68,7 +68,7 @@ def __init__( document_mid: str, fields: List[GrammarElementFormField], project_config: ProjectConfig, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, imported_grammar_file: Optional[str], ): assert isinstance(document_mid, str), document_mid @@ -76,7 +76,7 @@ def __init__( self.document_mid = document_mid self.fields: List[GrammarElementFormField] = fields self.project_config: ProjectConfig = project_config - self.jinja_environment: Environment = jinja_environment + self.jinja_environment: JinjaEnvironment = jinja_environment self.imported_grammar_file: Optional[str] = imported_grammar_file @staticmethod @@ -85,7 +85,7 @@ def create_from_request( document_mid: str, request_form_data: FormData, project_config: ProjectConfig, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ) -> "GrammarFormObject": form_object_fields: List[GrammarElementFormField] = [] request_form_data_as_list = [ @@ -125,7 +125,7 @@ def create_from_document( *, document: SDocDocument, project_config: ProjectConfig, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ) -> "GrammarFormObject": assert isinstance(document, SDocDocument) assert isinstance(document.grammar, DocumentGrammar) @@ -180,10 +180,9 @@ def validate(self) -> bool: return len(self.errors) == 0 def render(self): - template: Template = self.jinja_environment.get_template( - "components/grammar_form/index.jinja" + rendered_template = self.jinja_environment.render_template_as_markup( + "components/grammar_form/index.jinja", form_object=self ) - rendered_template = template.render(form_object=self) return render_turbo_stream( content=rendered_template, action="update", target="modal" ) diff --git a/strictdoc/export/html/form_objects/included_document_form_object.py b/strictdoc/export/html/form_objects/included_document_form_object.py index 368ad1ce4..be94b0fc1 100644 --- a/strictdoc/export/html/form_objects/included_document_form_object.py +++ b/strictdoc/export/html/form_objects/included_document_form_object.py @@ -3,10 +3,10 @@ from collections import defaultdict from typing import Dict, List, Optional -from jinja2 import Environment, Template from starlette.datastructures import FormData from strictdoc.backend.sdoc.models.document import SDocDocument +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.helpers.auto_described import auto_described from strictdoc.helpers.cast import assert_cast from strictdoc.helpers.form_data import parse_form_data @@ -23,7 +23,7 @@ def __init__( document_mid: str, context_document_mid: str, document_title: str, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ): assert isinstance(document_mid, str), document_mid assert isinstance(context_document_mid, str), context_document_mid @@ -32,11 +32,11 @@ def __init__( self.document_mid: Optional[str] = document_mid self.context_document_mid: Optional[str] = context_document_mid self.document_title: str = document_title - self.jinja_environment: Environment = jinja_environment + self.jinja_environment: JinjaEnvironment = jinja_environment @staticmethod def create_from_request( - *, request_form_data: FormData, jinja_environment: Environment + *, request_form_data: FormData, jinja_environment: JinjaEnvironment ) -> "IncludedDocumentFormObject": request_form_data_as_list = [ (field_name, field_value) @@ -76,7 +76,7 @@ def create_from_document( *, document: SDocDocument, context_document_mid: str, - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ) -> "IncludedDocumentFormObject": assert isinstance(document, SDocDocument) @@ -88,10 +88,9 @@ def create_from_document( ) def render_edit_form(self): - template: Template = self.jinja_environment.get_template( - "components/included_document_form/index.jinja" + rendered_template = self.jinja_environment.render_template_as_markup( + "components/included_document_form/index.jinja", form_object=self ) - rendered_template = template.render(form_object=self) return render_turbo_stream( content=rendered_template, action="replace", diff --git a/strictdoc/export/html/form_objects/rows/row_with_grammar_element_form_object.py b/strictdoc/export/html/form_objects/rows/row_with_grammar_element_form_object.py index a604622e2..b8143ed9b 100644 --- a/strictdoc/export/html/form_objects/rows/row_with_grammar_element_form_object.py +++ b/strictdoc/export/html/form_objects/rows/row_with_grammar_element_form_object.py @@ -2,33 +2,33 @@ from dataclasses import dataclass from typing import Any, Dict, List -from jinja2 import Environment, Template +from strictdoc.export.html.html_templates import JinjaEnvironment @dataclass class RowWithGrammarElementFormObject: field: Any errors: Dict[str, List] - jinja_environment: Environment + jinja_environment: JinjaEnvironment def __post_init__(self): assert self.field is not None assert isinstance( - self.jinja_environment, Environment + self.jinja_environment, JinjaEnvironment ), self.jinja_environment def render(self): if self.field.is_new: - template: Template = self.jinja_environment.get_template( - "components/grammar_form/row_with_new_grammar_element/index.jinja" + rendered_template = self.jinja_environment.render_template_as_markup( + "components/grammar_form/row_with_new_grammar_element/index.jinja", + form_object=self, ) - rendered_template = template.render(form_object=self) return rendered_template else: - template: Template = self.jinja_environment.get_template( - "components/grammar_form/row_with_grammar_element/index.jinja" + rendered_template = self.jinja_environment.render_template_as_markup( + "components/grammar_form/row_with_grammar_element/index.jinja", + form_object=self, ) - rendered_template = template.render(form_object=self) return rendered_template def get_errors(self, field_name) -> List: diff --git a/strictdoc/export/html/generators/source_file_coverage.py b/strictdoc/export/html/generators/source_file_coverage.py index cdcacc489..71aaf4ff7 100644 --- a/strictdoc/export/html/generators/source_file_coverage.py +++ b/strictdoc/export/html/generators/source_file_coverage.py @@ -1,10 +1,9 @@ # mypy: disable-error-code="no-untyped-call,no-untyped-def" -from jinja2 import Environment from strictdoc import __version__ from strictdoc.core.project_config import ProjectConfig from strictdoc.core.traceability_index import TraceabilityIndex -from strictdoc.export.html.html_templates import HTMLTemplates +from strictdoc.export.html.html_templates import HTMLTemplates, JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer @@ -24,11 +23,10 @@ def __init__( self.is_running_on_server: bool = project_config.is_running_on_server self.strictdoc_version = __version__ - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template( - "screens/source_file_coverage/index.jinja" + def render_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/source_file_coverage/index.jinja", view_object=self ) - return template.render(view_object=self) def render_static_url(self, url: str): return self.link_renderer.render_static_url(url) diff --git a/strictdoc/export/html/generators/view_objects/diff_screen_results_view_object.py b/strictdoc/export/html/generators/view_objects/diff_screen_results_view_object.py index 98439cef6..626bbff45 100644 --- a/strictdoc/export/html/generators/view_objects/diff_screen_results_view_object.py +++ b/strictdoc/export/html/generators/view_objects/diff_screen_results_view_object.py @@ -3,10 +3,9 @@ from datetime import datetime from typing import Optional -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.core.project_config import ProjectConfig +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer from strictdoc.git.change_generator import ChangeContainer @@ -59,10 +58,10 @@ def __init__( self.strictdoc_version = __version__ self.error_message: Optional[str] = None - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.overlay(autoescape=False).get_template( - "screens/git/index.jinja" - ) + def render_screen(self, jinja_environment: JinjaEnvironment): + template = jinja_environment.environment.overlay( + autoescape=False + ).get_template("screens/git/index.jinja") return template.render(view_object=self) def render_url(self, url: str): diff --git a/strictdoc/export/html/generators/view_objects/diff_screen_view_object.py b/strictdoc/export/html/generators/view_objects/diff_screen_view_object.py index dd535906e..003139136 100644 --- a/strictdoc/export/html/generators/view_objects/diff_screen_view_object.py +++ b/strictdoc/export/html/generators/view_objects/diff_screen_view_object.py @@ -2,10 +2,9 @@ from dataclasses import dataclass from datetime import datetime -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.core.project_config import ProjectConfig +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer @@ -40,9 +39,10 @@ def __init__( self.is_running_on_server: bool = project_config.is_running_on_server self.strictdoc_version = __version__ - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template("screens/git/index.jinja") - return template.render(view_object=self) + def render_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/git/index.jinja", view_object=self + ) def render_url(self, url: str): return self.link_renderer.render_url(url) diff --git a/strictdoc/export/html/generators/view_objects/document_screen_view_object.py b/strictdoc/export/html/generators/view_objects/document_screen_view_object.py index b5c272cd9..b6a648c4e 100644 --- a/strictdoc/export/html/generators/view_objects/document_screen_view_object.py +++ b/strictdoc/export/html/generators/view_objects/document_screen_view_object.py @@ -3,7 +3,7 @@ from datetime import datetime from typing import List, Optional, Union -from jinja2 import Environment, Template +from jinja2 import Template from strictdoc import __version__ from strictdoc.backend.sdoc.models.document import SDocDocument @@ -15,6 +15,7 @@ from strictdoc.core.project_config import ProjectConfig from strictdoc.core.traceability_index import TraceabilityIndex from strictdoc.export.html.document_type import DocumentType +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer from strictdoc.server.helpers.turbo import render_turbo_stream @@ -63,68 +64,65 @@ def __init__( def has_included_document(self): return len(self.document.included_documents) > 0 - def render_screen(self, jinja_environment: Environment): + def render_screen(self, jinja_environment: JinjaEnvironment): if self.document_type.is_document: if self.document.config.layout == "Website": - template = jinja_environment.get_template( - "website/document/index.jinja" - ) - else: - template = jinja_environment.get_template( - "screens/document/document/index.jinja" + return jinja_environment.render_template_as_markup( + "website/document/index.jinja", view_object=self ) + return jinja_environment.render_template_as_markup( + "screens/document/document/index.jinja", view_object=self + ) elif self.document_type.is_table(): - template = jinja_environment.get_template( - "screens/document/table/index.jinja" + return jinja_environment.render_template_as_markup( + "screens/document/table/index.jinja", view_object=self ) elif self.document_type.is_trace(): - template = jinja_environment.get_template( - "screens/document/traceability/index.jinja" + return jinja_environment.render_template_as_markup( + "screens/document/traceability/index.jinja", view_object=self ) elif self.document_type.is_deeptrace: - template = jinja_environment.get_template( - "screens/document/traceability_deep/index.jinja" + return jinja_environment.render_template_as_markup( + "screens/document/traceability_deep/index.jinja", + view_object=self, ) elif self.document_type.is_pdf(): - template = jinja_environment.get_template( - "screens/document/pdf/index.jinja" + return jinja_environment.render_template_as_markup( + "screens/document/pdf/index.jinja", view_object=self ) else: raise NotImplementedError(self.document_type) - return template.render(view_object=self) - def render_table_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template( - "screens/document/table/index.jinja" + def render_table_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/document/table/index.jinja", view_object=self ) - return template.render(view_object=self) - def render_trace_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template( - "screens/document/traceability/index.jinja" + def render_trace_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/document/traceability/index.jinja", view_object=self ) - return template.render(view_object=self) - def render_updated_screen(self, jinja_environment: Environment) -> str: - template = jinja_environment.get_template( + def render_updated_screen(self, jinja_environment: JinjaEnvironment) -> str: + output = jinja_environment.render_template_as_markup( "actions/" "document/" "create_requirement/" - "stream_created_requirement.jinja.html" + "stream_created_requirement.jinja.html", + view_object=self, ) - output = template.render(view_object=self) - toc_template = jinja_environment.get_template( - "actions/document/_shared/stream_updated_toc.jinja.html" + output += jinja_environment.render_template_as_markup( + "actions/document/_shared/stream_updated_toc.jinja.html", + view_object=self, ) - output += toc_template.render(view_object=self) return output def render_updated_nodes_and_toc( self, nodes: List[Union[SDocDocument, SDocNode]], - jinja_environment: Environment, + jinja_environment: JinjaEnvironment, ) -> str: output: str = "" @@ -139,20 +137,22 @@ def render_updated_nodes_and_toc( template_folder = "requirement" else: raise NotImplementedError - template = jinja_environment.get_template( - f"components/{template_folder}/index_extends_node.jinja" + content = jinja_environment.render_template_as_markup( + f"components/{template_folder}/index_extends_node.jinja", + view_object=self, + node=node_, ) output += render_turbo_stream( - content=template.render(view_object=self, node=node_), + content=content, action="replace", target=f"article-{node_.reserved_mid}", ) - toc_template = jinja_environment.get_template( - "screens/document/_shared/toc.jinja" + toc_content = jinja_environment.render_template_as_markup( + "screens/document/_shared/toc.jinja", view_object=self ) output += render_turbo_stream( - content=toc_template.render(view_object=self), + content=toc_content, action="update", target="frame-toc", ) @@ -160,24 +160,24 @@ def render_updated_nodes_and_toc( return output def render_update_document_content_with_moved_node( - self, jinja_environment: Environment, moved_node + self, jinja_environment: JinjaEnvironment, moved_node ) -> str: - template = jinja_environment.get_template( - "screens/document/document/frame_document_content.jinja.html" + content = jinja_environment.render_template_as_markup( + "screens/document/document/frame_document_content.jinja.html", + view_object=self, ) output = render_turbo_stream( - content=template.render(view_object=self), + content=content, action="replace", target="frame_document_content", ) - toc_template = jinja_environment.get_template( - "actions/document/_shared/stream_updated_toc.jinja.html" + toc_content = jinja_environment.render_template_as_markup( + "actions/document/_shared/stream_updated_toc.jinja.html", + view_object=self, + last_moved_node_id=moved_node.reserved_mid, ) output += render_turbo_stream( - toc_template.render( - view_object=self, - last_moved_node_id=moved_node.reserved_mid, - ), + toc_content, action="update", target="frame-toc", ) diff --git a/strictdoc/export/html/generators/view_objects/nestor_view_object.py b/strictdoc/export/html/generators/view_objects/nestor_view_object.py index 00b1cb642..cc62f01f6 100644 --- a/strictdoc/export/html/generators/view_objects/nestor_view_object.py +++ b/strictdoc/export/html/generators/view_objects/nestor_view_object.py @@ -1,8 +1,6 @@ from dataclasses import dataclass from typing import Optional -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.backend.sdoc.models.document import SDocDocument from strictdoc.backend.sdoc.models.document_view import DocumentView @@ -10,7 +8,7 @@ from strictdoc.core.project_config import ProjectConfig from strictdoc.core.traceability_index import TraceabilityIndex from strictdoc.export.html.document_type import DocumentType -from strictdoc.export.html.html_templates import HTMLTemplates +from strictdoc.export.html.html_templates import HTMLTemplates, JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer @@ -55,9 +53,10 @@ def __init__( self.link_document_type: DocumentType = DocumentType.document() self.document: Optional[SDocDocument] = None - def render_screen(self, jinja_environment: Environment) -> str: - template = jinja_environment.get_template("screens/nestor/index.jinja") - return template.render(view_object=self) + def render_screen(self, jinja_environment: JinjaEnvironment) -> str: + return jinja_environment.render_template_as_markup( + "screens/nestor/index.jinja", view_object=self + ) def render_static_url(self, url: str) -> str: return self.link_renderer.render_static_url(url) diff --git a/strictdoc/export/html/generators/view_objects/project_statistics_view_object.py b/strictdoc/export/html/generators/view_objects/project_statistics_view_object.py index 84dd90cfe..e2306eb83 100644 --- a/strictdoc/export/html/generators/view_objects/project_statistics_view_object.py +++ b/strictdoc/export/html/generators/view_objects/project_statistics_view_object.py @@ -2,8 +2,6 @@ from dataclasses import dataclass from datetime import datetime -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.core.document_tree_iterator import DocumentTreeIterator from strictdoc.core.project_config import ProjectConfig @@ -11,6 +9,7 @@ from strictdoc.export.html.generators.view_objects.project_tree_stats import ( DocumentTreeStats, ) +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer @@ -35,11 +34,10 @@ def __init__( self.is_running_on_server: bool = project_config.is_running_on_server self.strictdoc_version = __version__ - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template( - "screens/project_statistics/index.jinja" + def render_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/project_statistics/index.jinja", view_object=self ) - return template.render(view_object=self) def render_static_url(self, url: str): return self.link_renderer.render_static_url(url) diff --git a/strictdoc/export/html/generators/view_objects/project_tree_view_object.py b/strictdoc/export/html/generators/view_objects/project_tree_view_object.py index 0678f750b..4f2559ef1 100644 --- a/strictdoc/export/html/generators/view_objects/project_tree_view_object.py +++ b/strictdoc/export/html/generators/view_objects/project_tree_view_object.py @@ -1,12 +1,11 @@ # mypy: disable-error-code="no-any-return,no-untyped-call,no-untyped-def" from dataclasses import dataclass -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.core.document_tree_iterator import DocumentTreeIterator from strictdoc.core.project_config import ProjectConfig from strictdoc.core.traceability_index import TraceabilityIndex +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer @@ -36,11 +35,10 @@ def __init__( traceability_index.contains_included_documents ) - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template( - "screens/project_index/index.jinja" + def render_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/project_index/index.jinja", view_object=self ) - return template.render(view_object=self) def render_static_url(self, url: str): return self.link_renderer.render_static_url(url) diff --git a/strictdoc/export/html/generators/view_objects/search_screen_view_object.py b/strictdoc/export/html/generators/view_objects/search_screen_view_object.py index 124e7e2d1..b537733c6 100644 --- a/strictdoc/export/html/generators/view_objects/search_screen_view_object.py +++ b/strictdoc/export/html/generators/view_objects/search_screen_view_object.py @@ -3,8 +3,6 @@ from datetime import datetime from typing import Optional -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.backend.sdoc.models.document import SDocDocument from strictdoc.backend.sdoc.models.document_view import DocumentView @@ -13,7 +11,7 @@ from strictdoc.core.project_config import ProjectConfig from strictdoc.core.traceability_index import TraceabilityIndex from strictdoc.export.html.document_type import DocumentType -from strictdoc.export.html.html_templates import HTMLTemplates +from strictdoc.export.html.html_templates import HTMLTemplates, JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer @@ -67,9 +65,10 @@ def render_truncated_node_statement(self, node): self.document_type, node ) - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template("screens/search/index.jinja") - return template.render(view_object=self) + def render_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/search/index.jinja", view_object=self + ) def is_empty_tree(self) -> bool: return self.document_tree_iterator.is_empty_tree() diff --git a/strictdoc/export/html/generators/view_objects/source_file_view_object.py b/strictdoc/export/html/generators/view_objects/source_file_view_object.py index b0a40f86e..6f62c38fe 100644 --- a/strictdoc/export/html/generators/view_objects/source_file_view_object.py +++ b/strictdoc/export/html/generators/view_objects/source_file_view_object.py @@ -3,13 +3,12 @@ from datetime import datetime from typing import List -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.core.document_tree_iterator import DocumentTreeIterator from strictdoc.core.finders.source_files_finder import SourceFile from strictdoc.core.project_config import ProjectConfig from strictdoc.core.traceability_index import TraceabilityIndex +from strictdoc.export.html.html_templates import JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer @@ -44,11 +43,10 @@ def __init__( self.is_running_on_server: bool = project_config.is_running_on_server self.strictdoc_version = __version__ - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template( - "screens/source_file_view/index.jinja" + def render_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/source_file_view/index.jinja", view_object=self ) - return template.render(view_object=self) def is_empty_tree(self) -> bool: return self.document_tree_iterator.is_empty_tree() diff --git a/strictdoc/export/html/generators/view_objects/traceability_matrix_view_object.py b/strictdoc/export/html/generators/view_objects/traceability_matrix_view_object.py index 3d63e213e..fa886af54 100644 --- a/strictdoc/export/html/generators/view_objects/traceability_matrix_view_object.py +++ b/strictdoc/export/html/generators/view_objects/traceability_matrix_view_object.py @@ -1,15 +1,13 @@ # mypy: disable-error-code="arg-type,no-any-return,no-untyped-call,no-untyped-def,union-attr,type-arg" from typing import Dict, List, Optional, Tuple -from jinja2 import Environment - from strictdoc import __version__ from strictdoc.backend.sdoc.models.document import SDocDocument from strictdoc.backend.sdoc.models.document_grammar import DocumentGrammar from strictdoc.core.document_tree_iterator import DocumentTreeIterator from strictdoc.core.project_config import ProjectConfig from strictdoc.core.traceability_index import TraceabilityIndex -from strictdoc.export.html.html_templates import HTMLTemplates +from strictdoc.export.html.html_templates import HTMLTemplates, JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer @@ -42,11 +40,10 @@ def iterate_documents(self): self.traceability_index.document_tree.document_list, ) - def render_screen(self, jinja_environment: Environment): - template = jinja_environment.get_template( - "screens/traceability_matrix/index.jinja" + def render_screen(self, jinja_environment: JinjaEnvironment): + return jinja_environment.render_template_as_markup( + "screens/traceability_matrix/index.jinja", view_object=self ) - return template.render(view_object=self) def render_static_url(self, url: str): return self.link_renderer.render_static_url(url) diff --git a/strictdoc/export/html/html_templates.py b/strictdoc/export/html/html_templates.py index 5769b7731..fedeecfb5 100644 --- a/strictdoc/export/html/html_templates.py +++ b/strictdoc/export/html/html_templates.py @@ -17,6 +17,7 @@ nodes, ) from jinja2.ext import Extension +from markupsafe import Markup from strictdoc import environment from strictdoc.core.project_config import ProjectConfig @@ -24,6 +25,23 @@ from strictdoc.helpers.timing import measure_performance +class JinjaEnvironment: + environment: Environment + + def __init__(self, environment: Environment): + self.environment = environment + + def get_template(self, *args, **kwargs): + return self.environment.get_template(*args, **kwargs) + + def render_template_as_markup( + self, template: str, *args, **kwargs + ) -> Markup: + return Markup( + self.environment.get_template(template).render(*args, **kwargs) + ) + + # https://stackoverflow.com/questions/21778252/how-to-raise-an-exception-in-a-jinja2-macro class AssertExtension(Extension): # This is our keyword(s): @@ -90,7 +108,7 @@ def create( return NormalHTMLTemplates() - def jinja_environment(self) -> Environment: + def jinja_environment(self) -> JinjaEnvironment: raise NotImplementedError def reset_jinja_environment_if_outdated( @@ -101,14 +119,18 @@ def reset_jinja_environment_if_outdated( class NormalHTMLTemplates(HTMLTemplates): def __init__(self): - self._jinja_environment: Environment = Environment( - loader=FileSystemLoader(environment.get_path_to_html_templates()), - undefined=StrictUndefined, - extensions=[AssertExtension], - autoescape=True, + self._jinja_environment: JinjaEnvironment = JinjaEnvironment( + Environment( + loader=FileSystemLoader( + environment.get_path_to_html_templates() + ), + undefined=StrictUndefined, + extensions=[AssertExtension], + autoescape=True, + ) ) - def jinja_environment(self) -> Environment: + def jinja_environment(self) -> JinjaEnvironment: return self._jinja_environment def reset_jinja_environment_if_outdated( @@ -131,7 +153,7 @@ def __init__(self, project_config: ProjectConfig): CompiledHTMLTemplates.PATH_TO_JINJA_CACHE_DIR, path_to_output_dir_hash, ) - self._jinja_environment: Optional[Environment] = None + self._jinja_environment: Optional[JinjaEnvironment] = None def compile_jinja_templates(self): if os.path.isdir(self.path_to_jinja_cache_bucket_dir): @@ -140,6 +162,7 @@ def compile_jinja_templates(self): loader=FileSystemLoader(environment.get_path_to_html_templates()), undefined=StrictUndefined, extensions=[AssertExtension], + autoescape=True, ) # TODO: Check if this line is still needed (might be some older workaround). jinja_environment.globals.update(isinstance=isinstance) @@ -163,14 +186,17 @@ def filter_function_(name: str) -> bool: ignore_errors=False, ) - def jinja_environment(self) -> Environment: + def jinja_environment(self) -> JinjaEnvironment: if self._jinja_environment is not None: return self._jinja_environment assert os.path.isdir(self.path_to_jinja_cache_bucket_dir) - self._jinja_environment = Environment( - loader=ModuleLoader(self.path_to_jinja_cache_bucket_dir), - undefined=StrictUndefined, - extensions=[AssertExtension], + self._jinja_environment = JinjaEnvironment( + Environment( + loader=ModuleLoader(self.path_to_jinja_cache_bucket_dir), + undefined=StrictUndefined, + extensions=[AssertExtension], + autoescape=True, + ) ) return self._jinja_environment diff --git a/strictdoc/server/helpers/turbo.py b/strictdoc/server/helpers/turbo.py index 47bed28af..9b468c21a 100644 --- a/strictdoc/server/helpers/turbo.py +++ b/strictdoc/server/helpers/turbo.py @@ -1,3 +1,6 @@ +from markupsafe import Markup + + # mypy: disable-error-code="no-untyped-def" def render_turbo_stream(content: str, action: str, target: str): assert action in ("append", "replace", "update") @@ -9,4 +12,4 @@ def render_turbo_stream(content: str, action: str, target: str): """ - return turbo_stream + return Markup(turbo_stream) diff --git a/strictdoc/server/routers/main_router.py b/strictdoc/server/routers/main_router.py index 6d9a42aa2..77cc73915 100644 --- a/strictdoc/server/routers/main_router.py +++ b/strictdoc/server/routers/main_router.py @@ -7,7 +7,6 @@ from typing import Dict, List, Optional, Union from fastapi import APIRouter, Form, UploadFile -from jinja2 import Environment from reqif.models.error_handling import ReqIFXMLParsingError from reqif.parser import ReqIFParser from reqif.unparser import ReqIFUnparser @@ -112,7 +111,7 @@ SearchScreenViewObject, ) from strictdoc.export.html.html_generator import HTMLGenerator -from strictdoc.export.html.html_templates import HTMLTemplates +from strictdoc.export.html.html_templates import HTMLTemplates, JinjaEnvironment from strictdoc.export.html.renderers.link_renderer import LinkRenderer from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer from strictdoc.export.html2pdf.pdf_print_driver import PDFPrintDriver @@ -183,7 +182,7 @@ def create_main_router( export_output_html_root=project_config.export_output_html_root, ) - def env() -> Environment: + def env() -> JinjaEnvironment: return html_templates.jinja_environment() router = APIRouter() @@ -201,12 +200,6 @@ def requirement__show_full(reference_mid: str): requirement: SDocNode = ( export_action.traceability_index.get_node_by_mid(MID(reference_mid)) ) - template = env().get_template( - "actions/" - "node/" - "show_full_node/" - "stream_show_full_requirement.jinja" - ) link_renderer = LinkRenderer( root_path=requirement.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -228,8 +221,13 @@ def requirement__show_full(reference_mid: str): markup_renderer=markup_renderer, standalone=False, ) - output = template.render( - view_object=view_object, requirement=requirement + output = env().render_template_as_markup( + "actions/" + "node/" + "show_full_node/" + "stream_show_full_requirement.jinja", + view_object=view_object, + requirement=requirement, ) return HTMLResponse( content=output, @@ -244,12 +242,6 @@ def section__show_full(reference_mid: str): section: SDocSection = export_action.traceability_index.get_node_by_mid( MID(reference_mid) ) - template = env().get_template( - "actions/" - "node/" - "show_full_node/" - "stream_show_full_section.jinja" - ) link_renderer = LinkRenderer( root_path=section.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -265,7 +257,11 @@ def section__show_full(reference_mid: str): current_view: ViewElement = section.document.view.get_current_view( project_config.view ) - output = template.render( + output = env().render_template_as_markup( + "actions/" + "node/" + "show_full_node/" + "stream_show_full_section.jinja", renderer=markup_renderer, section=section, traceability_index=export_action.traceability_index, @@ -317,9 +313,6 @@ def get_new_section( else: raise NotImplementedError - template = env().get_template( - "actions/document/create_section/stream_new_section.jinja.html" - ) link_renderer = LinkRenderer( root_path=document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -332,7 +325,8 @@ def get_new_section( config=project_config, context_document=document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/document/create_section/stream_new_section.jinja.html", renderer=markup_renderer, form_object=section_form_object, reference_mid=reference_mid, @@ -392,9 +386,6 @@ async def create_section(request: Request): for error_key, errors in validation_error.errors.items(): for error in errors: form_object.add_error(error_key, error) - template = env().get_template( - "actions/document/create_section/stream_new_section.jinja.html" - ) link_renderer = LinkRenderer( root_path=context_document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -407,7 +398,8 @@ async def create_section(request: Request): config=project_config, context_document=context_document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/document/create_section/stream_new_section.jinja.html", renderer=markup_renderer, form_object=form_object, reference_mid=reference_mid, @@ -458,17 +450,13 @@ async def create_section(request: Request): ) # Rendering back the Turbo template. - template = env().get_template( - "actions/document/create_section/stream_created_section.jinja.html" - ) - output = template.render( + output = env().render_template_as_markup( + "actions/document/create_section/stream_created_section.jinja.html", view_object=view_object, ) - toc_template = env().get_template( - "actions/document/_shared/stream_updated_toc.jinja.html" - ) - output += toc_template.render( + output += env().render_template_as_markup( + "actions/document/_shared/stream_updated_toc.jinja.html", view_object=view_object, ) return HTMLResponse( @@ -487,9 +475,6 @@ def get_edit_section(node_id: str, context_document_mid: str): form_object = SectionFormObject.create_from_section( section=section, context_document_mid=context_document_mid ) - template = env().get_template( - "actions/document/edit_section/stream_edit_section.jinja.html" - ) link_renderer = LinkRenderer( root_path=section.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -502,7 +487,8 @@ def get_edit_section(node_id: str, context_document_mid: str): config=project_config, context_document=section.document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/document/edit_section/stream_edit_section.jinja.html", renderer=markup_renderer, form_object=form_object, document_type=DocumentType.document(), @@ -548,9 +534,6 @@ async def put_update_section(request: Request): for error_key, errors in validation_error.errors.items(): for error in errors: form_object.add_error(error_key, error) - template = env().get_template( - "actions/document/edit_section/stream_edit_section.jinja.html" - ) link_renderer = LinkRenderer( root_path=section.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -563,7 +546,8 @@ async def put_update_section(request: Request): config=project_config, context_document=section.document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/document/edit_section/stream_edit_section.jinja.html", renderer=markup_renderer, link_renderer=link_renderer, form_object=form_object, @@ -598,9 +582,6 @@ async def put_update_section(request: Request): ) # Rendering back the Turbo template. - template = env().get_template( - "actions/document/edit_section/stream_updated_section.jinja.html" - ) link_renderer = LinkRenderer( root_path=section.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -622,11 +603,15 @@ async def put_update_section(request: Request): markup_renderer=markup_renderer, standalone=False, ) - output = template.render(node=section, view_object=view_object) - toc_template = env().get_template( - "actions/document/_shared/stream_updated_toc.jinja.html" + output = env().render_template_as_markup( + "actions/document/edit_section/stream_updated_section.jinja.html", + node=section, + view_object=view_object, + ) + output += env().render_template_as_markup( + "actions/document/_shared/stream_updated_toc.jinja.html", + view_object=view_object, ) - output += toc_template.render(view_object=view_object) return HTMLResponse( content=output, headers={ @@ -636,13 +621,13 @@ async def put_update_section(request: Request): @router.get("/actions/document/cancel_new_section", response_class=Response) def cancel_new_section(section_mid: str): - template = env().get_template( + output = env().render_template_as_markup( "actions/" "document/" "create_section/" - "stream_cancel_new_section.jinja.html" + "stream_cancel_new_section.jinja.html", + section_mid=section_mid, ) - output = template.render(section_mid=section_mid) return HTMLResponse( content=output, headers={ @@ -657,9 +642,6 @@ def cancel_edit_section(section_mid: str): section: SDocSection = export_action.traceability_index.get_node_by_mid( MID(section_mid) ) - template = env().get_template( - "actions/document/edit_section/stream_updated_section.jinja.html" - ) link_renderer = LinkRenderer( root_path=section.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -681,7 +663,11 @@ def cancel_edit_section(section_mid: str): markup_renderer=markup_renderer, standalone=False, ) - output = template.render(view_object=view_object, node=section) + output = env().render_template_as_markup( + "actions/document/edit_section/stream_updated_section.jinja.html", + view_object=view_object, + node=section, + ) return HTMLResponse( content=output, status_code=200, @@ -751,12 +737,6 @@ def get_new_requirement( else: raise NotImplementedError - template = env().get_template( - "actions/" - "document/" - "create_requirement/" - "stream_new_requirement.jinja.html" - ) link_renderer = LinkRenderer( root_path=document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -769,7 +749,11 @@ def get_new_requirement( config=project_config, context_document=document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/" + "document/" + "create_requirement/" + "stream_new_requirement.jinja.html", is_new_requirement=True, renderer=markup_renderer, form_object=form_object, @@ -822,12 +806,6 @@ def get_clone_requirement(reference_mid: str, context_document_mid: str): whereto = NodeCreationOrder.AFTER replace_action = "after" - template = env().get_template( - "actions/" - "document/" - "create_requirement/" - "stream_new_requirement.jinja.html" - ) link_renderer = LinkRenderer( root_path=document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -840,7 +818,11 @@ def get_clone_requirement(reference_mid: str, context_document_mid: str): config=project_config, context_document=document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/" + "document/" + "create_requirement/" + "stream_new_requirement.jinja.html", is_new_requirement=True, renderer=markup_renderer, form_object=form_object, @@ -911,12 +893,6 @@ async def create_requirement(request: Request): command.perform() if form_object.any_errors(): - template = env().get_template( - "actions/" - "document/" - "create_requirement/" - "stream_new_requirement.jinja.html" - ) link_renderer = LinkRenderer( root_path=document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -929,8 +905,11 @@ async def create_requirement(request: Request): config=project_config, context_document=document, ) - - output = template.render( + output = env().render_template_as_markup( + "actions/" + "document/" + "create_requirement/" + "stream_new_requirement.jinja.html", is_new_requirement=True, renderer=markup_renderer, form_object=form_object, @@ -1008,12 +987,6 @@ def get_edit_requirement(node_id: str, context_document_mid: str): ) ) document = requirement.document - template = env().get_template( - "actions/" - "document/" - "edit_requirement/" - "stream_edit_requirement.jinja.html" - ) link_renderer = LinkRenderer( root_path=document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -1026,7 +999,11 @@ def get_edit_requirement(node_id: str, context_document_mid: str): config=project_config, context_document=document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/" + "document/" + "edit_requirement/" + "stream_edit_requirement.jinja.html", is_new_requirement=False, renderer=markup_renderer, form_object=form_object, @@ -1075,10 +1052,8 @@ def reset_uid(reference_mid: str): field_type=RequirementFormFieldType.SINGLELINE, field_value=next_uid, ) - template = env().get_template( - "components/form/row/row_uid_with_reset/stream.jinja" - ) - output = template.render( + output = env().render_template_as_markup( + "components/form/row/row_uid_with_reset/stream.jinja", next_uid=next_uid, reference_mid=reference_mid, uid_form_field=uid_form_field, @@ -1144,12 +1119,6 @@ async def document__update_requirement(request: Request): update_requirement_command_result_or_none = update_command.perform() if form_object.any_errors(): - template = env().get_template( - "actions/" - "document/" - "edit_requirement/" - "stream_edit_requirement.jinja.html" - ) link_renderer = LinkRenderer( root_path=document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -1162,7 +1131,11 @@ async def document__update_requirement(request: Request): config=project_config, context_document=document, ) - output = template.render( + output = env().render_template_as_markup( + "actions/" + "document/" + "edit_requirement/" + "stream_edit_requirement.jinja.html", is_new_requirement=False, renderer=markup_renderer, requirement=requirement, @@ -1234,13 +1207,13 @@ async def document__update_requirement(request: Request): "/actions/document/cancel_new_requirement", response_class=Response ) def cancel_new_requirement(requirement_mid: str): - template = env().get_template( + output = env().render_template_as_markup( "actions/" "document/" "create_requirement/" - "stream_cancel_new_requirement.jinja.html" + "stream_cancel_new_requirement.jinja.html", + requirement_mid=requirement_mid, ) - output = template.render(requirement_mid=requirement_mid) return HTMLResponse( content=output, status_code=200, @@ -1318,11 +1291,9 @@ def delete_section( errors = [] except MultipleValidationErrorAsList as error_: errors = error_.errors - template = env().get_template( + output = env().render_template_as_markup( "actions/document/delete_section/" - "stream_confirm_delete_section.jinja" - ) - output = template.render( + "stream_confirm_delete_section.jinja", section_mid=node_id, context_document_mid=context_document_mid, errors=errors, @@ -1342,11 +1313,9 @@ def delete_section( delete_command.perform() except MultipleValidationErrorAsList as error_: errors = error_.errors - template = env().get_template( + output = env().render_template_as_markup( "actions/document/delete_section/" - "stream_confirm_delete_section.jinja" - ) - output = template.render( + "stream_confirm_delete_section.jinja", section_mid=node_id, context_document_mid=context_document_mid, errors=errors, @@ -1370,9 +1339,6 @@ def delete_section( SDWriter(project_config).write_to_file(section.document) # Rendering back the Turbo template. - template = env().get_template( - "actions/document/delete_section/stream_delete_section.jinja.html" - ) link_renderer = LinkRenderer( root_path=section.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -1394,11 +1360,14 @@ def delete_section( markup_renderer=markup_renderer, standalone=False, ) - output = template.render(view_object=view_object) - toc_template = env().get_template( - "actions/document/_shared/stream_updated_toc.jinja.html" + output = env().render_template_as_markup( + "actions/document/delete_section/stream_delete_section.jinja.html", + view_object=view_object, + ) + output += env().render_template_as_markup( + "actions/document/_shared/stream_updated_toc.jinja.html", + view_object=view_object, ) - output += toc_template.render(view_object=view_object) return HTMLResponse( content=output, status_code=200, @@ -1429,11 +1398,9 @@ def delete_requirement( except MultipleValidationErrorAsList as error_: errors = error_.errors - template = env().get_template( + output = env().render_template_as_markup( "actions/document/delete_requirement/" - "stream_confirm_delete_requirement.jinja" - ) - output = template.render( + "stream_confirm_delete_requirement.jinja", requirement_mid=node_id, context_document_mid=context_document_mid, errors=errors, @@ -1472,10 +1439,6 @@ def delete_requirement( ) # Rendering back the Turbo template. - template = env().get_template( - "actions/document/delete_requirement/" - "stream_delete_requirement.jinja.html" - ) link_renderer = LinkRenderer( root_path=requirement.document.meta.get_root_path_prefix(), static_path=project_config.dir_for_sdoc_assets, @@ -1497,12 +1460,16 @@ def delete_requirement( markup_renderer=markup_renderer, standalone=False, ) - output = template.render(view_object=view_object) + output = env().render_template_as_markup( + "actions/document/delete_requirement/" + "stream_delete_requirement.jinja.html", + view_object=view_object, + ) - toc_template = env().get_template( - "actions/document/_shared/stream_updated_toc.jinja.html" + output += env().render_template_as_markup( + "actions/document/_shared/stream_updated_toc.jinja.html", + view_object=view_object, ) - output += toc_template.render(view_object=view_object) return HTMLResponse( content=output, status_code=200, @@ -1609,10 +1576,8 @@ async def move_node(request: Request): # Generic routes @router.get("/actions/project_index/new_document", response_class=Response) def get_new_document(): - template = env().get_template( - "actions/project_index/stream_new_document.jinja.html" - ) - output = template.render( + output = env().render_template_as_markup( + "actions/project_index/stream_new_document.jinja.html", error_object=ErrorObject(), document_title="", document_path="", @@ -1680,10 +1645,8 @@ def document_tree__create_document( ) if error_object.any_errors(): - template = env().get_template( - "actions/project_index/stream_new_document.jinja.html" - ) - output = template.render( + output = env().render_template_as_markup( + "actions/project_index/stream_new_document.jinja.html", error_object=error_object, document_title=document_title if document_title is not None @@ -1755,16 +1718,14 @@ def document_tree__create_document( export_action.build_index() export_action.export() - template = env().get_template( - "actions/project_index/stream_create_document.jinja.html" - ) - view_object = ProjectTreeViewObject( traceability_index=export_action.traceability_index, project_config=project_config, ) - - output = template.render(view_object=view_object) + output = env().render_template_as_markup( + "actions/project_index/stream_create_document.jinja.html", + view_object=view_object, + ) return HTMLResponse( content=output, status_code=200, @@ -1785,15 +1746,13 @@ def document__add_comment( ) assert document.grammar is not None grammar: DocumentGrammar = document.grammar - template = env().get_template( + # The data of the form object is ignored. What matters is the comment + # form data. + output = env().render_template_as_markup( "actions/" "document/" "add_requirement_comment/" - "stream_add_requirement_comment.jinja.html" - ) - # The data of the form object is ignored. What matters is the comment - # form data. - output = template.render( + "stream_add_requirement_comment.jinja.html", requirement_mid=requirement_mid, form_object=RequirementFormObject( is_new=False, @@ -1839,15 +1798,13 @@ def document__add_relation( grammar_element_relations = element.get_relation_types() - template = env().get_template( + # The data of the form object is ignored. What matters is the relation + # form data. + output = env().render_template_as_markup( "actions/" "document/" "add_requirement_relation/" - "stream_add_requirement_relation.jinja.html" - ) - # The data of the form object is ignored. What matters is the relation - # form data. - output = template.render( + "stream_add_requirement_relation.jinja.html", requirement_mid=requirement_mid, form_object=RequirementFormObject( is_new=False, @@ -1887,13 +1844,11 @@ def document__edit_config(document_mid: str): document=document ) - template = env().get_template( + output = env().render_template_as_markup( "actions/" "document/" "edit_document_config/" - "stream_edit_document_config.jinja.html" - ) - output = template.render( + "stream_edit_document_config.jinja.html", form_object=form_object, document=document, ) @@ -1952,13 +1907,11 @@ async def document__save_edit_config(request: Request): for error_key, errors in validation_error.errors.items(): for error in errors: form_object.add_error(error_key, error) - template = env().get_template( + html_output = env().render_template_as_markup( "actions/" "document/" "edit_document_config/" - "stream_edit_document_config.jinja.html" - ) - html_output: str = template.render( + "stream_edit_document_config.jinja.html", form_object=form_object, document=document, ) @@ -1999,13 +1952,13 @@ async def document__save_edit_config(request: Request): markup_renderer=markup_renderer, standalone=False, ) - template = env().get_template( + html_output = env().render_template_as_markup( "actions/" "document/" "edit_document_config/" - "stream_save_document_config.jinja.html" + "stream_save_document_config.jinja.html", + view_object=view_object, ) - html_output = template.render(view_object=view_object) return HTMLResponse( content=html_output, status_code=200, @@ -2110,12 +2063,6 @@ def document__cancel_edit_config(document_mid: str): config=project_config, context_document=document, ) - template = env().get_template( - "actions/" - "document/" - "edit_document_config/" - "stream_cancel_edit_document_config.jinja.html" - ) view_object = DocumentScreenViewObject( document_type=DocumentType.document(), document=document, @@ -2125,7 +2072,14 @@ def document__cancel_edit_config(document_mid: str): markup_renderer=markup_renderer, standalone=False, ) - output = template.render(view_object=view_object, document=document) + output = env().render_template_as_markup( + "actions/" + "document/" + "edit_document_config/" + "stream_cancel_edit_document_config.jinja.html", + view_object=view_object, + document=document, + ) return HTMLResponse( content=output, status_code=200, @@ -2154,9 +2108,6 @@ def document__cancel_edit_included_document(document_mid: str): config=project_config, context_document=document, ) - template = env().get_template( - "actions/document/edit_section/stream_updated_section.jinja.html" - ) view_object = DocumentScreenViewObject( document_type=DocumentType.document(), document=document, @@ -2166,8 +2117,11 @@ def document__cancel_edit_included_document(document_mid: str): markup_renderer=markup_renderer, standalone=False, ) - output = template.render( - view_object=view_object, document=document, node=document + output = env().render_template_as_markup( + "actions/document/edit_section/stream_updated_section.jinja.html", + view_object=view_object, + document=document, + node=document, ) return HTMLResponse( content=output, @@ -2271,14 +2225,12 @@ async def document__save_grammar(request: Request): markup_renderer=markup_renderer, standalone=False, ) - template = env().get_template( + output = env().render_template_as_markup( "actions/" "document/" "_shared/" - "stream_refresh_document.jinja.html" - ) - output = form_object.render_close_form() + template.render( - view_object=view_object + "stream_refresh_document.jinja.html", + view_object=view_object, ) return HTMLResponse( content=output, @@ -2412,14 +2364,15 @@ async def document__save_grammar_element(request: Request): markup_renderer=markup_renderer, standalone=False, ) - template = env().get_template( - "actions/" - "document/" - "_shared/" - "stream_refresh_document.jinja.html" - ) - output = form_object.render_close_form() + template.render( - view_object=view_object + output = ( + form_object.render_close_form() + + env().render_template_as_markup( + "actions/" + "document/" + "_shared/" + "stream_refresh_document.jinja.html", + view_object=view_object, + ) ) return HTMLResponse( content=output, @@ -2474,11 +2427,9 @@ def document__add_grammar_relation(document_mid: str): response_class=Response, ) def get_import_reqif_document_form(): - template = env().get_template( + output = env().render_template_as_markup( "actions/project_index/import_reqif_document/" - "stream_form_import_reqif_document.jinja.html" - ) - output = template.render( + "stream_form_import_reqif_document.jinja.html", error_object=ErrorObject(), ) return HTMLResponse( @@ -2515,11 +2466,9 @@ async def import_document_reqif(reqif_file: UploadFile): error_object.add_error("reqif_file", str(exception)) if error_object.any_errors(): - template = env().get_template( + output = env().render_template_as_markup( "actions/project_index/import_reqif_document/" - "stream_form_import_reqif_document.jinja.html" - ) - output = template.render( + "stream_form_import_reqif_document.jinja.html", error_object=error_object, ) return HTMLResponse( @@ -2571,15 +2520,15 @@ async def import_document_reqif(reqif_file: UploadFile): export_action.build_index() export_action.export() - template = env().get_template( - "actions/project_index/import_reqif_document/" - "stream_refresh_with_imported_reqif_document.jinja.html" - ) view_object = ProjectTreeViewObject( traceability_index=export_action.traceability_index, project_config=project_config, ) - output = template.render(view_object=view_object) + output = env().render_template_as_markup( + "actions/project_index/import_reqif_document/" + "stream_refresh_with_imported_reqif_document.jinja.html", + view_object=view_object, + ) return HTMLResponse( content=output, status_code=200,