Skip to content

Commit

Permalink
Closes #8765: Display and enable bulk clearing of user's table prefer…
Browse files Browse the repository at this point in the history
…ences
  • Loading branch information
jeremystretch committed Mar 3, 2022
1 parent 21e3159 commit e6072a5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
46 changes: 46 additions & 0 deletions netbox/templates/users/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<form method="post" action="" id="preferences-update">
{% csrf_token %}

{# Built-in preferences #}
{% for group, fields in form.fieldsets %}
<div class="field-group my-5">
<div class="row mb-2">
Expand All @@ -19,6 +20,7 @@ <h5 class="offset-sm-3">{{ group }}</h5>
</div>
{% endfor %}

{# Plugin preferences #}
{% with plugin_fields=form.plugin_fields %}
{% if plugin_fields %}
<div class="field-group my-5">
Expand All @@ -32,6 +34,50 @@ <h5 class="offset-sm-3">Plugins</h5>
{% endif %}
{% endwith %}

{# Table configurations #}
<div class="field-group my-5">
<div class="row mb-2">
<h5 class="offset-sm-3">Table Configurations</h5>
</div>
<div class="row">
{% if request.user.config.data.tables %}
<label class="col-sm-3 col-form-label text-lg-end">
Clear table preferences
</label>
<div class="col-sm-9">
<table class="table table-hover object-list">
<thead>
<tr>
<th>
<input type="checkbox" class="toggle form-check-input" title="Toggle All">
</th>
<th>Table</th>
<th>Ordering</th>
<th>Columns</th>
</tr>
</thead>
<tbody>
{% for table, prefs in request.user.config.data.tables.items %}
<tr>
<td>
<input type="checkbox" name="pk" value="tables.{{ table }}" class="form-check-input" />
</td>
<td>{{ table }}</td>
<td>{{ prefs.ordering|join:", "|placeholder }}</td>
<td>{{ prefs.columns|join:", "|placeholder }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<div class="offset-sm-3">
<p class="text-muted">None found</p>
</div>
{% endif %}
</div>
</div>

<div class="text-end my-3">
<a class="btn btn-outline-secondary" href="{% url 'user:preferences' %}">Cancel</a>
<button type="submit" name="_update" class="btn btn-primary">Save </button>
Expand Down
16 changes: 16 additions & 0 deletions netbox/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class UserConfigForm(BootstrapMixin, forms.ModelForm, metaclass=UserConfigFormMe
'data_format',
)),
)
# List of clearable preferences
pk = forms.MultipleChoiceField(
choices=[],
required=False
)

class Meta:
model = UserConfig
Expand All @@ -63,12 +68,23 @@ def __init__(self, *args, instance=None, **kwargs):

super().__init__(*args, instance=instance, **kwargs)

# Compile clearable preference choices
self.fields['pk'].choices = (
(f'tables.{table_name}', '') for table_name in instance.data.get('tables', [])
)

def save(self, *args, **kwargs):

# Set UserConfig data
for pref_name, value in self.cleaned_data.items():
if pref_name == 'pk':
continue
self.instance.set(pref_name, value, commit=False)

# Clear selected preferences
for preference in self.cleaned_data['pk']:
self.instance.clear(preference)

return super().save(*args, **kwargs)

@property
Expand Down

0 comments on commit e6072a5

Please sign in to comment.