Skip to content

Commit

Permalink
Use Jinja in html_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhoyer committed Aug 20, 2024
1 parent 9511da8 commit f99ed11
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 162 deletions.
178 changes: 20 additions & 158 deletions src/tmt_web/generators/html_generator.py
Original file line number Diff line number Diff line change
@@ -1,170 +1,32 @@
import tmt
from pathlib import Path

from celery.result import AsyncResult
from jinja2 import Environment, FileSystemLoader
from tmt import Logger, Plan, Test

templ_dir = Path(__file__).resolve().parent / 'templates'

def generate_status_callback(r: AsyncResult, status_callback_url: str) -> str: # type: ignore [type-arg]
"""
This function generates the status callback for the HTML file
:param r: AsyncResult object
:param status_callback_url: URL for the status callback
:return:
"""
if r.status == "PENDING":
return (f'''<html>
<head>
<title>HTML File</title>
<meta charset="UTF-8">
</head>
<body>
Processing... Try this clicking this url in a few seconds: <a href="{status_callback_url}">{status_callback_url}
</a> </body>''')
else:
return (f'''<html>
<head>
<title>HTML File</title>
<meta charset="UTF-8">
</head>
<body>
Status: {r.status}<br>
The result is: <br>{r.result}
</body>''')

env = Environment(loader=FileSystemLoader(str(templ_dir)), autoescape=True)

def generate_test_html_page(test: Test, logger: Logger) -> str:
"""
This function generates an HTML file with the input data for a test
:param test: Test object
:param logger: tmt.Logger instance
:return:
"""
logger.print("Generating the HTML file...")
full_url = test.web_link()
# Adding the input data to the HTML file
file_html = (f'''<html>
<head>
<title>HTML File</title>
<meta charset="UTF-8">
</head>
<body>
name: {test.name}<br>
summary: {test.summary}<br>
description: {test.description}<br>
url: <a href=\"{full_url}\" target=\"_blank\">{full_url}</a><br>
ref: {test.fmf_id.ref}<br>
contact: {test.contact}<br>
tag: {test.tag}<br>
tier: {test.tier}<br>
id: {test.id}<br>
fmf-id:<br>
<ul>
<li>url: {test.fmf_id.url}</li>
<li>path: {test.fmf_id.path.as_posix() if test.fmf_id.path is not None else None}</li>
<li>name: {test.fmf_id.name}</li>
<li>ref: {test.fmf_id.ref}</li>
</ul>
</body>
</html>''')
logger.print("HTML file generated successfully!", color="green")
return file_html
def render_template(template_name: str, **kwargs) -> str:
template = env.get_template(template_name)
return template.render(**kwargs)

def generate_status_callback(r: AsyncResult, status_callback_url: str) -> str:
data = {
"status": r.status,
"status_callback_url": status_callback_url,
"result": r.result
}
return render_template("status_callback.html.j2", **data)

def generate_plan_html_page(plan: Plan, logger: Logger) -> str:
"""
This function generates an HTML file with the input data for a plan
:param plan: Plan object
:param logger: tmt.Logger instance
:return:
"""
def generate_html_page(obj: Test | Plan, logger: Logger) -> str:
logger.print("Generating the HTML file...")
full_url = plan.web_link()
# Adding the input data to the HTML file
file_html = (f'''<html>
<head>
<title>HTML File</title>
<meta charset="UTF-8">
</head>
<body>
name: {plan.name}<br>
summary: {plan.summary}<br>
description: {plan.description}<br>
url: <a href=\"{full_url}\" target=\"_blank\">{full_url}</a><br>
ref: {plan.fmf_id.ref}<br>
contact: {plan.contact}<br>
tag: {plan.tag}<br>
tier: {plan.tier}<br>
id: {plan.id}<br>
fmf-id:<br>
<ul>
<li>url: {plan.fmf_id.url}</li>
<li>path: {plan.fmf_id.path.as_posix() if plan.fmf_id.path is not None else None}</li>
<li>name: {plan.fmf_id.name}</li>
<li>ref: {plan.fmf_id.ref}</li>
</ul>
</body>
</html>''')
logger.print("HTML file generated successfully!", color="green")
return file_html
return render_template('testorplan.html.j2', testorplan=obj)


def generate_testplan_html_page(test: tmt.Test, plan: tmt.Plan, logger: Logger) -> str:
"""
This function generates an HTML file with the input data for a test and a plan
:param test: Test object
:param plan: Plan object
:param logger: tmt.Logger instance
:return:
"""
def generate_testplan_html_page(test: Test, plan: Plan, logger: Logger) -> str:
logger.print("Generating the HTML file...")
full_url_test = test.web_link()
full_url_plan = plan.web_link()
# Adding the input data to the HTML file
file_html = (f'''<html>
<head>
<title>HTML File</title>
<meta charset="UTF-8">
</head>
<body>
Test metadata<br>
name: {test.name}<br>
summary: {test.summary}<br>
description: {test.description}<br>
url: <a href=\"{full_url_test}\" target=\"_blank\">{full_url_test}</a><br>
ref: {test.fmf_id.ref}<br>
contact: {test.contact}<br>
tag: {test.tag}<br>
tier: {test.tier}<br>
id: {test.id}<br>
fmf-id:<br>
<ul>
<li>url: {test.fmf_id.url}</li>
<li>path: {test.fmf_id.path.as_posix() if test.fmf_id.path is not None else None}</li>
<li>name: {test.fmf_id.name}</li>
<li>ref: {test.fmf_id.ref}</li>
</ul>
<br>
Plan metadata<br>
name: {plan.name}<br>
summary: {plan.summary}<br>
description: {plan.description}<br>
url: <a href=\"{full_url_plan}\" target=\"_blank\">{full_url_plan}</a><br>
ref: {plan.fmf_id.ref}<br>
contact: {plan.contact}<br>
tag: {plan.tag}<br>
tier: {plan.tier}<br>
id: {plan.id}<br>
fmf-id:<br>
<ul>
<li>url: {plan.fmf_id.url}</li>
<li>path: {plan.fmf_id.path.as_posix() if plan.fmf_id.path is not None else None}</li>
<li>name: {plan.fmf_id.name}</li>
<li>ref: {plan.fmf_id.ref}</li>
</ul>
</body>
</html>''')
logger.print("HTML file generated successfully!", color="green")
return file_html

return render_template('testandplan.html.j2', test=test, plan=plan)

if __name__ == "__main__":
print("This is not executable file!")
print("This is not an executable file!")
10 changes: 10 additions & 0 deletions src/tmt_web/generators/templates/_base.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- _base.html.j2 -->
<html>
<head>
<title>{{ title | default('HTML File') }}</title>
<meta charset="UTF-8">
</head>
<body>
{%- block content %}{% endblock %}
</body>
</html>
13 changes: 13 additions & 0 deletions src/tmt_web/generators/templates/status_callback.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends '_base.html.j2' %}
{% set title = "Status" %}

{% block content %}
{% if status == "PENDING" %}
Processing... Try this clicking this url in a few seconds: <a href="{{ status_callback_url }}">{{ status_callback_url }}</a>
{%- elif status == "RETRYING" %}
Task is retrying... Please wait for a few seconds and try again: <a href="{{ status_callback_url }}">{{ status_callback_url }}</a>
{%- else %}
Status: {{ status }}<br>
The result is: <br>{{ result }}
{%- endif %}
{%- endblock %}
27 changes: 27 additions & 0 deletions src/tmt_web/generators/templates/testandplan.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- test_and_plan.html.j2 -->
{% extends '_base.html.j2' %}
{% set title = "Test and Plan Information" %}

{% block content %}
<h1>Test Information</h1>
<p>name: {{ test.name }}</p>
<p>summary: {{ test.summary }}</p>
<p>description: {{ test.description }}</p>
<p>url: <a href="{{ test.web_link() }}" target="_blank">{{ test.web_link() }}</a></p>
<p>ref: {{ test.fmf_id.ref }}</p>
<p>contact: {{ test.contact }}</p>
<p>tag: {{ test.tag }}</p>
<p>tier: {{ test.tier }}</p>
<p>id: {{ test.id }}</p>

<h1>Plan Information</h1>
<p>name: {{ plan.name }}</p>
<p>summary: {{ plan.summary }}</p>
<p>description: {{ plan.description }}</p>
<p>url: <a href="{{ plan.web_link() }}" target="_blank">{{ plan.web_link() }}</a></p>
<p>ref: {{ plan.fmf_id.ref }}</p>
<p>contact: {{ plan.contact }}</p>
<p>tag: {{ plan.tag }}</p>
<p>tier: {{ plan.tier }}</p>
<p>id: {{ plan.id }}</p>
{%- endblock %}
24 changes: 24 additions & 0 deletions src/tmt_web/generators/templates/testorplan.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- test_page.html.j2 -->
{% extends '_base.html.j2' %}
{% set title = testorplan.name %}

{% block content %}
<p>name: {{ testorplan.name }}</p>
<p>summary: {{ testorplan.summary }}</p>
<p>description: {{ testorplan.description }}</p>
<p>url: <a href="{{ testorplan.web_link() }}" target="_blank">{{ testorplan.web_link() }}</a></p>
<p>ref: {{ testorplan.fmf_id.ref }}</p>
<p>contact: {{ testorplan.contact }}</p>
<p>tag: {{ testorplan.tag }}</p>
<p>tier: {{ testorplan.tier }}</p>
<p>id: {{ testorplan.id }}</p>
fmf-id:<br>
<ul>
<li>url: {{ testorplan.fmf_id.url }}</li>
{%- if testorplan.fmf_id.path %}
<li>path: {{ testorplan.fmf_id.path.as_posix() }}</li>
{%- else %}
<li>path: None</li>
{%- endif %}
</ul>
{%- endblock %}
4 changes: 2 additions & 2 deletions src/tmt_web/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def process_test_request(test_url: str,
return wanted_test
match out_format:
case "html":
return html.generate_test_html_page(wanted_test, logger=logger)
return html.generate_html_page(wanted_test, logger=logger)
case "json":
return json_generator.generate_test_json(wanted_test, logger=logger)
case "yaml":
Expand Down Expand Up @@ -116,7 +116,7 @@ def process_plan_request(plan_url: str,
return wanted_plan
match out_format:
case "html":
return html.generate_plan_html_page(wanted_plan, logger=logger)
return html.generate_html_page(wanted_plan, logger=logger)
case "json":
return json_generator.generate_plan_json(wanted_plan, logger=logger)
case "yaml":
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_html_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class TestHtmlGenerator:
def test_generate_test_html(self):
logger = tmt.Logger(logging.getLogger("tmt-logger"))
test = tmt.Tree(logger=logger).tests(names=["/tests/objects/sample_test"])[0]
data = html_generator.generate_test_html_page(test, logger)
assert 'name: /tests/objects/sample_test<br>' in data
data = html_generator.generate_html_page(test, logger)
assert 'name: /tests/objects/sample_test</p>' in data

0 comments on commit f99ed11

Please sign in to comment.