Skip to content

Commit 9909213

Browse files
12416 warning for missing script file (#12456)
* 12416 warning for missing script file * 12416 widen exception catching for internal script error * Update netbox/extras/models/scripts.py Co-authored-by: Jeremy Stretch <[email protected]> * 12416 update from review feedback --------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent 7a38f60 commit 9909213

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

netbox/extras/models/scripts.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import logging
23
from functools import cached_property
34

45
from django.db import models
@@ -16,6 +17,8 @@
1617
'ScriptModule',
1718
)
1819

20+
logger = logging.getLogger('netbox.data_backends')
21+
1922

2023
class Script(WebhooksMixin, models.Model):
2124
"""
@@ -53,7 +56,12 @@ def _get_name(cls):
5356
# For child objects in submodules use the full import path w/o the root module as the name
5457
return cls.full_name.split(".", maxsplit=1)[1]
5558

56-
module = self.get_module()
59+
try:
60+
module = self.get_module()
61+
except Exception as e:
62+
logger.debug(f"Failed to load script: {self.python_name} error: {e}")
63+
module = None
64+
5765
scripts = {}
5866
ordered = getattr(module, 'script_order', [])
5967

netbox/extras/views.py

-1
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,6 @@ def get_required_permission(self):
10331033
return 'extras.view_script'
10341034

10351035
def get(self, request, module, name):
1036-
print(module)
10371036
module = get_object_or_404(ScriptModule.objects.restrict(request.user), file_path__startswith=module)
10381037
script = module.scripts[name]()
10391038
form = script.as_form(initial=normalize_querydict(request.GET))

netbox/templates/extras/script_list.html

+43-37
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,49 @@ <h5 class="card-header" id="module{{ module.pk }}">
3737
</h5>
3838
<div class="card-body">
3939
{% include 'inc/sync_warning.html' with object=module %}
40-
<table class="table table-hover table-headings reports">
41-
<thead>
42-
<tr>
43-
<th width="250">Name</th>
44-
<th>Description</th>
45-
<th>Last Run</th>
46-
<th class="text-end">Status</th>
47-
</tr>
48-
</thead>
49-
<tbody>
50-
{% with jobs=module.get_latest_jobs %}
51-
{% for script_name, script_class in module.scripts.items %}
52-
<tr>
53-
<td>
54-
<a href="{% url 'extras:script' module=module.python_name name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>
55-
</td>
56-
<td>
57-
{{ script_class.Meta.description|markdown|placeholder }}
58-
</td>
59-
{% with last_result=jobs|get_key:script_class.name %}
60-
{% if last_result %}
61-
<td>
62-
<a href="{% url 'extras:script_result' job_pk=last_result.pk %}">{{ last_result.created|annotated_date }}</a>
63-
</td>
64-
<td class="text-end">
65-
{% badge last_result.get_status_display last_result.get_status_color %}
66-
</td>
67-
{% else %}
68-
<td class="text-muted">Never</td>
69-
<td class="text-end">{{ ''|placeholder }}</td>
70-
{% endif %}
71-
{% endwith %}
72-
</tr>
73-
{% endfor %}
74-
{% endwith %}
75-
</tbody>
76-
</table>
40+
{% if not module.scripts %}
41+
<div class="alert alert-warning d-flex align-items-center" role="alert">
42+
<i class="mdi mdi-alert"></i>&nbsp; Script file at: {{module.full_path}} could not be loaded.
43+
</div>
44+
{% else %}
45+
<table class="table table-hover table-headings reports">
46+
<thead>
47+
<tr>
48+
<th width="250">Name</th>
49+
<th>Description</th>
50+
<th>Last Run</th>
51+
<th class="text-end">Status</th>
52+
</tr>
53+
</thead>
54+
<tbody>
55+
{% with jobs=module.get_latest_jobs %}
56+
{% for script_name, script_class in module.scripts.items %}
57+
<tr>
58+
<td>
59+
<a href="{% url 'extras:script' module=module.python_name name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>
60+
</td>
61+
<td>
62+
{{ script_class.Meta.description|markdown|placeholder }}
63+
</td>
64+
{% with last_result=jobs|get_key:script_class.name %}
65+
{% if last_result %}
66+
<td>
67+
<a href="{% url 'extras:script_result' job_pk=last_result.pk %}">{{ last_result.created|annotated_date }}</a>
68+
</td>
69+
<td class="text-end">
70+
{% badge last_result.get_status_display last_result.get_status_color %}
71+
</td>
72+
{% else %}
73+
<td class="text-muted">Never</td>
74+
<td class="text-end">{{ ''|placeholder }}</td>
75+
{% endif %}
76+
{% endwith %}
77+
</tr>
78+
{% endfor %}
79+
{% endwith %}
80+
</tbody>
81+
</table>
82+
{% endif %}
7783
</div>
7884
</div>
7985
{% empty %}

0 commit comments

Comments
 (0)