diff --git a/apps/assets/api.py b/apps/assets/api.py index 534e837eb..f02f1411f 100644 --- a/apps/assets/api.py +++ b/apps/assets/api.py @@ -3,8 +3,9 @@ from rest_framework import serializers from rest_framework import viewsets, serializers,generics from .models import AssetGroup, Asset, IDC, AssetExtend - - +from common.mixins import BulkDeleteApiMixin +from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin,ListBulkCreateUpdateDestroyAPIView +from .serializers import * class AssetGroupSerializer(serializers.ModelSerializer): class Meta: model = AssetGroup @@ -51,3 +52,6 @@ class IDCViewSet(viewsets.ReadOnlyModelViewSet): queryset = IDC.objects.all() serializer_class = IDCSerializer +class AssetListUpdateApi(BulkDeleteApiMixin, ListBulkCreateUpdateDestroyAPIView): + queryset = Asset.objects.all() + serializer_class = AssetBulkUpdateSerializer \ No newline at end of file diff --git a/apps/assets/templates/assets/asset_list.html b/apps/assets/templates/assets/asset_list.html index c9d7f298f..25ae809ab 100644 --- a/apps/assets/templates/assets/asset_list.html +++ b/apps/assets/templates/assets/asset_list.html @@ -1,21 +1,26 @@ {% extends '_base_list.html' %} {% load i18n %} +{% load static %} {% load common_tags %} {% block custom_head_css_js %} + + + {% endblock %} {% block content_left_head %} @@ -59,6 +64,7 @@ div.dataTables_wrapper div.dataTables_filter { {% block table_head %} + id {% trans 'Hostname' %} {% trans 'IP' %} {% trans 'Port' %} @@ -74,6 +80,7 @@ div.dataTables_wrapper div.dataTables_filter { + {{ asset.id }} {{ asset.hostname }} @@ -92,30 +99,36 @@ div.dataTables_wrapper div.dataTables_filter { {% trans 'Update' %} - {% trans 'Delete' %} + {% trans 'Delete' %} {% endfor %} {% endblock %} {% block content_bottom_left %} - +{% include "assets/_asset_bulk_update_modal.html" %} + + + {% endblock %} {% block custom_foot_js %} @@ -126,8 +139,10 @@ div.dataTables_wrapper div.dataTables_filter { if(tag_on.length > 0){ oDiv.style.display = "block"; } + }; + function tagShow() { var oDiv = document.getElementById("ydxbd"); if (oDiv.style.display == 'none'){ @@ -135,8 +150,37 @@ div.dataTables_wrapper div.dataTables_filter { }else{ oDiv.style.display = "none"; } - }; + };//onload; + + $(document).ready(function(){ + + + + $('#modal').modal({ + show: false, + backdrop: 'static', + keyboard: 'false', + remote:"{% url 'assets:asset-modal-update' %}", + }); + + //$('#modal').on('shown.bs.modal',function(){ + $('#asset_bulk_update_modal').on('shown.bs.modal',function(){ + //alert('当弹窗完全加载完后,再触发;') + $('.select2').select2(); + $("#tags").select2({ + tags: true, + maximumSelectionLength: 8, //最多能够选择的个数 + closeOnSelect: false + }); + }); + + + $('#asset_bulk_update_modal').modal({ + show: false, + backdrop: 'static', + keyboard: 'false', + }); var oTags = document.getElementById("ydxbd"); var table = $('#editable').DataTable({ "aLengthMenu": [[10, 25, 50, -1], ["10", "25", "50", "all"]], @@ -145,7 +189,7 @@ div.dataTables_wrapper div.dataTables_filter { "aoColumnDefs": [ { "bSortable": false, "aTargets": [ 0 ] }], "bAutoWidth": false, "language": {"url": "/static/js/plugins/dataTables/i18n/zh-hans.json"}, - "dom": '<"custom">lftip', + "dom": '<"custom"fl>tip', "initComplete": function() { //alert( 'DataTables has finished its initialisation.' ); $('#editable_length').before(" {% trans 'Create asset' %}   "); @@ -156,6 +200,7 @@ div.dataTables_wrapper div.dataTables_filter { {data: "id"}, {data: "name"}, {data: "ip"}, + {data: "port"}, {data: "type"}, {data: "group"}, {data: "dp"}, @@ -163,7 +208,7 @@ div.dataTables_wrapper div.dataTables_filter { ] }); //将ID列隐藏 - //table.column('1').visible(false); + table.column('1').visible(false); $('#editable tbody').on( 'click', 'tr', function () { //alert($(this).hasClass('selected')); if($(this).hasClass('selected')){ @@ -174,25 +219,90 @@ div.dataTables_wrapper div.dataTables_filter { this.children[0].children[0].checked=1; }; }); - $('#close-btn').on('click',function(){ - $('#modal').modal('hide'); - }); - $('#save-btn').on('click',function(){ + + + $('#btn_bulk_update').on('click',function(){ // alert( table.rows('.selected').data().length +' row(s) selected' ); - var d = table.rows('.selected').data(); - var size = d.length; - document.getElementById('add_asset').value = size; var column2 = table.rows('.selected').data(); - $("#asset_sed").find("input[name='assets']").remove(); + var id_list = []; + var plain_id_list = []; + var the_url = "{% url 'assets:asset-bulk-update-api' %}"; for(var i=0;i"); + id_list.push({id: column2[i].id,hostname:column2[i].ip}); + plain_id_list.push(parseInt(column2[i].id)); + }; + + var url_delete = the_url + '?id__in=' + JSON.stringify(plain_id_list); + //APIUpdateAttr({url: url_delete, method: 'DELETE', success: success, error: fail}); + function doDelete(){ + swal({ + title: "{% trans 'Are you sure?' %}", + text: "{% trans 'This will delete the selected assets !!!' %}", + type: "warning", + showCancelButton: true, + cancelButtonText: 'Cancel', + confirmButtonColor: "#DD6B55", + confirmButtonText: "{% trans 'Confirm' %}", + closeOnConfirm: false + }, function () { + $.ajax({ + type:'DELETE', + url:url_delete, + data:{ + data:'true', + }, + success:function(){ + window.location.reload(); + } + }); + }); + }; + + function doUpdate() { + $('#asset_bulk_update_modal').modal('show'); + // $('#modal').modal('show'); + // alert(JSON.stringify(id_list)); + }; + + var action = $('#slct_bulk_update option:selected').val(); + if (id_list.length === 0) { + action = 'default'; + }; + switch(action) { + case 'deactive': + alert(action+"未完成"); + //doDeactive(); + break; + case 'delete': + doDelete(); + break; + case 'update': + doUpdate(); + break; + case 'export': + alert(action+"未完成"); + break; + default: + swal({ + title: "未选择任何元素", + text: "Please..." + }); + break; } - $('#modal').modal('hide'); - }); + });//button + + $('#btn_asset_bulk_update').on('click',function(){ + + });//button + + + + }); //$(document).ready + + + var bCheck = 1; function checkAll(){ if(bCheck){ @@ -213,5 +323,19 @@ div.dataTables_wrapper div.dataTables_filter { bCheck = 1; }; }; + + + + + + + + + + + + + + {% endblock %} \ No newline at end of file diff --git a/apps/assets/templates/assets/asset_tags_list.html b/apps/assets/templates/assets/asset_tags_list.html index 499ab9288..cd8cdf559 100644 --- a/apps/assets/templates/assets/asset_tags_list.html +++ b/apps/assets/templates/assets/asset_tags_list.html @@ -2,7 +2,7 @@ {% load i18n %} {% load common_tags %} {% block content_left_head %} - {% trans "Create tag" %} + {% trans "Create tag" %} {% endblock %} {% block table_head %} diff --git a/apps/assets/urls.py b/apps/assets/urls.py index ca6493cf7..50c4669cb 100644 --- a/apps/assets/urls.py +++ b/apps/assets/urls.py @@ -21,6 +21,7 @@ urlpatterns = [ url(r'^asset/(?P[0-9]+)/update', views.AssetUpdateView.as_view(), name='asset-update'), url(r'^asset/(?P[0-9]+)/delete$', views.AssetDeleteView.as_view(), name='asset-delete'), url(r'^asset-modal$', views.AssetModalListView.as_view(), name='asset-modal-list'), + url(r'^asset-modal-update$', views.AssetModalCreateView.as_view(), name='asset-modal-update'), # Resource asset group url url(r'^asset-group$', views.AssetGroupListView.as_view(), name='asset-group-list'), @@ -65,6 +66,7 @@ urlpatterns = [ urlpatterns += [ #json url(r'^v1/assets/$', api.AssetViewSet.as_view({'get':'list'}), name='assets-list-api'), + url(r'^v1/assets_bulk/$', api.AssetListUpdateApi.as_view(), name='asset-bulk-update-api'), url(r'^v1/idc/$', api.IDCViewSet.as_view({'get':'list'}), name='idc-list-json'), ] diff --git a/apps/assets/views.py b/apps/assets/views.py index e60f063e8..edda4172d 100644 --- a/apps/assets/views.py +++ b/apps/assets/views.py @@ -46,6 +46,8 @@ class AssetListView(AdminUserRequiredMixin, ListView): return super(AssetListView, self).get_context_data(**kwargs) + + class AssetCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,CreateView): model = Asset tag_type = 'asset' @@ -73,8 +75,12 @@ class AssetCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,CreateView): return super(AssetCreateView, self).get_context_data(**kwargs) - - +class AssetModalCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,CreateView): + model = Asset + # tag_type = 'asset' + form_class = AssetCreateForm + template_name = 'assets/asset_modal_update.html' + success_url = reverse_lazy('assets:asset-list') class AssetUpdateView(AdminUserRequiredMixin,UpdateAssetTagsMiXin,UpdateView): model = Asset diff --git a/apps/static/css/jumpserver.css b/apps/static/css/jumpserver.css index f61cdca4f..0881bb3da 100644 --- a/apps/static/css/jumpserver.css +++ b/apps/static/css/jumpserver.css @@ -204,7 +204,7 @@ table.dataTable tbody td.selected td i.text-navy padding-top: 15px; margin-bottom: 15px; display: block; - /*background: #ccc;*/ + background: #f4f4f4; padding-left: 10px; } .mar {