automatic reload on lists; new django decorator for ajax request

Change-Id: I3eb41de76217191268acb6053ad0c04aec0e8388
Signed-off-by: lombardofr <lombardo@everyup.it>
diff --git a/sdnctrlhandler/template/sdn_list.html b/sdnctrlhandler/template/sdn_list.html
index 423efcd..da5526f 100644
--- a/sdnctrlhandler/template/sdn_list.html
+++ b/sdnctrlhandler/template/sdn_list.html
@@ -51,27 +51,7 @@
                         </tr>
                         </thead>
                         <tbody>
-                        {% for s in sdns %}
-                            <tr>
-                                <td>{{ s|get:"name" }}</td>
-                                <td>{{ s|get:"_id" }}</td>
-                                <td>{{ s|get:"type" }}</td>
-                                <td>{{ s|get_sub:"_admin,operationalState"}}</td>
-                                <td>{{ s|get:"ip" }}</td>
-                                <td>{{ s|get:"port" }}</td>
-                                <td>
-                                    <div class="btn-group">
-                                        <button type="button" class="btn btn-default"
-                                                onclick="javascript:showSDN( '{{ s|get:"_id" }}')" data-toggle="tooltip" data-placement="top" data-container="body" title="Show Info"><i
-                                                class="fa fa-info"></i></button>
-                                        <button type="button" class="btn btn-default"
-                                                onclick="javascript:deleteSDN('{{ s|get:"_id" }}')" data-toggle="tooltip" data-placement="top" data-container="body" title="Delete"><i
-                                                class="far fa-trash-alt" ></i></button>
-                                    </div>
-                                </td>
 
-                            </tr>
-                        {% endfor %}
                         </tbody>
                     </table>
                 </div>
@@ -91,8 +71,83 @@
 
      <script>
     $(document).ready( function () {
-        $('#sdns_table').DataTable();
-    } );
+        var table = $('#sdns_table').DataTable({
+            responsive: true,
+            "ajax": {
+                "url": "/sdn/list",
+                "dataSrc": function (json) {
+                    console.log(json)
+                    return json['sdns'];
+                },
+                statusCode: {
+                    401: function () {
+                        console.log("no auth");
+                        moveToLogin(window.location.pathname);
+                    }
+                },
+                "error": function (hxr, error, thrown) {
+                    console.log(hxr)
+                    console.log(thrown)
+                    console.log(error);
+                }
+
+            },
+            "columns": [
+                {
+                    "render": function (data, type, row) {
+                        return row["name"];
+                    },
+                    "targets": 0
+                },
+                {
+                    "render": function (data, type, row) {
+                        return row['_id'];
+                    },
+                    "targets": 1
+                },
+                {
+                    "render": function (data, type, row) {
+                        return row["type"];
+                    },
+                    "targets": 2
+                },
+                {
+                    "render": function (data, type, row) {
+                        return row["_admin"]["operationalState"];
+                    },
+                    "targets": 3
+                },
+                {
+                    "render": function (data, type, row) {
+                        return row["ip"];
+                    },
+                    "targets": 4
+                },
+                {
+                    "render": function (data, type, row) {
+                        return row["port"];
+                    },
+                    "targets": 5
+                },
+                {
+                    "render": function (data, type, row) {
+                        return '<div class="btn-group">' +
+                            '<button type="button" class="btn btn-default" ' +
+                            'onclick="javascript:showSDN( \''+row['_id'] + '\', \''+row['name'] +'\')" data-toggle="tooltip" data-placement="top" data-container="body" title="Show Info"><i class="fa fa-info"></i>' +
+                        '</button>' +
+                        '<button type="button" class="btn btn-default"' +
+                        'onclick="javascript:deleteSDN(\''+row['_id']+'\', \''+ row["name"] +'\')" data-toggle="tooltip" data-placement="top" data-container="body" title="Delete">' +
+                        '<i class="far fa-trash-alt" ></i></button></div>';
+                    },
+                    "targets": 6
+                }
+            ]
+        });
+
+        setInterval(function () {
+                table.ajax.reload();
+            }, 10000);
+    });
     </script>
 {% endblock %}
 
diff --git a/sdnctrlhandler/views.py b/sdnctrlhandler/views.py
index e015ded..52be0a4 100644
--- a/sdnctrlhandler/views.py
+++ b/sdnctrlhandler/views.py
@@ -15,7 +15,7 @@
 #
 
 from django.shortcuts import render, redirect
-from django.contrib.auth.decorators import login_required
+from sf_t3d.decorators import login_required
 from django.http import HttpResponse
 import json
 import logging
@@ -30,13 +30,15 @@
 def list(request):
     user = osmutils.get_user(request)
     project_id = user.project_id
+    result = {'project_id': project_id}
+    raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
+    if 'application/json' not in raw_content_types:
+        return __response_handler(request, result, 'sdn_list.html')
     client = Client()
-    result = client.sdn_list(user.get_token())
+    result_client = client.sdn_list(user.get_token())
 
-    result = {
-        'project_id': project_id,
-        'sdns': result['data'] if result and result['error'] is False else []
-    }
+    result['sdns'] = result_client['data'] if result_client and result_client['error'] is False else []
+
     return __response_handler(request, result, 'sdn_list.html')