Skip to content

Commit ca1ced5

Browse files
author
Bryan Sieber
committed
Migrate to jinja2
1 parent e1ba4cb commit ca1ced5

File tree

5 files changed

+171
-105
lines changed

5 files changed

+171
-105
lines changed

poetry.lock

Lines changed: 69 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ atlassian-python-api = "^3.20.1"
1717
dockerflow = "2022.1.0"
1818
PyYAML = "^6.0"
1919
types-PyYAML = "^6.0.4"
20+
Jinja2 = "^3.0.3"
2021

2122

2223
[tool.poetry.dev-dependencies]

src/jbi/router.py

Lines changed: 14 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import logging
2-
from typing import Dict, Optional
2+
from typing import Optional
33

44
from fastapi import APIRouter, Depends, Request
55
from fastapi.responses import HTMLResponse
6+
from fastapi.staticfiles import StaticFiles
7+
from fastapi.templating import Jinja2Templates
68

79
from src.app import environment
810
from src.jbi import configuration
911

12+
templates = Jinja2Templates(directory="src/templates")
13+
1014
api_router = APIRouter(tags=["JBI"])
15+
api_router.mount("/static", StaticFiles(directory="src/static"), name="static")
1116

1217
jbi_logger = logging.getLogger("src.jbi")
1318

@@ -54,108 +59,13 @@ def get_actions_by_type(action_type: Optional[str] = None):
5459

5560

5661
@api_router.get("/powered_by_jbi", response_class=HTMLResponse)
57-
def powered_by_jbi(enabled: Optional[bool] = None):
62+
def powered_by_jbi(request: Request, enabled: Optional[bool] = None):
5863
data = configuration.get_yaml_configurations()
59-
num_configs = len(data)
60-
html = f"""
61-
<html>
62-
<head>
63-
<title>Powered by JBI</title>
64-
<style>{get_css_style()}
65-
</style>
66-
</head>
67-
<body>
68-
<h1>Powered by JBI</h1>
69-
<p>{num_configs} collections.</p>
70-
<div id="collections"> {create_inner_html_table(data=data, enable_query=enabled)} </div>
71-
</body>
72-
</html>
73-
"""
74-
return html
75-
76-
77-
def create_inner_html_table(data: Dict, enable_query: Optional[bool]):
78-
agg_table = ""
79-
header = """
80-
<tr>
81-
<th>Identifier</th>
82-
<th>Action</th>
83-
<th>Contact</th>
84-
<th>Description </th>
85-
<th>Enabled</th>
86-
<th>Parameters</th>
87-
</tr>
88-
"""
89-
for key, value in data.items():
90-
enabled = value.get("enabled")
91-
if enable_query is None or enabled is enable_query:
92-
parameters = ", ".join(f"{k}={v}" for k, v in value["parameters"].items())
93-
per_row = f"""
94-
<tr>
95-
<td class="identifier">{key}</td>
96-
<td class="action">{value.get("action")}</td>
97-
<td class="contact">{value.get("contact")}</td>
98-
<td class="description">{value.get("description")}</td>
99-
<td class="enabled">{enabled}</td>
100-
<td class="parameters">{parameters}</td>
101-
</tr>"""
102-
agg_table += per_row
103-
104-
html = f"""
105-
<table>
106-
<thead>{header}</thead>
107-
<tbody class="list">{agg_table}</tbody>
108-
</table>
109-
"""
110-
return html
111-
112-
113-
def get_css_style():
114-
return """body {
115-
font-family: sans-serif;
116-
}
117-
118-
table {
119-
border-collapse: collapse;
120-
margin: 25px 0;
121-
font-size: 0.9em;
122-
min-width: 400px;
123-
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
124-
}
125-
126-
thead tr {
127-
background-color: #009879;
128-
color: #ffffff;
129-
text-align: left;
130-
}
131-
132-
th, td {
133-
padding: 12px 15px;
134-
}
135-
136-
tbody tr {
137-
border-bottom: 1px solid #dddddd;
138-
}
139-
140-
tbody tr:nth-of-type(even) {
141-
background-color: #f3f3f3;
142-
}
143-
144-
tbody tr:last-of-type {
145-
border-bottom: 2px solid #009879;
146-
}
147-
148-
.sort:after {
149-
content: "▼▲";
150-
padding-left: 10px;
151-
opacity: 0.5;
152-
}
153-
.sort.desc:after {
154-
content: "▲";
155-
opacity: 1;
156-
}
157-
.sort.asc:after {
158-
content: "▼";
159-
opacity: 1;
64+
context = {
65+
"request": request,
66+
"title": "Powered by JBI",
67+
"num_configs": len(data),
68+
"data": data,
69+
"enable_query": enabled,
16070
}
161-
"""
71+
return templates.TemplateResponse("powered_by_template.html", context)

src/static/styles.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
body {
2+
font-family: sans-serif;
3+
}
4+
5+
table {
6+
border-collapse: collapse;
7+
margin: 25px 0;
8+
font-size: 0.9em;
9+
min-width: 400px;
10+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
11+
}
12+
13+
thead tr {
14+
background-color: #009879;
15+
color: #ffffff;
16+
text-align: left;
17+
}
18+
19+
th, td {
20+
padding: 12px 15px;
21+
}
22+
23+
tbody tr {
24+
border-bottom: 1px solid #dddddd;
25+
}
26+
27+
tbody tr:nth-of-type(even) {
28+
background-color: #f3f3f3;
29+
}
30+
31+
tbody tr:last-of-type {
32+
border-bottom: 2px solid #009879;
33+
}
34+
35+
.sort:after {
36+
content: "▼▲";
37+
padding-left: 10px;
38+
opacity: 0.5;
39+
}
40+
.sort.desc:after {
41+
content: "▲";
42+
opacity: 1;
43+
}
44+
.sort.asc:after {
45+
content: "▼";
46+
opacity: 1;
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<html>
2+
<head>
3+
<title>{{title}}</title>
4+
<link rel="stylesheet" type="text/css" href="static/styles.css">
5+
</head>
6+
<body>
7+
<h1>{{title}}</h1>
8+
<p>{{num_configs}} collections.</p>
9+
<div id="collections">
10+
<table>
11+
<thead>
12+
<tr>
13+
<th>Identifier</th>
14+
<th>Action</th>
15+
<th>Contact</th>
16+
<th>Description </th>
17+
<th>Enabled</th>
18+
<th>Parameters</th>
19+
</tr>
20+
</thead>
21+
<tbody class="list">
22+
{% for key, value in data.items() %}
23+
{% if enable_query is none or value.get("enabled") is enable_query %}
24+
<tr>
25+
<td class="identifier">{{key}}</td>
26+
<td class="action">{{value.get("action")}}</td>
27+
<td class="contact">{{value.get("contact")}}</td>
28+
<td class="description">{{value.get("description")}}</td>
29+
<td class="enabled">{{value.get("enabled")}}</td>
30+
<td class="parameters">{% for key, value in value["parameters"].items() %}{{key ~ ": " ~ value}}{{ ", " if not loop.last else "" }}{% endfor %}
31+
</td>
32+
</tr>
33+
{% endif %}
34+
{% endfor %}
35+
</tbody>
36+
</table>
37+
</div>
38+
39+
</body>
40+
</html>

0 commit comments

Comments
 (0)