diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index 8644277a8..9297b6716 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -513,7 +513,7 @@ class BaseScript: return data - def run_tests(self, job): + def run_tests(self): """ Run the report and save its results. Each test method will be executed in order. """ @@ -623,7 +623,7 @@ def run_script(data, job, request=None, commit=True, **kwargs): raise AbortTransaction() except AbortTransaction: msg = _("Database changes have been reverted automatically.") - if is_report(script): + if is_report(type(script)): # script and legacy reports have different log function signatures script.log_info(message=msg) else: @@ -640,7 +640,7 @@ def run_script(data, job, request=None, commit=True, **kwargs): except Exception as e: if type(e) is AbortScript: msg = _("Script aborted with error: ") + str(e) - if is_report(script): + if is_report(type(script)): script.log_failure(message=msg) else: script.log_failure(msg) @@ -649,13 +649,13 @@ def run_script(data, job, request=None, commit=True, **kwargs): else: stacktrace = traceback.format_exc() msg = _("An exception occurred: : ") + f"`{type(e).__name__}: {e}`\n```\n{stacktrace}\n```" - if is_report(script): + if is_report(type(script)): script.log_failure(message=msg) else: script.log_failure(msg) logger.error(f"Exception raised during script execution: {e}") msg = _("Database changes have been reverted due to error.") - if is_report(script): + if is_report(type(script)): script.log_info(message=msg) else: script.log_info(msg) diff --git a/netbox/extras/utils.py b/netbox/extras/utils.py index fd95b8f9b..c6b2de188 100644 --- a/netbox/extras/utils.py +++ b/netbox/extras/utils.py @@ -47,6 +47,17 @@ def register_features(model, features): registry['models'][app_label].add(model_name) +def is_script(obj): + """ + Returns True if the object is a Script. + """ + from .scripts import Script + try: + return issubclass(obj, Script) and obj != Script + except TypeError: + return False + + def is_report(obj): """ Returns True if the given object is a Report. @@ -54,20 +65,5 @@ def is_report(obj): from .reports import Report try: return issubclass(obj, Report) and obj != Report - except TypeError: - print("TypeError") - return False - - -def is_script(obj): - """ - Returns True if the object is a Script. - """ - - # TODO: Deprecated legacy reports support - from .reports import Report - from .scripts import Script - try: - return ((issubclass(obj, Report) and obj != Report) or (issubclass(obj, Script) and obj != Script)) except TypeError: return False