From bed3a99d167b682da36ba220915bbad53fcef3c0 Mon Sep 17 00:00:00 2001 From: Roberto Prevato Date: Mon, 21 Aug 2023 23:01:46 +0200 Subject: [PATCH] Make .jinja the default extension for Jinja templates --- CHANGELOG.md | 9 +++++++-- blacksheep/__init__.py | 2 +- blacksheep/server/rendering/jinja2.py | 6 ++++-- tests/test_templating.py | 6 ++---- tests/testapp/templates/{form_1.html => form_1.jinja} | 0 tests/testapp/templates/{form_2.html => form_2.jinja} | 0 tests/testapp/templates/{home.html => home.jinja} | 0 .../templates/lorem/{form_1.html => form_1.jinja} | 0 .../testapp/templates/lorem/{hello.html => hello.jinja} | 0 .../testapp/templates/lorem/{index.html => index.jinja} | 0 .../templates/lorem/{nomodel.html => nomodel.jinja} | 0 .../templates/lorem/{specific.html => specific.jinja} | 0 12 files changed, 14 insertions(+), 9 deletions(-) rename tests/testapp/templates/{form_1.html => form_1.jinja} (100%) rename tests/testapp/templates/{form_2.html => form_2.jinja} (100%) rename tests/testapp/templates/{home.html => home.jinja} (100%) rename tests/testapp/templates/lorem/{form_1.html => form_1.jinja} (100%) rename tests/testapp/templates/lorem/{hello.html => hello.jinja} (100%) rename tests/testapp/templates/lorem/{index.html => index.jinja} (100%) rename tests/testapp/templates/lorem/{nomodel.html => nomodel.jinja} (100%) rename tests/testapp/templates/lorem/{specific.html => specific.jinja} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2af0e1f6..e57c9567 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0a10] - 2023-08-21 :broccoli: + +- Add support for `.jinja` extension by @thearchitector. +- Makes the `.jinja` extension default for Jinja templates. + ## [2.0a9] - 2023-07-14 -- Fixes bug #394, causing the `Content` max body size to be 2147483647 - (C int max value). Reported and fixed by @thomafred +- Fixes bug #394, causing the `Content` max body size to be 2147483647. + (C int max value). Reported and fixed by @thomafred. ## [2.0a8] - 2023-07-02 diff --git a/blacksheep/__init__.py b/blacksheep/__init__.py index 2242c0c3..58b61208 100644 --- a/blacksheep/__init__.py +++ b/blacksheep/__init__.py @@ -3,7 +3,7 @@ used types to reduce the verbosity of the imports statements. """ __author__ = "Roberto Prevato " -__version__ = "2.0a9" +__version__ = "2.0a10" from .contents import Content as Content from .contents import FormContent as FormContent diff --git a/blacksheep/server/rendering/jinja2.py b/blacksheep/server/rendering/jinja2.py index e93084bd..5e6851e7 100644 --- a/blacksheep/server/rendering/jinja2.py +++ b/blacksheep/server/rendering/jinja2.py @@ -11,11 +11,13 @@ from .abc import Renderer +_DEFAULT_TEMPLATES_EXTENSION = os.environ.get("APP_JINJA_EXTENSION", ".jinja") + @lru_cache(1200) def get_template_name(name: str) -> str: - if not name.endswith(".html") and not name.endswith(".jinja"): - return name + ".html" + if not name.endswith(_DEFAULT_TEMPLATES_EXTENSION): + return name + _DEFAULT_TEMPLATES_EXTENSION return name diff --git a/tests/test_templating.py b/tests/test_templating.py index 7fc3d312..4426df2b 100644 --- a/tests/test_templating.py +++ b/tests/test_templating.py @@ -322,11 +322,9 @@ def index(self): @pytest.mark.parametrize( "value,expected_name", [ - ("index", "index.html"), - ("index.html", "index.html"), - ("index.html.jinja", "index.html.jinja"), + ("index", "index.jinja"), ("index.jinja", "index.jinja"), - ("default", "default.html"), + ("default", "default.jinja"), ], ) def test_template_name(value, expected_name): diff --git a/tests/testapp/templates/form_1.html b/tests/testapp/templates/form_1.jinja similarity index 100% rename from tests/testapp/templates/form_1.html rename to tests/testapp/templates/form_1.jinja diff --git a/tests/testapp/templates/form_2.html b/tests/testapp/templates/form_2.jinja similarity index 100% rename from tests/testapp/templates/form_2.html rename to tests/testapp/templates/form_2.jinja diff --git a/tests/testapp/templates/home.html b/tests/testapp/templates/home.jinja similarity index 100% rename from tests/testapp/templates/home.html rename to tests/testapp/templates/home.jinja diff --git a/tests/testapp/templates/lorem/form_1.html b/tests/testapp/templates/lorem/form_1.jinja similarity index 100% rename from tests/testapp/templates/lorem/form_1.html rename to tests/testapp/templates/lorem/form_1.jinja diff --git a/tests/testapp/templates/lorem/hello.html b/tests/testapp/templates/lorem/hello.jinja similarity index 100% rename from tests/testapp/templates/lorem/hello.html rename to tests/testapp/templates/lorem/hello.jinja diff --git a/tests/testapp/templates/lorem/index.html b/tests/testapp/templates/lorem/index.jinja similarity index 100% rename from tests/testapp/templates/lorem/index.html rename to tests/testapp/templates/lorem/index.jinja diff --git a/tests/testapp/templates/lorem/nomodel.html b/tests/testapp/templates/lorem/nomodel.jinja similarity index 100% rename from tests/testapp/templates/lorem/nomodel.html rename to tests/testapp/templates/lorem/nomodel.jinja diff --git a/tests/testapp/templates/lorem/specific.html b/tests/testapp/templates/lorem/specific.jinja similarity index 100% rename from tests/testapp/templates/lorem/specific.html rename to tests/testapp/templates/lorem/specific.jinja