1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Add option to commit database changes

This commit is contained in:
Jeremy Stretch
2019-08-12 14:28:06 -04:00
parent 3e75da4307
commit 0d289d660d
5 changed files with 38 additions and 11 deletions

View File

@ -7,6 +7,7 @@ from django.conf import settings
from django.core.validators import RegexValidator
from django.db import transaction
from utilities.exceptions import AbortTransaction
from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_SUCCESS, LOG_WARNING
from .forms import ScriptForm
@ -197,21 +198,32 @@ def is_variable(obj):
return isinstance(obj, ScriptVariable)
def run_script(script, data=None):
def run_script(script, data, commit=True):
"""
A wrapper for calling Script.run(). This performs error handling. It exists outside of the Script class to ensure
it cannot be overridden by a script author.
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.
"""
output = None
try:
with transaction.atomic():
return script.run(data)
output = script.run(data)
if not commit:
raise AbortTransaction()
except AbortTransaction:
pass
except Exception as e:
script.log_failure(
"An exception occurred: {}".format(e)
)
script.log_info(
"Database changes have been reverted automatically."
"An exception occurred. {}: {}".format(type(e).__name__, e)
)
commit = False
finally:
if not commit:
script.log_info(
"Database changes have been reverted automatically."
)
return output
def get_scripts():