mirror of
https://github.com/mikeckennedy/fastapi-chameleon.git
synced 2025-08-26 11:21:36 +08:00
Allow decorator without parentheses
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Optional
|
from typing import Optional, Union, Callable
|
||||||
|
|
||||||
import fastapi
|
import fastapi
|
||||||
from chameleon import PageTemplateLoader, PageTemplate
|
from chameleon import PageTemplateLoader, PageTemplate
|
||||||
@ -49,7 +49,7 @@ def response(template_file: str, mimetype='text/html', status_code=200, **templa
|
|||||||
return fastapi.Response(content=html, media_type=mimetype, status_code=status_code)
|
return fastapi.Response(content=html, media_type=mimetype, status_code=status_code)
|
||||||
|
|
||||||
|
|
||||||
def template(template_file: Optional[str] = None, mimetype: str = 'text/html'):
|
def template(template_file: Optional[Union[Callable, str]] = None, mimetype: str = 'text/html'):
|
||||||
"""
|
"""
|
||||||
Decorate a FastAPI view method to render an HTML response.
|
Decorate a FastAPI view method to render an HTML response.
|
||||||
|
|
||||||
@ -58,6 +58,11 @@ def template(template_file: Optional[str] = None, mimetype: str = 'text/html'):
|
|||||||
:return: Decorator to be consumed by FastAPI
|
:return: Decorator to be consumed by FastAPI
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
wrapped_function = None
|
||||||
|
if callable(template_file):
|
||||||
|
wrapped_function = template_file
|
||||||
|
template_file = None
|
||||||
|
|
||||||
def response_inner(f):
|
def response_inner(f):
|
||||||
nonlocal template_file
|
nonlocal template_file
|
||||||
global template_path
|
global template_path
|
||||||
@ -92,7 +97,7 @@ def template(template_file: Optional[str] = None, mimetype: str = 'text/html'):
|
|||||||
else:
|
else:
|
||||||
return sync_view_method
|
return sync_view_method
|
||||||
|
|
||||||
return response_inner
|
return response_inner(wrapped_function) if wrapped_function else response_inner
|
||||||
|
|
||||||
|
|
||||||
def __render_response(template_file, response_val, mimetype):
|
def __render_response(template_file, response_val, mimetype):
|
||||||
|
@ -42,6 +42,18 @@ def test_default_template_name_pt():
|
|||||||
assert '<h1>Hello default WORLD!</h1>' in html
|
assert '<h1>Hello default WORLD!</h1>' in html
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_template_name_no_parentheses():
|
||||||
|
@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_default_template_name_html():
|
def test_default_template_name_html():
|
||||||
@fc.template()
|
@fc.template()
|
||||||
def details(a, b, c):
|
def details(a, b, c):
|
||||||
|
Reference in New Issue
Block a user