Просмотр исходного кода

Fixes #19999: Script list widget now displays correctly

- Extract script list content into reusable partial template
- Add object-list CSS class for proper embedded table styling
- Hide module headers and management actions in widget context
- Use compact buttons with icon-only labels for widgets
- Add test coverage for embedded parameter handling

The embedded version now renders cleanly in dashboard widgets while
preserving full functionality in the main script list page.
Jason Novinger 6 месяцев назад
Родитель
Сommit
9c6d0d1ddc

+ 18 - 0
netbox/extras/tests/test_views.py

@@ -807,3 +807,21 @@ class NotificationTestCase(
 
     def test_list_objects_with_constrained_permission(self):
         return
+
+
+class ScriptListViewTest(TestCase):
+    user_permissions = ['extras.view_script']
+
+    def test_script_list_embedded_parameter(self):
+        """Test that ScriptListView accepts embedded parameter without error"""
+        url = reverse('extras:script_list')
+
+        # Test normal request
+        response = self.client.get(url)
+        self.assertEqual(response.status_code, 200)
+        self.assertTemplateUsed(response, 'extras/script_list.html')
+
+        # Test embedded request
+        response = self.client.get(url, {'embedded': 'true'})
+        self.assertEqual(response.status_code, 200)
+        self.assertTemplateUsed(response, 'extras/inc/script_list_content.html')

+ 10 - 3
netbox/extras/views.py

@@ -1282,11 +1282,18 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
         script_modules = ScriptModule.objects.restrict(request.user).prefetch_related(
             'data_source', 'data_file', 'jobs'
         )
-
-        return render(request, 'extras/script_list.html', {
+        context = {
             'model': ScriptModule,
             'script_modules': script_modules,
-        })
+        }
+
+        # Use partial template for dashboard widgets
+        template_name = 'extras/script_list.html'
+        if request.GET.get('embedded'):
+            template_name = 'extras/inc/script_list_content.html'
+            context['embedded'] = True
+
+        return render(request, template_name, context)
 
 
 class BaseScriptView(generic.ObjectView):

+ 139 - 0
netbox/templates/extras/inc/script_list_content.html

@@ -0,0 +1,139 @@
+{% load buttons %}
+{% load helpers %}
+{% load perms %}
+{% load i18n %}
+
+{# Core script list content - used by both full page and embedded views #}
+{% for module in script_modules %}
+  {% include 'inc/sync_warning.html' with object=module %}
+  <div class="card{% if embedded %} mb-3{% endif %}">
+    {% if not embedded %}
+      <h2 class="card-header" id="module{{ module.pk }}">
+        <i class="mdi mdi-file-document-outline"></i> {{ module }}
+        <div class="card-actions">
+          {% if perms.extras.edit_scriptmodule %}
+            <a href="{% url 'extras:scriptmodule_edit' pk=module.pk %}" class="btn btn-ghost-warning btn-sm">
+              <i class="mdi mdi-pencil" aria-hidden="true"></i> {% trans "Edit" %}
+            </a>
+          {% endif %}
+          {% if perms.extras.delete_scriptmodule %}
+            <a href="{% url 'extras:scriptmodule_delete' pk=module.pk %}" class="btn btn-ghost-danger btn-sm">
+              <i class="mdi mdi-trash-can-outline" aria-hidden="true"></i> {% trans "Delete" %}
+            </a>
+          {% endif %}
+        </div>
+      </h2>
+    {% endif %}
+    {% with scripts=module.ordered_scripts %}
+      {% if scripts %}
+        <table class="table table-hover scripts{% if embedded %} object-list table-sm{% endif %}">
+          <thead>
+            <tr>
+              <th>{% trans "Name" %}</th>
+              <th>{% trans "Description" %}</th>
+              <th>{% trans "Last Run" %}</th>
+              <th>{% trans "Status" %}</th>
+              <th></th>
+            </tr>
+          </thead>
+          <tbody>
+            {% for script in scripts %}
+              {% with last_job=script.get_latest_jobs|first %}
+                <tr>
+                  <td>
+                    {% if script.is_executable %}
+                      <a href="{% url 'extras:script' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a>
+                    {% else %}
+                      <a href="{% url 'extras:script_jobs' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a>
+                      <span class="text-danger">
+                        <i class="mdi mdi-alert" title="{% trans "Script is no longer present in the source file" %}"></i>
+                      </span>
+                    {% endif %}
+                  </td>
+                  <td>{{ script.python_class.description|markdown|placeholder }}</td>
+                  {% if last_job %}
+                    <td>
+                      <a href="{% url 'extras:script_result' job_pk=last_job.pk %}">{{ last_job.created|isodatetime }}</a>
+                    </td>
+                    <td>
+                      {% badge last_job.get_status_display last_job.get_status_color %}
+                    </td>
+                  {% else %}
+                    <td class="text-muted">{% trans "Never" %}</td>
+                    <td>{{ ''|placeholder }}</td>
+                  {% endif %}
+                  <td>
+                    {% if request.user|can_run:script and script.is_executable %}
+                      <div class="float-end d-print-none">
+                        <form action="{% url 'extras:script' script.pk %}" method="post">
+                          {% if script.python_class.commit_default %}
+                            <input type="checkbox" name="_commit" hidden checked>
+                          {% endif %}
+                          {% csrf_token %}
+                          <button type="submit" name="_run" class="btn btn-primary{% if embedded %} btn-sm{% endif %}">
+                            {% if last_job %}
+                              <i class="mdi mdi-replay"></i> {% if not embedded %}{% trans "Run Again" %}{% endif %}
+                            {% else %}
+                              <i class="mdi mdi-play"></i> {% if not embedded %}{% trans "Run Script" %}{% endif %}
+                            {% endif %}
+                          </button>
+                        </form>
+                      </div>
+                    {% endif %}
+                  </td>
+                </tr>
+                {% if last_job and not embedded %}
+                  {% for test_name, data in last_job.data.tests.items %}
+                    <tr>
+                      <td colspan="4" class="method">
+                        <span class="ps-3">{{ test_name }}</span>
+                      </td>
+                      <td class="text-end text-nowrap script-stats">
+                        <span class="badge text-bg-success">{{ data.success }}</span>
+                        <span class="badge text-bg-info">{{ data.info }}</span>
+                        <span class="badge text-bg-warning">{{ data.warning }}</span>
+                        <span class="badge text-bg-danger">{{ data.failure }}</span>
+                      </td>
+                    </tr>
+                  {% endfor %}
+                {% elif last_job and not last_job.data.log and not embedded %}
+                  {# legacy #}
+                  {% 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 report-stats">
+                        <span class="badge bg-success">{{ stats.success }}</span>
+                        <span class="badge bg-info">{{ stats.info }}</span>
+                        <span class="badge bg-warning">{{ stats.warning }}</span>
+                        <span class="badge bg-danger">{{ stats.failure }}</span>
+                      </td>
+                    </tr>
+                  {% endfor %}
+                {% endif %}
+              {% endwith %}
+            {% endfor %}
+          </tbody>
+        </table>
+      {% else %}
+        <div class="card-body">
+          <div class="alert alert-warning" role="alert">
+            <i class="mdi mdi-alert"></i>
+            {% blocktrans with module=module.name %}Could not load scripts from module {{ module }}{% endblocktrans %}
+          </div>
+        </div>
+      {% endif %}
+    {% endwith %}
+  </div>
+{% empty %}
+  <div class="alert alert-info" role="alert">
+    <h4 class="alert-heading">{% trans "No Scripts Found" %}</h4>
+    {% if perms.extras.add_scriptmodule and not embedded %}
+      {% url 'extras:scriptmodule_add' as create_script_url %}
+      {% blocktrans trimmed %}
+        Get started by <a href="{{ create_script_url }}">creating a script</a> from an uploaded file or data source.
+      {% endblocktrans %}
+    {% endif %}
+  </div>
+{% endfor %}

+ 1 - 131
netbox/templates/extras/script_list.html

@@ -19,135 +19,5 @@
 {% endblock controls %}
 
 {% block content %}
-  {% for module in script_modules %}
-    {% include 'inc/sync_warning.html' with object=module %}
-    <div class="card">
-      <h2 class="card-header" id="module{{ module.pk }}">
-        <i class="mdi mdi-file-document-outline"></i> {{ module }}
-        <div class="card-actions">
-          {% if perms.extras.edit_scriptmodule %}
-            <a href="{% url 'extras:scriptmodule_edit' pk=module.pk %}" class="btn btn-ghost-warning btn-sm">
-              <i class="mdi mdi-pencil" aria-hidden="true"></i> {% trans "Edit" %}
-            </a>
-          {% endif %}
-          {% if perms.extras.delete_scriptmodule %}
-            <a href="{% url 'extras:scriptmodule_delete' pk=module.pk %}" class="btn btn-ghost-danger btn-sm">
-              <i class="mdi mdi-trash-can-outline" aria-hidden="true"></i> {% trans "Delete" %}
-            </a>
-          {% endif %}
-        </div>
-      </h2>
-      {% with scripts=module.ordered_scripts %}
-        {% if scripts %}
-          <table class="table table-hover scripts">
-            <thead>
-              <tr>
-                <th>{% trans "Name" %}</th>
-                <th>{% trans "Description" %}</th>
-                <th>{% trans "Last Run" %}</th>
-                <th>{% trans "Status" %}</th>
-                <th></th>
-              </tr>
-            </thead>
-            <tbody>
-              {% for script in scripts %}
-                {% with last_job=script.get_latest_jobs|first %}
-                  <tr>
-                    <td>
-                      {% if script.is_executable %}
-                        <a href="{% url 'extras:script' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a>
-                      {% else %}
-                        <a href="{% url 'extras:script_jobs' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a>
-                        <span class="text-danger">
-                          <i class="mdi mdi-alert" title="{% trans "Script is no longer present in the source file" %}"></i>
-                        </span>
-                      {% endif %}
-                    </td>
-                    <td>{{ script.python_class.description|markdown|placeholder }}</td>
-                    {% if last_job %}
-                      <td>
-                        <a href="{% url 'extras:script_result' job_pk=last_job.pk %}">{{ last_job.created|isodatetime }}</a>
-                      </td>
-                      <td>
-                        {% badge last_job.get_status_display last_job.get_status_color %}
-                      </td>
-                    {% else %}
-                      <td class="text-muted">{% trans "Never" %}</td>
-                      <td>{{ ''|placeholder }}</td>
-                    {% endif %}
-                    <td>
-                      {% if request.user|can_run:script and script.is_executable %}
-                        <div class="float-end d-print-none">
-                          <form action="{% url 'extras:script' script.pk %}" method="post">
-                            {% if script.python_class.commit_default %}
-                              <input type="checkbox" name="_commit" hidden checked>
-                            {% endif %}
-                            {% csrf_token %}
-                            <button type="submit" name="_run" class="btn btn-primary btn-sm">
-                              {% 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>
-                  {% if last_job %}
-                    {% for test_name, data in last_job.data.tests.items %}
-                      <tr>
-                        <td colspan="4" class="method">
-                          <span class="ps-3">{{ test_name }}</span>
-                        </td>
-                        <td class="text-end text-nowrap script-stats">
-                          <span class="badge text-bg-success">{{ data.success }}</span>
-                          <span class="badge text-bg-info">{{ data.info }}</span>
-                          <span class="badge text-bg-warning">{{ data.warning }}</span>
-                          <span class="badge text-bg-danger">{{ data.failure }}</span>
-                        </td>
-                      </tr>
-                    {% endfor %}
-                  {% elif not last_job.data.log %}
-                    {# legacy #}
-                    {% 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 report-stats">
-                          <span class="badge bg-success">{{ stats.success }}</span>
-                          <span class="badge bg-info">{{ stats.info }}</span>
-                          <span class="badge bg-warning">{{ stats.warning }}</span>
-                          <span class="badge bg-danger">{{ stats.failure }}</span>
-                        </td>
-                      </tr>
-                    {% endfor %}
-                  {% endif %}
-                {% endwith %}
-              {% endfor %}
-            </tbody>
-          </table>
-        {% else %}
-          <div class="card-body">
-            <div class="alert alert-warning" role="alert">
-              <i class="mdi mdi-alert"></i>
-              {% blocktrans with module=module.name %}Could not load scripts from module {{ module }}{% endblocktrans %}
-            </div>
-          </div>
-        {% endif %}
-      {% endwith %}
-    </div>
-  {% empty %}
-    <div class="alert alert-info" role="alert">
-      <h4 class="alert-heading">{% trans "No Scripts Found" %}</h4>
-      {% if perms.extras.add_scriptmodule %}
-        {% url 'extras:scriptmodule_add' as create_script_url %}
-        {% blocktrans trimmed %}
-          Get started by <a href="{{ create_script_url }}">creating a script</a> from an uploaded file or data source.
-        {% endblocktrans %}
-      {% endif %}
-    </div>
-  {% endfor %}
+  {% include 'extras/inc/script_list_content.html' with embedded=False %}
 {% endblock content %}