mirror of
https://github.com/mikeckennedy/fastapi-chameleon.git
synced 2026-03-13 10:32:00 +08:00
Adds default template select (templates/module/method.pt) when not specified.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""fastapi-chameleon - Adds integration of the Chameleon template language to FastAPI."""
|
||||
|
||||
__version__ = '0.1.1'
|
||||
__version__ = '0.1.2'
|
||||
__author__ = 'Michael Kennedy <michael@talkpython.fm>'
|
||||
__all__ = ['template', 'global_init']
|
||||
|
||||
|
||||
@@ -49,18 +49,24 @@ def response(template_file: str, mimetype='text/html', status_code=200, **templa
|
||||
return fastapi.Response(content=html, media_type=mimetype, status_code=status_code)
|
||||
|
||||
|
||||
def template(template_file: str, mimetype: str = 'text/html'):
|
||||
def template(template_file: Optional[str] = None, mimetype: str = 'text/html'):
|
||||
"""
|
||||
Decorate a FastAPI view method to render an HTML response.
|
||||
|
||||
:param str template_file: The Chameleon template file (path relative to template folder, *.pt).
|
||||
:param str template_file: Optional, the Chameleon template file (path relative to template folder, *.pt).
|
||||
:param str mimetype: The mimetype response (defaults to text/html).
|
||||
:return: Decorator to be consumed by FastAPI
|
||||
"""
|
||||
if not template_file:
|
||||
raise FastAPIChameleonException("You must specify a template file.")
|
||||
|
||||
def response_inner(f):
|
||||
nonlocal template_file
|
||||
|
||||
if not template_file:
|
||||
# Use the default naming scheme: template_folder/module_name/function_name.pt
|
||||
module = f.__module__
|
||||
view = f.__name__
|
||||
template_file = f'{module}/{view}.pt'
|
||||
|
||||
@wraps(f)
|
||||
def sync_view_method(*args, **kwargs):
|
||||
response_val = f(*args, **kwargs)
|
||||
@@ -80,7 +86,7 @@ def template(template_file: str, mimetype: str = 'text/html'):
|
||||
|
||||
|
||||
def __render_response(template_file, response_val, mimetype):
|
||||
# sourcery skip: assign-if-exp
|
||||
# source skip: assign-if-exp
|
||||
if isinstance(response_val, fastapi.Response):
|
||||
return response_val
|
||||
|
||||
|
||||
12
tests/templates/test_render/index.pt
Normal file
12
tests/templates/test_render/index.pt
Normal file
@@ -0,0 +1,12 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>Hello default ${world}!</h1>
|
||||
<table>
|
||||
<tr tal:repeat="row 'apple', 'banana', 'pineapple'">
|
||||
<td tal:repeat="col 'juice', 'muffin', 'pie'">
|
||||
${row.capitalize()} ${col}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -6,6 +6,7 @@ import fastapi
|
||||
import pytest
|
||||
|
||||
import fastapi_chameleon as fc
|
||||
from fastapi_chameleon.exceptions import FastAPIChameleonException
|
||||
|
||||
here = os.path.dirname(__file__)
|
||||
folder = os.path.join(here, 'templates')
|
||||
@@ -20,6 +21,27 @@ def test_cannot_decorate_missing_template():
|
||||
view_method()
|
||||
|
||||
|
||||
def test_requires_template_for_default_name():
|
||||
with pytest.raises(ValueError):
|
||||
@fc.template(None)
|
||||
def view_method():
|
||||
return {}
|
||||
|
||||
view_method()
|
||||
|
||||
|
||||
def test_default_template_name():
|
||||
@fc.template()
|
||||
def index(a, b, c):
|
||||
return {'a': a, 'b': b, 'c': c, 'world': 'WORLD'}
|
||||
|
||||
resp = index(1, 2, 3)
|
||||
assert isinstance(resp, fastapi.Response)
|
||||
assert resp.status_code == 200
|
||||
html = resp.body.decode('utf-8')
|
||||
assert '<h1>Hello default WORLD!</h1>' in html
|
||||
|
||||
|
||||
def test_can_decorate_dict_sync_method():
|
||||
@fc.template('home/index.pt')
|
||||
def view_method(a, b, c):
|
||||
|
||||
Reference in New Issue
Block a user