12510 update templates

This commit is contained in:
Arthur 2024-01-26 10:36:34 -08:00
parent ab14a8a71c
commit 8a990d7372
3 changed files with 121 additions and 68 deletions

View File

@ -273,7 +273,7 @@ class BaseScript(object):
def __init__(self): def __init__(self):
self._results = {} self._results = {}
self.failed = False self.failed = False
self._current_method = 'main' self._current_method = 'total'
# Initiate the log # Initiate the log
self.logger = logging.getLogger(f"netbox.scripts.{self.__module__}.{self.__class__.__name__}") self.logger = logging.getLogger(f"netbox.scripts.{self.__module__}.{self.__class__.__name__}")
@ -283,7 +283,7 @@ class BaseScript(object):
self.request = None self.request = None
# Compile test methods and initialize results skeleton # Compile test methods and initialize results skeleton
self._results['main'] = { self._results['total'] = {
'success': 0, 'success': 0,
'info': 0, 'info': 0,
'warning': 0, 'warning': 0,
@ -454,8 +454,8 @@ class BaseScript(object):
if log_level != LogLevelChoices.LOG_DEFAULT: if log_level != LogLevelChoices.LOG_DEFAULT:
self._results[self._current_method][log_level] += 1 self._results[self._current_method][log_level] += 1
if self._current_method != 'main': if self._current_method != 'total':
self._results['main'][log_level] += 1 self._results['total'][log_level] += 1
if obj: if obj:
self.logger.log(level, f"{log_level.capitalize()} | {obj}: {message}") self.logger.log(level, f"{log_level.capitalize()} | {obj}: {message}")

View File

@ -1,6 +1,5 @@
{% load humanize %} {% load humanize %}
{% load helpers %} {% load helpers %}
{% load log_levels %}
{% load i18n %} {% load i18n %}
<p> <p>
@ -17,40 +16,62 @@
<span id="pending-result-label">{% badge job.get_status_display job.get_status_color %}</span> <span id="pending-result-label">{% badge job.get_status_display job.get_status_color %}</span>
</p> </p>
{% if job.completed %} {% if job.completed %}
<div class="card mb-3"> <div class="card">
<h5 class="card-header">{% trans "Script Log" %}</h5> <h5 class="card-header">{% trans "Script Methods" %}</h5>
<table class="table table-hover"> <table class="table table-hover">
<tr> {% for method, data in job.data.items %}
<th>{% trans "Line" %}</th>
<th>{% trans "Level" %}</th>
<th>{% trans "Message" %}</th>
</tr>
{% for log in job.data.log %}
<tr> <tr>
<td>{{ forloop.counter }}</td> <td class="font-monospace"><a href="#{{ method }}">{{ method }}</a></td>
<td>{% log_level log.status %}</td> <td class="text-end script-stats">
<td class="rendered-markdown">{{ log.message|markdown }}</td> <span class="badge text-bg-success">{{ data.success }}</span>
</tr> <span class="badge text-bg-info">{{ data.info }}</span>
{% empty %} <span class="badge text-bg-warning">{{ data.warning }}</span>
<tr> <span class="badge text-bg-danger">{{ data.failure }}</span>
<td colspan="3" class="text-center text-muted">
{% trans "No log output" %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
{% if execution_time %}
<div class="card-footer text-end text-muted">
<small>{% trans "Exec Time" %}: {{ execution_time|floatformat:3 }} {% trans "seconds" context "Unit of time" %}</small>
</div>
{% endif %}
</div> </div>
<h4>{% trans "Output" %}</h4> <div class="card">
{% if job.data.output %} <h5 class="card-header">{% trans "Script Results" %}</h5>
<pre class="block">{{ job.data.output }}</pre> <table class="table table-hover script">
{% else %} <thead>
<p class="text-muted">{% trans "None" %}</p> <tr>
{% endif %} <th>{% trans "Time" %}</th>
<th>{% trans "Level" %}</th>
<th>{% trans "Object" %}</th>
<th>{% trans "Message" %}</th>
</tr>
</thead>
<tbody>
{% for method, data in job.data.items %}
<tr>
<th colspan="4" style="font-family: monospace">
<a name="{{ method }}"></a>{{ method }}
</th>
</tr>
{% for time, level, obj, url, message in data.log %}
<tr class="{% if level == 'failure' %}danger{% elif level %}{{ level }}{% endif %}">
<td>{{ time }}</td>
<td>
<label class="badge text-bg-{% if level == 'failure' %}danger{% else %}{{ level }}{% endif %}">{{ level|title }}</label>
</td>
<td>
{% if obj and url %}
<a href="{{ url }}">{{ obj }}</a>
{% elif obj %}
{{ obj }}
{% else %}
{{ ''|placeholder }}
{% endif %}
</td>
<td class="rendered-markdown">{{ message|markdown }}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
</div>
{% elif job.started %} {% elif job.started %}
{% include 'extras/inc/result_pending.html' %} {% include 'extras/inc/result_pending.html' %}
{% endif %} {% endif %}

View File

@ -1,14 +1,11 @@
{% extends 'generic/_base.html' %} {% extends 'generic/_base.html' %}
{% load buttons %} {% load buttons %}
{% load helpers %} {% load helpers %}
{% load perms %}
{% load i18n %} {% load i18n %}
{% block title %}{% trans "Scripts" %}{% endblock %} {% block title %}{% trans "Scripts" %}{% endblock %}
{% block controls %}
{% add_button model %}
{% endblock controls %}
{% block tabs %} {% block tabs %}
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li class="nav-item" role="presentation"> <li class="nav-item" role="presentation">
@ -17,6 +14,10 @@
</ul> </ul>
{% endblock tabs %} {% endblock tabs %}
{% block controls %}
{% add_button model %}
{% endblock controls %}
{% block content %} {% block content %}
{% for module in script_modules %} {% for module in script_modules %}
<div class="card"> <div class="card">
@ -25,65 +26,96 @@
<i class="mdi mdi-file-document-outline"></i> {{ module }} <i class="mdi mdi-file-document-outline"></i> {{ module }}
</div> </div>
{% if perms.extras.delete_scriptmodule %} {% if perms.extras.delete_scriptmodule %}
<div class="float-end"> <a href="{% url 'extras:scriptmodule_delete' pk=module.pk %}" class="btn btn-danger btn-sm">
<a href="{% url 'extras:scriptmodule_delete' pk=module.pk %}" class="btn btn-danger btn-sm"> <i class="mdi mdi-trash-can-outline" aria-hidden="true"></i> {% trans "Delete" %}
<i class="mdi mdi-trash-can-outline" aria-hidden="true"></i> {% trans "Delete" %} </a>
</a>
</div>
{% endif %} {% endif %}
</h5> </h5>
<div class="card-body"> <div class="card-body">
{% include 'inc/sync_warning.html' with object=module %} {% include 'inc/sync_warning.html' with object=module %}
{% if not module.scripts %} {% if module.scripts %}
<div class="alert alert-warning d-flex align-items-center" role="alert"> <table class="table table-hover scripts">
<i class="mdi mdi-alert"></i>
{% blocktrans trimmed with file_path=module.full_path %}
Script file at <code class="mx-1">{{ file_path }}</code> could not be loaded.
{% endblocktrans %}
</div>
{% else %}
<table class="table table-hover reports">
<thead> <thead>
<tr> <tr>
<th width="250">{% trans "Name" %}</th> <th width="250">{% trans "Name" %}</th>
<th>{% trans "Description" %}</th> <th>{% trans "Description" %}</th>
<th>{% trans "Last Run" %}</th> <th>{% trans "Last Run" %}</th>
<th class="text-end">{% trans "Status" %}</th> <th>{% trans "Status" %}</th>
<th width="120"></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% with jobs=module.get_latest_jobs %} {% with jobs=module.get_latest_jobs %}
{% for script_name, script_class in module.scripts.items %} {% for script_name, script in module.scripts.items %}
<tr> {% with last_job=jobs|get_key:script.class_name %}
<td> <tr>
<a href="{% url 'extras:script' module=module.python_name name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a> <td>
</td> <a href="{% url 'extras:script' module=module.python_name name=script.class_name %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.name }}</a>
<td> </td>
{{ script_class.Meta.description|markdown|placeholder }} <td>{{ script.description|markdown|placeholder }}</td>
</td> {% if last_job %}
{% with last_result=jobs|get_key:script_class.class_name %}
{% if last_result %}
<td> <td>
<a href="{% url 'extras:script_result' job_pk=last_result.pk %}">{{ last_result.created|annotated_date }}</a> <a href="{% url 'extras:script_result' job_pk=last_job.pk %}">{{ last_job.created|annotated_date }}</a>
</td> </td>
<td class="text-end"> <td>
{% badge last_result.get_status_display last_result.get_status_color %} {% badge last_job.get_status_display last_job.get_status_color %}
</td> </td>
{% else %} {% else %}
<td class="text-muted">{% trans "Never" %}</td> <td class="text-muted">{% trans "Never" %}</td>
<td class="text-end">{{ ''|placeholder }}</td> <td>
{% if script.is_valid %}
{{ ''|placeholder }}
{% else %}
<span class="badge text-bg-danger" title="{% trans "Script has no methods" %}">
{% trans "Invalid" %}
</span>
{% endif %}
</td>
{% endif %} {% endif %}
{% endwith %} <td>
</tr> {% if perms.extras.run_script and script.is_valid %}
<div class="float-end d-print-none">
<form action="{% url 'extras:script' module=script.module name=script.class_name %}" method="post">
{% csrf_token %}
<button type="submit" name="_run" class="btn btn-primary" style="width: 110px">
{% if last_job %}
<i class="mdi mdi-replay"></i> {% trans "Run Again" %}
{% else %}
<i class="mdi mdi-play"></i> {% trans "Run Script" %}
{% endif %}
</button>
</form>
</div>
{% endif %}
</td>
</tr>
{% for method, stats in last_job.data.items %}
<tr>
<td colspan="4" class="method">
<span class="ps-3">{{ method }}</span>
</td>
<td class="text-end text-nowrap script-stats">
<span class="badge text-bg-success">{{ stats.success }}</span>
<span class="badge text-bg-info">{{ stats.info }}</span>
<span class="badge text-bg-warning">{{ stats.warning }}</span>
<span class="badge text-bg-danger">{{ stats.failure }}</span>
</td>
</tr>
{% endfor %}
{% endwith %}
{% endfor %} {% endfor %}
{% endwith %} {% endwith %}
</tbody> </tbody>
</table> </table>
{% else %}
<div class="alert alert-warning" role="alert">
<i class="mdi mdi-alert"></i> Could not load scripts from {{ module.name }}
</div>
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% empty %} {% empty %}
<div class="alert alert-info"> <div class="alert alert-info" role="alert">
<h4 class="alert-heading">{% trans "No Scripts Found" %}</h4> <h4 class="alert-heading">{% trans "No Scripts Found" %}</h4>
{% if perms.extras.add_scriptmodule %} {% if perms.extras.add_scriptmodule %}
{% url 'extras:scriptmodule_add' as create_script_url %} {% url 'extras:scriptmodule_add' as create_script_url %}