|
8 | 8 | import rq
|
9 | 9 | from django.conf import settings
|
10 | 10 | from django.db import transaction
|
| 11 | +from django.forms.fields import BooleanField |
11 | 12 | from django.utils.functional import classproperty
|
12 | 13 | from extras.context_managers import change_logging
|
13 | 14 | from extras.scripts import ScriptVariable
|
@@ -149,14 +150,24 @@ def get_fieldsets(self, instance=None):
|
149 | 150 |
|
150 | 151 | def as_form(self, data=None, files=None, initial=None, script_instance=None):
|
151 | 152 | """
|
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. |
153 | 154 | """
|
154 | 155 | # Create a dynamic ScriptForm subclass from script variables
|
155 | 156 | fields = {name: var.as_field() for name, var in self._get_vars().items()}
|
156 | 157 | FormClass = type("ScriptForm", (ScriptForm,), fields)
|
157 | 158 |
|
158 | 159 | form = FormClass(data, files, initial=initial)
|
159 | 160 |
|
| 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 | + |
160 | 171 | # Set initial "commit" checkbox state based on the script's Meta parameter
|
161 | 172 | form.fields["_commit"].initial = self.commit_default
|
162 | 173 |
|
|
0 commit comments