From f99ed119cabf322716bb069cc6d70f97857d68c0 Mon Sep 17 00:00:00 2001 From: Martin Hoyer Date: Tue, 20 Aug 2024 13:44:35 +0200 Subject: [PATCH] Use Jinja in html_generator --- src/tmt_web/generators/html_generator.py | 178 ++---------------- .../generators/templates/_base.html.j2 | 10 + .../templates/status_callback.html.j2 | 13 ++ .../generators/templates/testandplan.html.j2 | 27 +++ .../generators/templates/testorplan.html.j2 | 24 +++ src/tmt_web/service.py | 4 +- tests/unit/test_html_generator.py | 4 +- 7 files changed, 98 insertions(+), 162 deletions(-) create mode 100644 src/tmt_web/generators/templates/_base.html.j2 create mode 100644 src/tmt_web/generators/templates/status_callback.html.j2 create mode 100644 src/tmt_web/generators/templates/testandplan.html.j2 create mode 100644 src/tmt_web/generators/templates/testorplan.html.j2 diff --git a/src/tmt_web/generators/html_generator.py b/src/tmt_web/generators/html_generator.py index b476804..c775638 100644 --- a/src/tmt_web/generators/html_generator.py +++ b/src/tmt_web/generators/html_generator.py @@ -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 File - - - - Processing... Try this clicking this url in a few seconds: {status_callback_url} - ''') - else: - return (f''' - - HTML File - - - - Status: {r.status}
- The result is:
{r.result} - ''') - +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 File - - - - name: {test.name}
- summary: {test.summary}
- description: {test.description}
- url: {full_url}
- ref: {test.fmf_id.ref}
- contact: {test.contact}
- tag: {test.tag}
- tier: {test.tier}
- id: {test.id}
- fmf-id:
- - - ''') - 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 File - - - - name: {plan.name}
- summary: {plan.summary}
- description: {plan.description}
- url: {full_url}
- ref: {plan.fmf_id.ref}
- contact: {plan.contact}
- tag: {plan.tag}
- tier: {plan.tier}
- id: {plan.id}
- fmf-id:
- - - ''') - 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 File - - - - Test metadata
- name: {test.name}
- summary: {test.summary}
- description: {test.description}
- url: {full_url_test}
- ref: {test.fmf_id.ref}
- contact: {test.contact}
- tag: {test.tag}
- tier: {test.tier}
- id: {test.id}
- fmf-id:
- -
- Plan metadata
- name: {plan.name}
- summary: {plan.summary}
- description: {plan.description}
- url: {full_url_plan}
- ref: {plan.fmf_id.ref}
- contact: {plan.contact}
- tag: {plan.tag}
- tier: {plan.tier}
- id: {plan.id}
- fmf-id:
- - - ''') - 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!") diff --git a/src/tmt_web/generators/templates/_base.html.j2 b/src/tmt_web/generators/templates/_base.html.j2 new file mode 100644 index 0000000..691161d --- /dev/null +++ b/src/tmt_web/generators/templates/_base.html.j2 @@ -0,0 +1,10 @@ + + + + {{ title | default('HTML File') }} + + + + {%- block content %}{% endblock %} + + \ No newline at end of file diff --git a/src/tmt_web/generators/templates/status_callback.html.j2 b/src/tmt_web/generators/templates/status_callback.html.j2 new file mode 100644 index 0000000..5dab855 --- /dev/null +++ b/src/tmt_web/generators/templates/status_callback.html.j2 @@ -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: {{ status_callback_url }} + {%- elif status == "RETRYING" %} + Task is retrying... Please wait for a few seconds and try again: {{ status_callback_url }} + {%- else %} + Status: {{ status }}
+ The result is:
{{ result }} + {%- endif %} +{%- endblock %} diff --git a/src/tmt_web/generators/templates/testandplan.html.j2 b/src/tmt_web/generators/templates/testandplan.html.j2 new file mode 100644 index 0000000..bb4256c --- /dev/null +++ b/src/tmt_web/generators/templates/testandplan.html.j2 @@ -0,0 +1,27 @@ + +{% extends '_base.html.j2' %} +{% set title = "Test and Plan Information" %} + +{% block content %} +

Test Information

+

name: {{ test.name }}

+

summary: {{ test.summary }}

+

description: {{ test.description }}

+

url: {{ test.web_link() }}

+

ref: {{ test.fmf_id.ref }}

+

contact: {{ test.contact }}

+

tag: {{ test.tag }}

+

tier: {{ test.tier }}

+

id: {{ test.id }}

+ +

Plan Information

+

name: {{ plan.name }}

+

summary: {{ plan.summary }}

+

description: {{ plan.description }}

+

url: {{ plan.web_link() }}

+

ref: {{ plan.fmf_id.ref }}

+

contact: {{ plan.contact }}

+

tag: {{ plan.tag }}

+

tier: {{ plan.tier }}

+

id: {{ plan.id }}

+{%- endblock %} diff --git a/src/tmt_web/generators/templates/testorplan.html.j2 b/src/tmt_web/generators/templates/testorplan.html.j2 new file mode 100644 index 0000000..8d885f8 --- /dev/null +++ b/src/tmt_web/generators/templates/testorplan.html.j2 @@ -0,0 +1,24 @@ + +{% extends '_base.html.j2' %} +{% set title = testorplan.name %} + +{% block content %} +

name: {{ testorplan.name }}

+

summary: {{ testorplan.summary }}

+

description: {{ testorplan.description }}

+

url: {{ testorplan.web_link() }}

+

ref: {{ testorplan.fmf_id.ref }}

+

contact: {{ testorplan.contact }}

+

tag: {{ testorplan.tag }}

+

tier: {{ testorplan.tier }}

+

id: {{ testorplan.id }}

+ fmf-id:
+ +{%- endblock %} diff --git a/src/tmt_web/service.py b/src/tmt_web/service.py index 05ec207..3652d65 100644 --- a/src/tmt_web/service.py +++ b/src/tmt_web/service.py @@ -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": @@ -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": diff --git a/tests/unit/test_html_generator.py b/tests/unit/test_html_generator.py index 757e672..cd1beb1 100644 --- a/tests/unit/test_html_generator.py +++ b/tests/unit/test_html_generator.py @@ -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
' in data + data = html_generator.generate_html_page(test, logger) + assert 'name: /tests/objects/sample_test

' in data