mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 09:28:38 -06:00
Convert table config updates to use REST API
This commit is contained in:
parent
2845dd488e
commit
4de7fcd758
25
netbox/project-static/js/tableconfig.js
Normal file
25
netbox/project-static/js/tableconfig.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -96,6 +96,7 @@
|
|||||||
onerror="window.location='{% url 'media_failure' %}?filename=js/forms.js'"></script>
|
onerror="window.location='{% url 'media_failure' %}?filename=js/forms.js'"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var netbox_api_path = "/{{ settings.BASE_PATH }}api/";
|
var netbox_api_path = "/{{ settings.BASE_PATH }}api/";
|
||||||
|
var netbox_csrf_token = "{{ csrf_token }}";
|
||||||
var loading = $(".loading");
|
var loading = $(".loading");
|
||||||
$(document).ajaxStart(function() {
|
$(document).ajaxStart(function() {
|
||||||
loading.show();
|
loading.show();
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
<h4 class="modal-title">Table Configuration</h4>
|
<h4 class="modal-title">Table Configuration</h4>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<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 %}
|
{% csrf_token %}
|
||||||
{% render_form table_config_form %}
|
{% render_form table_config_form %}
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{% extends 'base.html' %}
|
{% extends 'base.html' %}
|
||||||
{% load buttons %}
|
{% load buttons %}
|
||||||
{% load helpers %}
|
{% load helpers %}
|
||||||
|
{% load static %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="pull-right noprint">
|
<div class="pull-right noprint">
|
||||||
@ -83,3 +84,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block javascript %}
|
||||||
|
<script src="{% static 'js/tableconfig.js' %}"></script>
|
||||||
|
{% endblock %}
|
||||||
|
@ -168,8 +168,14 @@ class TableConfigForm(BootstrapMixin, forms.Form):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, table, *args, **kwargs):
|
def __init__(self, table, *args, **kwargs):
|
||||||
|
self.table = table
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
# Initialize columns field based on table attributes
|
# Initialize columns field based on table attributes
|
||||||
self.fields['columns'].choices = table.configurable_columns
|
self.fields['columns'].choices = table.configurable_columns
|
||||||
self.fields['columns'].initial = table.visible_columns
|
self.fields['columns'].initial = table.visible_columns
|
||||||
|
|
||||||
|
@property
|
||||||
|
def table_name(self):
|
||||||
|
return self.table.__class__.__name__
|
||||||
|
@ -317,23 +317,6 @@ class ObjectListView(ObjectPermissionRequiredMixin, View):
|
|||||||
|
|
||||||
return render(request, self.template_name, context)
|
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):
|
def extra_context(self):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user