Convert table config updates to use REST API

This commit is contained in:
Jeremy Stretch 2020-10-21 14:52:50 -04:00
parent 2845dd488e
commit 4de7fcd758
6 changed files with 38 additions and 18 deletions

View File

@ -0,0 +1,25 @@
$(document).ready(function() {
$('form.tableconfigform').submit(function(event) {
event.preventDefault();
let table_name = this.getAttribute('data-table-name');
let data = {"tables": {}};
data['tables'][table_name] = {};
data['tables'][table_name]['columns'] = $('#id_columns').val();
$.ajax({
url: netbox_api_path + 'users/config/',
async: true,
contentType: 'application/json',
dataType: 'json',
type: 'PATCH',
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", netbox_csrf_token);
},
data: JSON.stringify(data),
}).done(function () {
// Reload the page
window.location.reload(true);
}).fail(function (xhr, status, error) {
alert("Failed: " + error);
});
});
});

View File

@ -96,6 +96,7 @@
onerror="window.location='{% url 'media_failure' %}?filename=js/forms.js'"></script>
<script type="text/javascript">
var netbox_api_path = "/{{ settings.BASE_PATH }}api/";
var netbox_csrf_token = "{{ csrf_token }}";
var loading = $(".loading");
$(document).ajaxStart(function() {
loading.show();

View File

@ -7,7 +7,7 @@
<h4 class="modal-title">Table Configuration</h4>
</div>
<div class="modal-body">
<form action="" method="post" class="form-horizontal">
<form action="" method="post" class="form-horizontal tableconfigform" data-table-name="{{ table_config_form.table_name }}">
{% csrf_token %}
{% render_form table_config_form %}
<div class="row">

View File

@ -1,6 +1,7 @@
{% extends 'base.html' %}
{% load buttons %}
{% load helpers %}
{% load static %}
{% block content %}
<div class="pull-right noprint">
@ -83,3 +84,7 @@
{% endif %}
</div>
{% endblock %}
{% block javascript %}
<script src="{% static 'js/tableconfig.js' %}"></script>
{% endblock %}

View File

@ -168,8 +168,14 @@ class TableConfigForm(BootstrapMixin, forms.Form):
)
def __init__(self, table, *args, **kwargs):
self.table = table
super().__init__(*args, **kwargs)
# Initialize columns field based on table attributes
self.fields['columns'].choices = table.configurable_columns
self.fields['columns'].initial = table.visible_columns
@property
def table_name(self):
return self.table.__class__.__name__

View File

@ -317,23 +317,6 @@ class ObjectListView(ObjectPermissionRequiredMixin, View):
return render(request, self.template_name, context)
@method_decorator(login_required)
def post(self, request):
# Update the user's table configuration
table = self.table(self.queryset)
form = TableConfigForm(table=table, data=request.POST)
preference_name = f"tables.{self.table.__name__}.columns"
if form.is_valid():
if 'set' in request.POST:
request.user.config.set(preference_name, form.cleaned_data['columns'], commit=True)
elif 'clear' in request.POST:
request.user.config.clear(preference_name, commit=True)
messages.success(request, "Your preferences have been updated.")
return redirect(request.get_full_path())
def extra_context(self):
return {}