-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathtemplate_render.py
More file actions
32 lines (25 loc) · 1.09 KB
/
template_render.py
File metadata and controls
32 lines (25 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import curlify2
from jinja2 import Environment, FileSystemLoader, PackageLoader
def render(template_path, context, is_external=False):
"""Controller function that handles the Jinja2 rending of the template."""
loader = _loader(is_external)
env = Environment(loader=loader, autoescape=True)
env.filters["curlify"] = curlify2.to_curl
env.filters["render_body"] = render_body
env.globals["is_bytes"] = lambda o: isinstance(o, bytes)
chosen_template = env.get_template(template_path)
return chosen_template.render(**context)
def _loader(is_external):
"""
Private function that either returns Jinja2 FileSystemLoader or the
PackageLoader.
"""
if is_external:
return FileSystemLoader(searchpath="./")
return PackageLoader("scanapi", "templates")
def render_body(request):
"""Render body according to its request content type."""
content_type = request.headers.get("Content-Type")
if content_type in ["application/json", "text/plain"]:
return request.body.decode()
return f"Can not render. Unsuported content type: {content_type}."