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

Closes #3581: Introduce commit_default custom script attribute to not commit changes by default

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

+ 1 - 0
CHANGELOG.md

@@ -15,6 +15,7 @@ v2.6.6 (FUTURE)
 * [#3545](https://github.com/netbox-community/netbox/issues/3545) - Add `MultiObjectVar` for custom scripts
 * [#3563](https://github.com/netbox-community/netbox/issues/3563) - Enable editing of individual DeviceType components
 * [#3580](https://github.com/netbox-community/netbox/issues/3580) - Render text and URL fields as textareas in the custom link form
+* [#3581](https://github.com/netbox-community/netbox/issues/3581) - Introduce `commit_default` custom script attribute to not commit changes by default
 
 ---
 

+ 8 - 0
docs/additional-features/custom-scripts.md

@@ -63,6 +63,14 @@ A list of field names indicating the order in which the form fields should appea
 field_order = ['var1', 'var2', 'var3']
 ```
 
+### `commit_default`
+
+The checkbox to commit database changes when executing a script is checked by default. Set `commit_default` to False under the script's Meta class to leave this option unchecked by default.
+
+```
+commit_default = False
+```
+
 ## Reading Data from Files
 
 The Script class provides two convenience methods for reading data from files:

+ 5 - 1
netbox/extras/forms.py

@@ -427,7 +427,7 @@ class ScriptForm(BootstrapMixin, forms.Form):
         help_text="Commit changes to the database (uncheck for a dry-run)"
     )
 
-    def __init__(self, vars, *args, **kwargs):
+    def __init__(self, vars, *args, commit_default=True, **kwargs):
 
         super().__init__(*args, **kwargs)
 
@@ -435,6 +435,10 @@ class ScriptForm(BootstrapMixin, forms.Form):
         for name, var in vars.items():
             self.fields[name] = var.as_field()
 
+        # Toggle default commit behavior based on Meta option
+        if not commit_default:
+            self.fields['_commit'].initial = False
+
         # Move _commit to the end of the form
         self.fields.move_to_end('_commit', True)
 

+ 1 - 1
netbox/extras/scripts.py

@@ -243,7 +243,7 @@ class BaseScript:
         Return a Django form suitable for populating the context data required to run this Script.
         """
         vars = self._get_vars()
-        form = ScriptForm(vars, data, files)
+        form = ScriptForm(vars, data, files, commit_default=getattr(self.Meta, 'commit_default', True))
 
         return form