Просмотр исходного кода

Convert table config updates to use REST API

Jeremy Stretch 5 лет назад
Родитель
Сommit
4de7fcd758

+ 25 - 0
netbox/project-static/js/tableconfig.js

@@ -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);
+        });
+    });
+});

+ 1 - 0
netbox/templates/base.html

@@ -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();

+ 1 - 1
netbox/templates/inc/table_config_form.html

@@ -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">

+ 5 - 0
netbox/templates/utilities/obj_list.html

@@ -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 %}

+ 6 - 0
netbox/utilities/forms/forms.py

@@ -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__

+ 0 - 17
netbox/utilities/views.py

@@ -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 {}