|
| 1 | +from django.http import Http404 |
1 | 2 | from django.shortcuts import get_object_or_404
|
2 | 3 | from django_rq.queues import get_connection
|
3 | 4 | from rest_framework import status
|
@@ -215,21 +216,32 @@ class ScriptViewSet(ModelViewSet):
|
215 | 216 | _ignore_model_permissions = True
|
216 | 217 | lookup_value_regex = '[^/]+' # Allow dots
|
217 | 218 |
|
| 219 | + def _get_script(self, pk): |
| 220 | + # If pk is numeric, retrieve script by ID |
| 221 | + if pk.isnumeric(): |
| 222 | + return get_object_or_404(self.queryset, pk=pk) |
| 223 | + |
| 224 | + # Default to retrieval by module & name |
| 225 | + try: |
| 226 | + module_name, script_name = pk.split('.', maxsplit=1) |
| 227 | + except ValueError: |
| 228 | + raise Http404 |
| 229 | + return get_object_or_404(self.queryset, module__file_path=f'{module_name}.py', name=script_name) |
| 230 | + |
218 | 231 | def retrieve(self, request, pk):
|
219 |
| - script = get_object_or_404(self.queryset, pk=pk) |
| 232 | + script = self._get_script(pk) |
220 | 233 | serializer = serializers.ScriptDetailSerializer(script, context={'request': request})
|
221 | 234 |
|
222 | 235 | return Response(serializer.data)
|
223 | 236 |
|
224 | 237 | def post(self, request, pk):
|
225 | 238 | """
|
226 |
| - Run a Script identified by the id and return the pending Job as the result |
| 239 | + Run a Script identified by its numeric PK or module & name and return the pending Job as the result |
227 | 240 | """
|
228 |
| - |
229 | 241 | if not request.user.has_perm('extras.run_script'):
|
230 | 242 | raise PermissionDenied("This user does not have permission to run scripts.")
|
231 | 243 |
|
232 |
| - script = get_object_or_404(self.queryset, pk=pk) |
| 244 | + script = self._get_script(pk) |
233 | 245 | input_serializer = serializers.ScriptInputSerializer(
|
234 | 246 | data=request.data,
|
235 | 247 | context={'script': script}
|
|
0 commit comments