From 48b825c64a8dd1387de59f5063084ad0b857e41c Mon Sep 17 00:00:00 2001 From: atownson <52260120+atownson@users.noreply.github.com> Date: Tue, 25 Feb 2025 08:39:39 -0600 Subject: [PATCH] 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 --- netbox/extras/urls.py | 3 +++ netbox/extras/views.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/netbox/extras/urls.py b/netbox/extras/urls.py index 32633493f..f470f72b1 100644 --- a/netbox/extras/urls.py +++ b/netbox/extras/urls.py @@ -75,8 +75,11 @@ urlpatterns = [ path('scripts/add/', views.ScriptModuleCreateView.as_view(), name='scriptmodule_add'), path('scripts/results//', views.ScriptResultView.as_view(), name='script_result'), path('scripts//', views.ScriptView.as_view(), name='script'), + path('scripts/./', views.ScriptView.as_view(), name='script'), path('scripts//source/', views.ScriptSourceView.as_view(), name='script_source'), + path('scripts/./source/', views.ScriptSourceView.as_view(), name='script_source'), path('scripts//jobs/', views.ScriptJobsView.as_view(), name='script_jobs'), + path('scripts/./jobs/', views.ScriptJobsView.as_view(), name='script_jobs'), path('script-modules//', include(get_model_urls('extras', 'scriptmodule'))), # Markdown diff --git a/netbox/extras/views.py b/netbox/extras/views.py index 86e7f214a..9b0eaebae 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -1251,6 +1251,14 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View): class BaseScriptView(generic.ObjectView): queryset = Script.objects.all() + def get_object(self, **kwargs): + if pk := kwargs.get('pk', False): + return get_object_or_404(self.queryset, pk=pk) + elif (module := kwargs.get('module')) and (name := kwargs.get('name', False)): + return get_object_or_404(self.queryset, module__file_path=f'{module}.py', name=name) + else: + raise Http404 + def _get_script_class(self, script): """ Return an instance of the Script's Python class