Просмотр исходного кода

Log a warning for scripts whose run() method does not accept a commit argument

Jeremy Stretch 5 лет назад
Родитель
Сommit
b410674f9e
2 измененных файлов с 6 добавлено и 1 удалено
  1. 1 1
      docs/additional-features/custom-scripts.md
  2. 5 0
      netbox/extras/scripts.py

+ 1 - 1
docs/additional-features/custom-scripts.md

@@ -37,7 +37,7 @@ The `run()` method should accept two arguments:
 * `commit` - A boolean indicating whether database changes will be committed.
 
 !!! note
-    The `commit` argument was introduced in NetBox v2.7.8. Backward compatibility is maintained for scripts which accept only the `data` argument, however moving forward scripts should accept both arguments.
+    The `commit` argument was introduced in NetBox v2.7.8. Backward compatibility is maintained for scripts which accept only the `data` argument, however beginning with v2.10 NetBox will require the `run()` method of every script to accept both arguments. (Either argument may still be ignored within the method.)
 
 Defining variables is optional: You may create a script with only a `run()` method if no user input is needed.
 

+ 5 - 0
netbox/extras/scripts.py

@@ -4,6 +4,7 @@ import logging
 import os
 import pkgutil
 import traceback
+import warnings
 from collections import OrderedDict
 
 import yaml
@@ -405,12 +406,16 @@ def run_script(data, request, commit=True, *args, **kwargs):
     # Add the current request as a property of the script
     script.request = request
 
+    # TODO: Drop backward-compatibility for absent 'commit' argument in v2.10
     # Determine whether the script accepts a 'commit' argument (this was introduced in v2.7.8)
     kwargs = {
         'data': data
     }
     if 'commit' in inspect.signature(script.run).parameters:
         kwargs['commit'] = commit
+    else:
+        warnings.warn(f"The run() method of script {script} should support a 'commit' argument. This will be required "
+                      f"beginning with NetBox v2.10.")
 
     try:
         with transaction.atomic():