mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-12 10:38:16 -06:00
16145 allow both pk and script name
This commit is contained in:
parent
452ba22bd8
commit
754ab82a99
@ -217,12 +217,17 @@ class ScriptViewSet(ModelViewSet):
|
|||||||
lookup_value_regex = '[^/]+' # Allow dots
|
lookup_value_regex = '[^/]+' # Allow dots
|
||||||
|
|
||||||
def _get_script(self, pk):
|
def _get_script(self, pk):
|
||||||
try:
|
if pk.isnumeric():
|
||||||
module_name, script_name = pk.split('.', maxsplit=1)
|
script = get_object_or_404(self.queryset, pk=pk)
|
||||||
except ValueError:
|
module = script.module
|
||||||
raise Http404
|
else:
|
||||||
|
try:
|
||||||
|
module_name, script_name = pk.split('.', maxsplit=1)
|
||||||
|
except ValueError:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
module, script = get_module_and_script(module_name, script_name)
|
||||||
|
|
||||||
module, script = get_module_and_script(module_name, script_name)
|
|
||||||
if script is None:
|
if script is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
|
@ -777,23 +777,15 @@ class ScriptTest(APITestCase):
|
|||||||
is_executable=True,
|
is_executable=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_vars(self, *args):
|
def python_class(self):
|
||||||
return {
|
return self.TestScriptClass
|
||||||
k: v.__class__.__name__ for k, v in self.TestScriptClass._get_vars().items()
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_test_script(self, *args):
|
|
||||||
module = ScriptModule.objects.first()
|
|
||||||
return module, module.scripts.first()
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
|
|
||||||
# Monkey-patch the Script model to return our TestScriptClass above
|
# Monkey-patch the Script model to return our TestScriptClass above
|
||||||
from extras.api.views import ScriptViewSet
|
from extras.api.views import ScriptViewSet
|
||||||
ScriptViewSet._get_script = self.get_test_script
|
Script.python_class = self.python_class
|
||||||
from extras.api.serializers_.scripts import ScriptSerializer
|
|
||||||
ScriptSerializer.get_vars = self.get_vars
|
|
||||||
|
|
||||||
def test_get_script(self):
|
def test_get_script(self):
|
||||||
module = ScriptModule.objects.get(
|
module = ScriptModule.objects.get(
|
||||||
@ -801,7 +793,7 @@ class ScriptTest(APITestCase):
|
|||||||
file_path='/var/tmp/script.py'
|
file_path='/var/tmp/script.py'
|
||||||
)
|
)
|
||||||
script = module.scripts.all().first()
|
script = module.scripts.all().first()
|
||||||
url = reverse('extras-api:script-detail', kwargs={'pk': None})
|
url = reverse('extras-api:script-detail', kwargs={'pk': script.pk})
|
||||||
response = self.client.get(url, **self.header)
|
response = self.client.get(url, **self.header)
|
||||||
|
|
||||||
self.assertEqual(response.data['name'], self.TestScriptClass.Meta.name)
|
self.assertEqual(response.data['name'], self.TestScriptClass.Meta.name)
|
||||||
|
Loading…
Reference in New Issue
Block a user