mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Implemented run_script() wrapper
This commit is contained in:
@ -5,6 +5,7 @@ import pkgutil
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.core.validators import RegexValidator
|
||||
from django.db import transaction
|
||||
|
||||
from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_SUCCESS, LOG_WARNING
|
||||
from .forms import ScriptForm
|
||||
@ -196,6 +197,23 @@ def is_variable(obj):
|
||||
return isinstance(obj, ScriptVariable)
|
||||
|
||||
|
||||
def run_script(script, data=None):
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
try:
|
||||
with transaction.atomic():
|
||||
return script.run(data)
|
||||
except Exception as e:
|
||||
script.log_failure(
|
||||
"An exception occurred: {}".format(e)
|
||||
)
|
||||
script.log_info(
|
||||
"Database changes have been reverted automatically."
|
||||
)
|
||||
|
||||
|
||||
def get_scripts():
|
||||
scripts = OrderedDict()
|
||||
|
||||
|
Reference in New Issue
Block a user