mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 04:02:52 -06:00
Implement request passing as a property of Script
This commit is contained in:
parent
6a451e0c0e
commit
0174c9747b
@ -235,6 +235,9 @@ class BaseScript:
|
|||||||
# Initiate the log
|
# Initiate the log
|
||||||
self.log = []
|
self.log = []
|
||||||
|
|
||||||
|
# Declare the placeholder for the current request
|
||||||
|
self.request = None
|
||||||
|
|
||||||
# Grab some info about the script
|
# Grab some info about the script
|
||||||
self.filename = inspect.getfile(self.__class__)
|
self.filename = inspect.getfile(self.__class__)
|
||||||
self.source = inspect.getsource(self.__class__)
|
self.source = inspect.getsource(self.__class__)
|
||||||
@ -337,7 +340,7 @@ def is_variable(obj):
|
|||||||
return isinstance(obj, ScriptVariable)
|
return isinstance(obj, ScriptVariable)
|
||||||
|
|
||||||
|
|
||||||
def run_script(script, data, files, commit=True):
|
def run_script(script, data, request, commit=True):
|
||||||
"""
|
"""
|
||||||
A wrapper for calling Script.run(). This performs error handling and provides a hook for committing changes. It
|
A wrapper for calling Script.run(). This performs error handling and provides a hook for committing changes. It
|
||||||
exists outside of the Script class to ensure it cannot be overridden by a script author.
|
exists outside of the Script class to ensure it cannot be overridden by a script author.
|
||||||
@ -347,9 +350,13 @@ def run_script(script, data, files, commit=True):
|
|||||||
end_time = None
|
end_time = None
|
||||||
|
|
||||||
# Add files to form data
|
# Add files to form data
|
||||||
|
files = request.FILES
|
||||||
for field_name, fileobj in files.items():
|
for field_name, fileobj in files.items():
|
||||||
data[field_name] = fileobj
|
data[field_name] = fileobj
|
||||||
|
|
||||||
|
# Add the current request as a property of the script
|
||||||
|
script.request = request
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
@ -413,7 +413,7 @@ class ScriptView(PermissionRequiredMixin, View):
|
|||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
commit = form.cleaned_data.pop('_commit')
|
commit = form.cleaned_data.pop('_commit')
|
||||||
output, execution_time = run_script(script, form.cleaned_data, request.FILES, commit)
|
output, execution_time = run_script(script, form.cleaned_data, request, commit)
|
||||||
|
|
||||||
return render(request, 'extras/script.html', {
|
return render(request, 'extras/script.html', {
|
||||||
'module': module,
|
'module': module,
|
||||||
|
Loading…
Reference in New Issue
Block a user