Skip to content

Commit

Permalink
优化优化:资产列表支持分页查询
Browse files Browse the repository at this point in the history
  • Loading branch information
welliamcao committed Nov 20, 2022
1 parent 0f0d983 commit 4e120d8
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 61 deletions.
14 changes: 11 additions & 3 deletions apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,26 @@ class Meta:
class AssetsSerializer(serializers.ModelSerializer):
crontab_total = serializers.SerializerMethodField(read_only=True,required=False)
database_total = serializers.SerializerMethodField(read_only=True,required=False)
detail = serializers.SerializerMethodField(read_only=True,required=False)
class Meta:
model = Assets
fields = ('id','assets_type','name','sn','buy_time','expire_date',
'buy_user','management_ip','manufacturer','provider','mark',
'model','status','put_zone','group','project',
'crontab_total','cabinet','database_total',)
'crontab_total','cabinet','database_total','detail')

def get_crontab_total(self, obj):
return [ cron.id for cron in obj.crontab_total.all() ] #返回列表

def get_database_total(self,obj):
return [ db.id for db in obj.database_total.all() ] #返回列表
def get_database_total(self, obj):
return [ db.id for db in obj.database_total.all() ] #返回列表

def get_detail(self, obj):
if obj.assets_type in ["vmser","server"]:
return Server_Assets.objects.get(assets=obj).to_json()
else:
return Network_Assets.objects.get(assets=obj).to_json()


class ServerSerializer(serializers.ModelSerializer):
assets = AssetsSerializer(required=False)
Expand Down
30 changes: 17 additions & 13 deletions apps/api/views/assets_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,26 @@ def raid_detail(request, id,format=None):


class AssetList(APIView,DataHandle):

def get(self,request,*args,**kwargs):
query_params = dict()
for ds in request.query_params.keys():
if ds == 'assets_type' and request.query_params.get(ds) == 'ser':
query_params["assets_type__in"] = ["vmser","server"]
continue
query_params[ds] = request.query_params.get(ds)
# query_params = dict()
# for ds in request.query_params.keys():
# if ds == 'assets_type' and request.query_params.get(ds) == 'ser':
# query_params["assets_type__in"] = ["vmser","server"]
# continue
# if ds == 'offset':continue
# query_params[ds] = request.query_params.get(ds)
if request.user.is_superuser:
snippets = Assets.objects.filter(**query_params)
# snippets = Assets.objects.filter(**query_params)
snippets = Assets.objects.all()
else:
snippets = [ ds.assets for ds in User_Server.objects.filter(user=request.user,assets__in=[ ds.id for ds in Assets.objects.filter(**query_params) ]) ]
dataList = []
for assets in snippets:
dataList.append(assets.to_json())
return Response(dataList)
# snippets = [ ds.assets for ds in User_Server.objects.filter(user=request.user,assets__in=[ ds.id for ds in Assets.objects.filter(**query_params) ])]
snippets = [ ds.assets for ds in User_Server.objects.filter(user=request.user)]
page = serializers.PageConfig() # 注册分页
page_assets_list = page.paginate_queryset(queryset=snippets, request=request, view=self)
ser = serializers.AssetsSerializer(instance=page_assets_list, many=True)
return page.get_paginated_response(ser.data)


def post(self,request,*args,**kwargs):
if not request.user.has_perm('asset.assets_add_assets'):
Expand Down
4 changes: 2 additions & 2 deletions apps/asset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def get(self, request, *args, **kwagrs):
class AssetsList(LoginRequiredMixin,AssetsBase,View):
login_url = '/login/'
@method_decorator_adaptor(permission_required, "asset.assets_read_assets","/403/")
def get(self, request, *args, **kwagrs):
return render(request, 'assets/assets_list.html',{"user":request.user,"assets":self.base(),"assetsList":self.assetsList()})
def get(self, request, *args, **kwagrs):
return render(request, 'assets/assets_list.html',{"user":request.user,"assets":self.base()})


class AssetsTree(LoginRequiredMixin,AssetsBase,View):
Expand Down
49 changes: 39 additions & 10 deletions static/js/account/center.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

function InitDataTableAssets(tableId,dataList,buttons,columns,columnDefs){
function InitDataTableAssets(tableId,data,buttons,columns,columnDefs, page){
oOverviewTable =$('#'+tableId).dataTable({
"dom": "Bfrtip",
"buttons":buttons,
"bScrollCollapse": false,
"bRetrieve": true,
"destroy": true,
"data": dataList,
"data": data["results"],
"columns": columns,
"columnDefs" :columnDefs,
"language" : language,
Expand All @@ -17,7 +17,17 @@ function InitDataTableAssets(tableId,dataList,buttons,columns,columnDefs){
},
"order": [[ 0, "ase" ]],
"autoWidth": false
});
});
if (data['next']){
$("button[name='"+ page +"_page_next']").attr("disabled", false).val(data['next']);
}else{
$("button[name='"+ page +"_page_next']").attr("disabled", true).val();
}
if (data['previous']){
$("button[name='"+ page +"_page_previous']").attr("disabled", false).val(data['next']);
}else{
$("button[name='"+ page +"_page_previous']").attr("disabled", true).val();
}
}

function makeRandomId() {
Expand Down Expand Up @@ -129,21 +139,31 @@ function format (dataList) {
}


function RefreshAssetsTable(tableId, urlData){
function RefreshAssetsTable(tableId, urlData, page){
$.getJSON(urlData, null, function( dataList )
{
table = $('#'+tableId).dataTable();
oSettings = table.fnSettings();

table.fnClearTable(this);

for (var i=0; i<dataList.length; i++)
for (var i=0; i<dataList["results"].length; i++)
{
table.oApi._fnAddData(oSettings, dataList[i]);
table.oApi._fnAddData(oSettings, dataList["results"][i]);
}

oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
table.fnDraw();
if (dataList['next']){
$("button[name='"+ page +"_page_next']").attr("disabled", false).val(dataList['next']);
}else{
$("button[name='"+ page +"_page_next']").attr("disabled", true).val();
}
if (dataList['previous']){
$("button[name='"+ page +"_page_previous']").attr("disabled", false).val(dataList['previous']);
}else{
$("button[name='"+ page +"_page_previous']").attr("disabled", true).val();
}
});
}

Expand Down Expand Up @@ -190,7 +210,7 @@ function batchManageChoiceAssets(dataList, title, content, type){
styling: 'bootstrap3',
delay: 18000
});
RefreshAssetsTable("assetsListTable","/api/assets/")
RefreshAssetsTable("assetsListTable","/api/assets/", "page")
}
else{
new PNotify({
Expand Down Expand Up @@ -227,6 +247,15 @@ function batchManageChoiceAssets(dataList, title, content, type){
}

$(document).ready(function() {

$("button[name^='assets_page_']").on("click", function(){
var url = $(this).val();
$(this).attr("disabled",true);
if (url.length){
RefreshAssetsTable('assetsListTable', url, 'assets');
}
$(this).attr('disabled',false);
});

if($("#assetsListTable").length){
function makeAssetsTableList(dataList){
Expand Down Expand Up @@ -355,7 +384,7 @@ $(document).ready(function() {
}
} ,
]
InitDataTableAssets('assetsListTable',dataList,buttons,columns,columnDefs);
InitDataTableAssets('assetsListTable',dataList,buttons,columns,columnDefs,"assets");
}
makeAssetsTableList(requests("get","/api/assets/"))
$('#assetsListTable tbody').on('click', 'td.details-control', function () {
Expand Down Expand Up @@ -415,7 +444,7 @@ $(document).ready(function() {
type: 'success',
styling: 'bootstrap3'
});
RefreshAssetsTable("assetsListTable","/api/assets/")
RefreshAssetsTable("assetsListTable","/api/assets/","assets")
}
});
},
Expand Down Expand Up @@ -459,7 +488,7 @@ $(document).ready(function() {
type: 'success',
styling: 'bootstrap3'
});
RefreshAssetsTable("assetsListTable","/api/assets/")
RefreshAssetsTable("assetsListTable","/api/assets/","assets")
}else{
new PNotify({
title: 'Ops Failed!',
Expand Down
78 changes: 68 additions & 10 deletions static/js/assets/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@
}
}

function InitDataTable(tableId,dataList,buttons,columns,columnDefs){
function InitDataTable(tableId,dataList,buttons,columns,columnDefs,page){
oOverviewTable =$('#'+tableId).dataTable({
"dom": "Bfrtip",
"buttons":buttons,
"bScrollCollapse": false,
"bRetrieve": true,
"destroy": true,
"data": dataList,
"data": dataList.results,
"columns": columns,
"columnDefs" :columnDefs,
"language" : language,
Expand All @@ -229,10 +229,48 @@
},
"order": [[ 0, "ase" ]],
"autoWidth": false
});
});
if (dataList['next']){
$("button[name='"+ page +"_page_next']").attr("disabled", false).val(dataList['next']);
}else{
$("button[name='"+ page +"_page_next']").attr("disabled", true).val();
}
if (dataList['previous']){
$("button[name='"+ page +"_page_previous']").attr("disabled", false).val(dataList['next']);
}else{
$("button[name='"+ page +"_page_previous']").attr("disabled", true).val();
}
}

function RefreshTable(tableId, urlData, page){
$.getJSON(urlData, null, function( dataList )
{
table = $('#'+tableId).dataTable();
oSettings = table.fnSettings();

table.fnClearTable(this);

for (var i=0; i<dataList["results"].length; i++)
{
table.oApi._fnAddData(oSettings, dataList["results"][i]);
}

oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
table.fnDraw();
if (dataList['next']){
$("button[name='"+ page +"_page_next']").attr("disabled", false).val(dataList['next']);
}else{
$("button[name='"+ page +"_page_next']").attr("disabled", true).val();
}
if (dataList['previous']){
$("button[name='"+ page +"_page_previous']").attr("disabled", false).val(dataList['previous']);
}else{
$("button[name='"+ page +"_page_previous']").attr("disabled", true).val();
}
});
}

function RefreshAssetsTable(tableId, urlData){
function RefreshAssetsTable(tableId, urlData, page){
$.getJSON(urlData, null, function( dataList ){
table = $('#'+tableId).dataTable();
oSettings = table.fnSettings();
Expand All @@ -245,6 +283,16 @@
}
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
table.fnDraw();
if (dataList['next']){
$("button[name='"+ page +"_page_next']").attr("disabled", false).val(dataList['next']);
}else{
$("button[name='"+ page +"_page_next']").attr("disabled", true).val();
}
if (dataList['previous']){
$("button[name='"+ page +"_page_previous']").attr("disabled", false).val(dataList['previous']);
}else{
$("button[name='"+ page +"_page_previous']").attr("disabled", true).val();
}
});
}

Expand Down Expand Up @@ -280,7 +328,7 @@
styling: 'bootstrap3',
delay: 18000
});
RefreshAssetsTable("assetsListTable", "/api/assets/")
RefreshAssetsTable("assetsListTable", "/api/assets/", "assets")
}
else{
new PNotify({
Expand Down Expand Up @@ -352,7 +400,17 @@
},
});
};
if ($("#assetsListTable").length) {
if ($("#assetsListTable").length) {

$("button[name^='assets_page_']").on("click", function(){
var url = $(this).val();
$(this).attr("disabled",true);
if (url.length){
RefreshTable('assetsListTable', url, 'assets');
}
$(this).attr('disabled',false);
});

function makeAssetsTableList(dataList){
var columns = [
{
Expand Down Expand Up @@ -453,7 +511,7 @@
}
}
]
InitDataTable('assetsListTable',dataList,buttons,columns,columnDefs);
InitDataTable('assetsListTable',dataList,buttons,columns,columnDefs,'assets');
}
makeAssetsTableList(requests("get","/api/assets/"))
// Add event listener for opening and closing details
Expand Down Expand Up @@ -1847,8 +1905,7 @@
var value = $(this).val();
if (key != "csrfmiddlewaretoken"){
parameter[key] = value;
}

}
})

var count = 0;
Expand Down Expand Up @@ -2087,4 +2144,5 @@
});
btnObj.removeAttr('disabled');
}
});
});

Loading

0 comments on commit 4e120d8

Please sign in to comment.