automatic reload on lists; new django decorator for ajax request
Change-Id: I3eb41de76217191268acb6053ad0c04aec0e8388
Signed-off-by: lombardofr <lombardo@everyup.it>
diff --git a/vimhandler/template/vim_list.html b/vimhandler/template/vim_list.html
index 07cb857..adc823a 100644
--- a/vimhandler/template/vim_list.html
+++ b/vimhandler/template/vim_list.html
@@ -50,29 +50,7 @@
</tr>
</thead>
<tbody>
- {% for p in datacenters %}
- <tr>
- <td>{{ p|get:"name" }}</td>
- <td>{{ p|get:"_id" }}</td>
- <td>{{ p|get:"vim_type" }}</td>
- <td>{{ p|get_sub:"_admin,operationalState"}}</td>
- <td>{{ p|get_sub:"_admin,description" }}</td>
-
-
- <td>
- <div class="btn-group">
- <button type="button" class="btn btn-default"
- onclick="location.href='{% url "vims:show" vim_id=p|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:deleteVim('{% url "vims:delete" vim_id=p|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>
@@ -88,13 +66,82 @@
<script src="/static/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<script>
$(document).ready( function () {
- $('#vims_table').DataTable();
- } );
+ var table = $('#vims_table').DataTable({
+ responsive: true,
+ "ajax": {
+ "url": "/vims/list/",
+ "dataSrc": function (json) {
+ return json['datacenters'];
+ },
+ 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["vim_type"];
+ },
+ "targets": 2
+ },
+ {
+ "render": function (data, type, row) {
+ return row["_admin"]['operationalState'];
+ },
+ "targets": 3
+ },
+ {
+ "render": function (data, type, row) {
+ return row["_admin"]['description'] || '';
+ },
+ "targets": 4
+ },
+ {
+ "render": function (data, type, row) {
+ return '<div class="btn-group"><button type="button" class="btn btn-default" ' +
+ 'onclick="location.href=\'/vims/'+row['_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:deleteVim(\''+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": 5
+ }
+ ]
+ });
+
+ setInterval(function () {
+ table.ajax.reload();
+ }, 10000);
+ });
</script>
<script>
- function deleteVim(url) {
- bootbox.confirm("Are you sure want to delete?", function (result) {
+ function deleteVim(vim_id, vim_name) {
+ var url = "/vims/"+vim_id+"/delete";
+ bootbox.confirm("Are you sure want to delete " + vim_name + "?", function (result) {
if (result) {
var dialog = bootbox.dialog({
message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
diff --git a/vimhandler/views.py b/vimhandler/views.py
index c40e4e9..d16fb7e 100644
--- a/vimhandler/views.py
+++ b/vimhandler/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
from lib.osm.osmclient.clientv2 import Client
@@ -31,13 +31,13 @@
def list(request):
user = osmutils.get_user(request)
project_id = user.project_id
+ result = {'type': 'ns', '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, 'vim_list.html')
client = Client()
- result = client.vim_list(user.get_token())
- print result
- result = {
- "project_id": project_id,
- "datacenters": result['data'] if result and result['error'] is False else []
- }
+ result_client = client.vim_list(user.get_token())
+ result["datacenters"] = result_client['data'] if result_client and result_client['error'] is False else []
return __response_handler(request, result, 'vim_list.html')