Skip to content

Commit

Permalink
Mark all template.render() output as Markup
Browse files Browse the repository at this point in the history
To be discussed... if ok, squash into previous commit.
  • Loading branch information
haxtibal committed Jul 20, 2024
1 parent b59ef71 commit 9b71ab0
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 366 deletions.
32 changes: 16 additions & 16 deletions strictdoc/export/html/form_objects/form_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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__()
Expand All @@ -118,15 +119,15 @@ 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(
*,
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] = []
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
)
Expand Down
15 changes: 7 additions & 8 deletions strictdoc/export/html/form_objects/grammar_form_object.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -68,15 +68,15 @@ 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
super().__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
Expand All @@ -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 = [
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 4 additions & 6 deletions strictdoc/export/html/generators/source_file_coverage.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 9b71ab0

Please sign in to comment.