mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Add fieldsets functionality to scripts to allow for form field groupings (#11880)
* update script template * update docs * introduce default_fieldset * correct custom script docs * default to use fieldsets in scripts * update scripts docs for new behavior * Misc cleanup --------- Co-authored-by: jeremystretch <jstretch@netboxlabs.com>
This commit is contained in:
@ -79,7 +79,22 @@ A human-friendly description of what your script does.
|
|||||||
|
|
||||||
### `field_order`
|
### `field_order`
|
||||||
|
|
||||||
By default, script variables will be ordered in the form as they are defined in the script. `field_order` may be defined as an iterable of field names to determine the order in which variables are rendered. Any fields not included in this iterable be listed last.
|
By default, script variables will be ordered in the form as they are defined in the script. `field_order` may be defined as an iterable of field names to determine the order in which variables are rendered within a default "Script Data" group. Any fields not included in this iterable be listed last. If `fieldsets` is defined, `field_order` will be ignored. A fieldset group for "Script Execution Parameters" will be added to the end of the form by default for the user.
|
||||||
|
|
||||||
|
### `fieldsets`
|
||||||
|
|
||||||
|
`fieldsets` may be defined as an iterable of field groups and their field names to determine the order in which variables are group and rendered. Any fields not included in this iterable will not be displayed in the form. If `fieldsets` is defined, `field_order` will be ignored. A fieldset group for "Script Execution Parameters" will be added to the end of the fieldsets by default for the user.
|
||||||
|
|
||||||
|
An example fieldset definition is provided below:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class MyScript(Script):
|
||||||
|
class Meta:
|
||||||
|
fieldsets = (
|
||||||
|
('First group', ('field1', 'field2', 'field3')),
|
||||||
|
('Second group', ('field4', 'field5')),
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
### `commit_default`
|
### `commit_default`
|
||||||
|
|
||||||
@ -302,7 +317,7 @@ Optionally `schedule_at` can be passed in the form data with a datetime string t
|
|||||||
Scripts can be run on the CLI by invoking the management command:
|
Scripts can be run on the CLI by invoking the management command:
|
||||||
|
|
||||||
```
|
```
|
||||||
python3 manage.py runscript [--commit] [--loglevel {debug,info,warning,error,critical}] [--data "<data>"] <module>.<script>
|
python3 manage.py runscript [--commit] [--loglevel {debug,info,warning,error,critical}] [--data "<data>"] <module>.<script>
|
||||||
```
|
```
|
||||||
|
|
||||||
The required ``<module>.<script>`` argument is the script to run where ``<module>`` is the name of the python file in the ``scripts`` directory without the ``.py`` extension and ``<script>`` is the name of the script class in the ``<module>`` to run.
|
The required ``<module>.<script>`` argument is the script to run where ``<module>`` is the name of the python file in the ``scripts`` directory without the ``.py`` extension and ``<script>`` is the name of the script class in the ``<module>`` to run.
|
||||||
|
@ -352,6 +352,18 @@ class BaseScript:
|
|||||||
# Set initial "commit" checkbox state based on the script's Meta parameter
|
# Set initial "commit" checkbox state based on the script's Meta parameter
|
||||||
form.fields['_commit'].initial = getattr(self.Meta, 'commit_default', True)
|
form.fields['_commit'].initial = getattr(self.Meta, 'commit_default', True)
|
||||||
|
|
||||||
|
# Append the default fieldset if defined in the Meta class
|
||||||
|
default_fieldset = (
|
||||||
|
('Script Execution Parameters', ('_schedule_at', '_interval', '_commit')),
|
||||||
|
)
|
||||||
|
if not hasattr(self.Meta, 'fieldsets'):
|
||||||
|
fields = (
|
||||||
|
name for name, _ in self._get_vars().items()
|
||||||
|
)
|
||||||
|
self.Meta.fieldsets = (('Script Data', fields),)
|
||||||
|
|
||||||
|
self.Meta.fieldsets += default_fieldset
|
||||||
|
|
||||||
return form
|
return form
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
|
@ -47,16 +47,34 @@
|
|||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="field-group my-4">
|
<div class="field-group my-4">
|
||||||
{% if form.requires_input %}
|
{% if form.requires_input %}
|
||||||
<div class="row mb-2">
|
{% if script.Meta.fieldsets %}
|
||||||
<h5 class="offset-sm-3">Script Data</h5>
|
{# Render grouped fields according to declared fieldsets #}
|
||||||
</div>
|
{% for group, fields in script.Meta.fieldsets %}
|
||||||
|
<div class="field-group mb-5">
|
||||||
|
<div class="row mb-2">
|
||||||
|
<h5 class="offset-sm-3">{{ group }}</h5>
|
||||||
|
</div>
|
||||||
|
{% for name in fields %}
|
||||||
|
{% with field=form|getfield:name %}
|
||||||
|
{% render_field field %}
|
||||||
|
{% endwith %}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% else %}
|
||||||
|
{# Render all fields as a single group #}
|
||||||
|
<div class="row mb-2">
|
||||||
|
<h5 class="offset-sm-3">Script Data</h5>
|
||||||
|
</div>
|
||||||
|
{% render_form form %}
|
||||||
|
{% endif %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="alert alert-info">
|
<div class="alert alert-info">
|
||||||
<i class="mdi mdi-information"></i>
|
<i class="mdi mdi-information"></i>
|
||||||
This script does not require any input to run.
|
This script does not require any input to run.
|
||||||
</div>
|
</div>
|
||||||
|
{% render_form form %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% render_form form %}
|
|
||||||
</div>
|
</div>
|
||||||
<div class="float-end">
|
<div class="float-end">
|
||||||
<a href="{% url 'extras:script_list' %}" class="btn btn-outline-danger">Cancel</a>
|
<a href="{% url 'extras:script_list' %}" class="btn btn-outline-danger">Cancel</a>
|
||||||
|
Reference in New Issue
Block a user