Skip to content

Commit

Permalink
添加资产导出功能
Browse files Browse the repository at this point in the history
  • Loading branch information
welliamcao committed Jun 8, 2018
1 parent ffd2810 commit fb42bff
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 96 deletions.
1 change: 1 addition & 0 deletions OpsManage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Meta:
("can_change_assets", "更改资产权限"),
("can_add_assets", "添加资产权限"),
("can_delete_assets", "删除资产权限"),
("can_dumps_assets", "导出资产权限"),
)
verbose_name = '总资产表'
verbose_name_plural = '总资产表'
Expand Down
57 changes: 51 additions & 6 deletions OpsManage/templates/assets/assets_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ <h1 class="page-header"><i class="fa fa-dashboard"></i>资产列表</h1>
<div class="panel-heading">
<div class="btn-group btn-group-xs pull-right">
<button type="button" class="btn btn-default"><a href="#" data-toggle="modal" data-target="#myAssetsImportModal"><i class="fa fa-pencil fa-fw"></i>批量导入</a></button>
<button type="button" class="btn btn-default"><a href="/assets_search"><i class="fa fa-search fa-fw"></i>高级搜索</a></button>
<button type="button" class="btn btn-default" onclick="fcDumpsAssetsData(this)"><i class="fa fa-refresh fa-fw"></i>导出资产</button>
<button type="button" class="btn btn-default" onclick="updateAllAssets(this,'update')"><i class="fa fa-refresh fa-fw"></i>批量更新</button>
<button type="button" class="btn btn-default" onclick="updateAllAssets(this,'delete')"><i class="fa fa-trash-o fa-fw"></i>批量删除</button>
</div>
Expand Down Expand Up @@ -883,25 +883,70 @@ <h4 class="modal-title" id="myModalLabel">Excel文件批量导入资产</h4>
}


var downLoadFile = function (options) {
var config = $.extend(true, { method: 'post' }, options);
var $iframe = $('<iframe id="down-file-iframe"/>');
var $form = $("<form target='down-file-iframe' method=" + config.method + " />");
$form.attr('action', config.url);
for (var key in config.data) {
console.log('<input type="hidden" name="' + key + '" value="' + config.data[key] + '"/>');
$form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '"/>');
}
$form.append("{% csrf_token %}");
$iframe.append($form);
$(document.body).append($iframe);
$form[0].submit();
$iframe.remove();
}

function fcDumpsAssetsData(obj){
var btnObj = $(obj);
btnObj.attr('disabled',true);
var serverId = [];
var url = "/assets/batch/dumps/";
$.each($("input[name='ckbox']:checkbox"),function(){
if(this.checked){
serverId.push($(this).val());
}
})
if (serverId.length > 0){
var data = {
'assetsIds':serverId,
};
downLoadFile({ //调用下载方法
url:url,data:data
});
}
else{
window.wxc.xcConfirm("至少选择一个资产~", window.wxc.xcConfirm.typeEnum.error);
}
btnObj.removeAttr('disabled');
}


function updateAllAssets(obj,model){
var btnObj = $(obj);
btnObj.attr('disabled',true);
var serverId = [];
var qcheck=document.getElementsByName("ckbox");
for (var i = 0; i < qcheck.length; i++)
{
for (var i = 0; i < qcheck.length; i++){
if(qcheck[i].checked==true){
serverId.push(qcheck[i].value);
}
}
}
if (model=='update'){
var url = '/assets/batch/update/'
}
else if (model=='delete'){
var url = '/assets/batch/update/'
}
if (serverId.length > 0){
$.ajax({
type: 'POST',
url: '/assets/batch/',
url: url,
dataType:"json",
data:{
'assetsIds':serverId,
'model':model
},
success:function(response){
var sip = '';
Expand Down
4 changes: 3 additions & 1 deletion OpsManage/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
url(r'^assets_import/',assets.assets_import),
url(r'^assets_search/',assets.assets_search),
url(r'^assets_server/',assets.assets_server),
url(r'^assets/batch/',assets.assets_batch),
url(r'^assets/batch/update/',assets.assets_update),
url(r'^assets/batch/delete/',assets.assets_delete),
url(r'^assets/batch/dumps/',assets.assets_dumps),
url(r'^assets/groups/(?P<id>[0-9]+)/$',assets.assets_groups),
url(r'^cron_add',cron.cron_add),
url(r'^cron_list/(?P<page>[0-9]+)/$',cron.cron_list),
Expand Down
45 changes: 45 additions & 0 deletions OpsManage/utils/execl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python
# _#_ coding:utf-8 _*_
import xlwt

class CellWriter(object):
def __init__(self,name):
self.file_name = name
self.workbook = xlwt.Workbook(encoding='utf-8')
# for sh in args:
# self.worksheet = self.workbook.add_sheet(sh)

def fontStyle(self):
font = xlwt.Font()
font.name = u"微软雅黑"
# font.height = 20 * 12
return font

def borderStyle(self):
borders = xlwt.Borders()
borders.left = 1
borders.right = 1
borders.top = 1
borders.bottom = 1
return borders

def writeBanner(self,sheetName,bList):
for ds in bList:
sheetName.write(0, bList.index(ds), ds, self.bodySttle(pattern=5))

def bodySttle(self,pattern=None):
style = xlwt.XFStyle()
style.alignment.horz = 2
style.alignment.vert = 1
style.alignment.wrap = 1
if pattern:
pat = xlwt.Pattern()
pat.pattern = xlwt.Pattern.SOLID_PATTERN
pat.pattern_fore_colour = pattern
style.pattern = pat
style.font = self.fontStyle()
style.borders = self.borderStyle()
return style

def save(self):
self.workbook.save(self.file_name)
Loading

0 comments on commit fb42bff

Please sign in to comment.