1
0
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:
Ryan Merolle
2023-03-14 15:50:49 -04:00
committed by GitHub
parent 016eff52c0
commit 4286d74d44
3 changed files with 51 additions and 6 deletions

View File

@@ -352,6 +352,18 @@ class BaseScript:
# Set initial "commit" checkbox state based on the script's Meta parameter
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
# Logging

View File

@@ -47,16 +47,34 @@
{% csrf_token %}
<div class="field-group my-4">
{% if form.requires_input %}
<div class="row mb-2">
<h5 class="offset-sm-3">Script Data</h5>
</div>
{% if script.Meta.fieldsets %}
{# Render grouped fields according to declared fieldsets #}
{% 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 %}
<div class="alert alert-info">
<i class="mdi mdi-information"></i>
This script does not require any input to run.
</div>
{% render_form form %}
{% endif %}
{% render_form form %}
</div>
<div class="float-end">
<a href="{% url 'extras:script_list' %}" class="btn btn-outline-danger">Cancel</a>