Skip to content

Commit 48b825c

Browse files
authored
Closes #18024: Add URL pattern for scripts to reference them by module.name (#18723)
* Add URL pattern for scripts to reference them by module.name * Change _get_script function name and syntax * Fix formatting issue
1 parent 4fb42ac commit 48b825c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

netbox/extras/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@
7575
path('scripts/add/', views.ScriptModuleCreateView.as_view(), name='scriptmodule_add'),
7676
path('scripts/results/<int:job_pk>/', views.ScriptResultView.as_view(), name='script_result'),
7777
path('scripts/<int:pk>/', views.ScriptView.as_view(), name='script'),
78+
path('scripts/<str:module>.<str:name>/', views.ScriptView.as_view(), name='script'),
7879
path('scripts/<int:pk>/source/', views.ScriptSourceView.as_view(), name='script_source'),
80+
path('scripts/<str:module>.<str:name>/source/', views.ScriptSourceView.as_view(), name='script_source'),
7981
path('scripts/<int:pk>/jobs/', views.ScriptJobsView.as_view(), name='script_jobs'),
82+
path('scripts/<str:module>.<str:name>/jobs/', views.ScriptJobsView.as_view(), name='script_jobs'),
8083
path('script-modules/<int:pk>/', include(get_model_urls('extras', 'scriptmodule'))),
8184

8285
# Markdown

netbox/extras/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,14 @@ def get(self, request):
12511251
class BaseScriptView(generic.ObjectView):
12521252
queryset = Script.objects.all()
12531253

1254+
def get_object(self, **kwargs):
1255+
if pk := kwargs.get('pk', False):
1256+
return get_object_or_404(self.queryset, pk=pk)
1257+
elif (module := kwargs.get('module')) and (name := kwargs.get('name', False)):
1258+
return get_object_or_404(self.queryset, module__file_path=f'{module}.py', name=name)
1259+
else:
1260+
raise Http404
1261+
12541262
def _get_script_class(self, script):
12551263
"""
12561264
Return an instance of the Script's Python class

0 commit comments

Comments
 (0)