Skip to content

Commit a956e6b

Browse files
committed
Set the initial value of checkboxes when rerunning scripts
1 parent 35cbfd3 commit a956e6b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

netbox_script_manager/scripts.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import rq
99
from django.conf import settings
1010
from django.db import transaction
11+
from django.forms.fields import BooleanField
1112
from django.utils.functional import classproperty
1213
from extras.context_managers import change_logging
1314
from extras.scripts import ScriptVariable
@@ -149,14 +150,24 @@ def get_fieldsets(self, instance=None):
149150

150151
def as_form(self, data=None, files=None, initial=None, script_instance=None):
151152
"""
152-
Return a Django form suitable for populating the context data required to run this Script.
153+
Return a form based on any ScriptVars defined in the script.
153154
"""
154155
# Create a dynamic ScriptForm subclass from script variables
155156
fields = {name: var.as_field() for name, var in self._get_vars().items()}
156157
FormClass = type("ScriptForm", (ScriptForm,), fields)
157158

158159
form = FormClass(data, files, initial=initial)
159160

161+
# Hackish way to set initial values for BooleanFields when rerunning a script
162+
# The sideeffect here is that the default value is not respected if the the
163+
# user manually includes GET parameters in the URL.
164+
if initial:
165+
for field_name, field in form.fields.items():
166+
if field_name == "_commit" or not isinstance(field, BooleanField):
167+
continue
168+
169+
field.initial = bool(initial.get(field_name))
170+
160171
# Set initial "commit" checkbox state based on the script's Meta parameter
161172
form.fields["_commit"].initial = self.commit_default
162173

0 commit comments

Comments
 (0)