-
Notifications
You must be signed in to change notification settings - Fork 45
/
templating.py
48 lines (34 loc) · 1.71 KB
/
templating.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import html
import textwrap
from os import path
from typing import Any, Dict, List
import markdown
from fastapi.templating import Jinja2Templates
TEMPLATES_DIR = path.join(path.dirname(__file__), "..", "templates")
templates = Jinja2Templates(directory=TEMPLATES_DIR)
TEMPLATE_TASK_TABLE_ROW_TASK_LINK = templates.get_template("table_row/task/task_link.jinja2")
TEMPLATE_TASK_TABLE_ROW_BADGES = templates.get_template("table_row/task/badges.jinja2")
TEMPLATE_ANALYSIS_TABLE_ROW_PENDING_TASKS = templates.get_template("table_row/analysis/pending_tasks.jinja2")
TEMPLATE_ANALYSIS_TABLE_ROW_ACTIONS = templates.get_template("table_row/analysis/actions.jinja2")
def dedent(text: str) -> str:
return textwrap.dedent(text)
def render_markdown(markdown_text: str) -> str:
return markdown.markdown(markdown_text)
templates.env.filters["dedent"] = dedent
templates.env.filters["render_markdown"] = render_markdown
def render_task_table_row(task_result: Dict[str, Any]) -> List[str]:
return [
html.escape(task_result["created_at"].strftime("%Y-%m-%d %H:%M:%S")) if "created_at" in task_result else None,
html.escape(task_result["tag"] or ""),
html.escape(task_result["receiver"]),
TEMPLATE_TASK_TABLE_ROW_TASK_LINK.render({"task_result": task_result}),
TEMPLATE_TASK_TABLE_ROW_BADGES.render({"task_result": task_result}),
html.escape(task_result.get("status_reason") or ""),
]
def render_analyses_table_row(entry: Dict[str, Any]) -> List[str]:
return [
html.escape(entry["target"]),
html.escape(entry["tag"] or ""),
TEMPLATE_ANALYSIS_TABLE_ROW_PENDING_TASKS.render({"entry": entry}),
TEMPLATE_ANALYSIS_TABLE_ROW_ACTIONS.render({"entry": entry}),
]