mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 10:58:37 -06:00
Add URL pattern for scripts to reference them by module.name
This commit is contained in:
parent
fba4141ce3
commit
dc9d54c3c1
@ -75,8 +75,11 @@ urlpatterns = [
|
||||
path('scripts/add/', views.ScriptModuleCreateView.as_view(), name='scriptmodule_add'),
|
||||
path('scripts/results/<int:job_pk>/', views.ScriptResultView.as_view(), name='script_result'),
|
||||
path('scripts/<int:pk>/', views.ScriptView.as_view(), name='script'),
|
||||
path('scripts/<str:module>.<str:name>/', views.ScriptView.as_view(), name='script'),
|
||||
path('scripts/<int:pk>/source/', views.ScriptSourceView.as_view(), name='script_source'),
|
||||
path('scripts/<str:module>.<str:name>/source/', views.ScriptSourceView.as_view(), name='script_source'),
|
||||
path('scripts/<int:pk>/jobs/', views.ScriptJobsView.as_view(), name='script_jobs'),
|
||||
path('scripts/<str:module>.<str:name>/jobs/', views.ScriptJobsView.as_view(), name='script_jobs'),
|
||||
path('script-modules/<int:pk>/', include(get_model_urls('extras', 'scriptmodule'))),
|
||||
|
||||
# Markdown
|
||||
|
@ -1251,6 +1251,17 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
|
||||
class BaseScriptView(generic.ObjectView):
|
||||
queryset = Script.objects.all()
|
||||
|
||||
def _get_script(self, **kwargs):
|
||||
if 'pk' in kwargs:
|
||||
pk = kwargs.get('pk')
|
||||
return get_object_or_404(self.queryset, pk=pk)
|
||||
elif 'module' in kwargs and 'name' in kwargs:
|
||||
module = kwargs.get('module')
|
||||
name = kwargs.get('name')
|
||||
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
|
||||
@ -1262,7 +1273,7 @@ class BaseScriptView(generic.ObjectView):
|
||||
class ScriptView(BaseScriptView):
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
script = self.get_object(**kwargs)
|
||||
script = self._get_script(**kwargs)
|
||||
script_class = self._get_script_class(script)
|
||||
if not script_class:
|
||||
return render(request, 'extras/script.html', {
|
||||
@ -1281,7 +1292,7 @@ class ScriptView(BaseScriptView):
|
||||
})
|
||||
|
||||
def post(self, request, **kwargs):
|
||||
script = self.get_object(**kwargs)
|
||||
script = self._get_script(**kwargs)
|
||||
|
||||
if not request.user.has_perm('extras.run_script', obj=script):
|
||||
return HttpResponseForbidden()
|
||||
@ -1326,7 +1337,7 @@ class ScriptSourceView(BaseScriptView):
|
||||
queryset = Script.objects.all()
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
script = self.get_object(**kwargs)
|
||||
script = self._get_script(**kwargs)
|
||||
script_class = self._get_script_class(script)
|
||||
|
||||
return render(request, 'extras/script/source.html', {
|
||||
@ -1341,7 +1352,7 @@ class ScriptJobsView(BaseScriptView):
|
||||
queryset = Script.objects.all()
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
script = self.get_object(**kwargs)
|
||||
script = self._get_script(**kwargs)
|
||||
|
||||
jobs_table = JobTable(
|
||||
data=script.jobs.all(),
|
||||
|
Loading…
Reference in New Issue
Block a user