From 872691a138486c80fe5589c68314dacc4ec46c25 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 20 Dec 2021 20:45:32 -0500 Subject: [PATCH] Convert scripts to use HTMX --- netbox/extras/views.py | 11 +++ .../templates/extras/htmx/script_result.html | 52 +++++++++++ netbox/templates/extras/script_result.html | 86 +++---------------- 3 files changed, 73 insertions(+), 76 deletions(-) create mode 100644 netbox/templates/extras/htmx/script_result.html diff --git a/netbox/extras/views.py b/netbox/extras/views.py index ab9e3ba52..f2cf1b38a 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -10,6 +10,7 @@ from rq import Worker from netbox.views import generic from utilities.forms import ConfirmationForm +from utilities.htmx import is_htmx from utilities.tables import paginate_table from utilities.utils import copy_safe_request, count_related, normalize_querydict, shallow_compare_dict from utilities.views import ContentTypePermissionRequiredMixin @@ -820,6 +821,16 @@ class ScriptResultView(ContentTypePermissionRequiredMixin, GetScriptMixin, View) script = self._get_script(result.name) + # If this is an HTMX request, return only the rendered table HTML + if is_htmx(request): + response = render(request, 'extras/htmx/script_result.html', { + 'script': script, + 'result': result, + }) + if result.completed: + response.status_code = 286 + return response + return render(request, 'extras/script_result.html', { 'script': script, 'result': result, diff --git a/netbox/templates/extras/htmx/script_result.html b/netbox/templates/extras/htmx/script_result.html new file mode 100644 index 000000000..013b4c81d --- /dev/null +++ b/netbox/templates/extras/htmx/script_result.html @@ -0,0 +1,52 @@ +{% load helpers %} +{% load log_levels %} + +{% if result.completed %} +

+ Run: {{ result.created|annotated_date }} + {% if result.completed %} + Duration: {{ result.duration }} + {% endif %} +

+
+
Script Log
+
+ + + + + + + {% for log in result.data.log %} + + + + + + {% empty %} + + + + {% endfor %} +
LineLevelMessage
{{ forloop.counter }}{% log_level log.status %}{{ log.message|render_markdown }}
+ No log output +
+
+ {% if execution_time %} + + {% endif %} +
+

Output

+ {% if result.data.output %} +
{{ result.data.output }}
+ {% else %} +

None

+ {% endif %} +{% else %} +
+ Loading... +
+

Results pending...

+{% endif %} diff --git a/netbox/templates/extras/script_result.html b/netbox/templates/extras/script_result.html index 3cbd0c611..f00b3bf91 100644 --- a/netbox/templates/extras/script_result.html +++ b/netbox/templates/extras/script_result.html @@ -4,16 +4,10 @@ {% load log_levels %} {% load static %} -{% block head %} - -{% endblock %} - {% block title %}{{ script }}{% endblock %} {% block subtitle %} {{ script.Meta.description|render_markdown }} - {% include 'extras/inc/job_label.html' with result=result %} {% endblock %} {% block header %} @@ -37,81 +31,21 @@ -
-

- Run: {{ result.created|annotated_date }} - {% if result.completed %} - Duration: {{ result.duration }} - {% else %} -

- Loading... -
- {% endif %} -

-
- {% if result.completed %} -
-
-
-
- Script Log -
-
- - - - - - - {% for log in result.data.log %} - - - - - - {% empty %} - - - - {% endfor %} -
LineLevelMessage
{{ forloop.counter }}{% log_level log.status %}{{ log.message|render_markdown }}
- No log output -
-
- {% if execution_time %} - - {% endif %} -
-
-
- {% else %} -
-
-
Pending Results
-
-
- {% endif %} -
-
-
{{ result.data.output }}
-
-
-

{{ script.filename }}

-
{{ script.source }}
+
+
+
+ {% include 'extras/htmx/script_result.html' %} +
+
+
+

{{ script.filename }}

+
{{ script.source }}
+
{% endblock content-wrapper %} - -{% block data %} - - -{% endblock %}