feat: 新增业务类型、显卡信息字段;优化列表列宽和字符截断;删除是否带卡列
This commit is contained in:
+93
-33
@@ -8,6 +8,7 @@ from .models import Category
|
||||
|
||||
# Excel列定义
|
||||
EXPORT_COLUMNS = [
|
||||
('id', 'ID', 8),
|
||||
('asset_number', '资产编号', 18),
|
||||
('name', '设备名称', 20),
|
||||
('category', '设备分类', 12),
|
||||
@@ -20,12 +21,15 @@ EXPORT_COLUMNS = [
|
||||
('cabinet_position', '机柜位置', 10),
|
||||
('bmc_address', 'BMC地址', 16),
|
||||
('ip_address', 'IP地址', 16),
|
||||
('gpu_type', '显卡类型', 15),
|
||||
('gpu_count', '卡数', 6),
|
||||
('purchase_date', '采购日期', 12),
|
||||
('warranty_expire', '质保到期', 12),
|
||||
('supplier', '供应商', 15),
|
||||
('responsible_person', '负责人', 10),
|
||||
('department', '使用部门', 15),
|
||||
('user', '使用人', 10),
|
||||
('business_type', '业务类型', 15),
|
||||
('status', '状态', 8),
|
||||
('remark', '备注', 30),
|
||||
]
|
||||
@@ -65,7 +69,9 @@ def export_assets_to_excel(queryset):
|
||||
# 写数据
|
||||
for row_idx, asset in enumerate(queryset, 2):
|
||||
for col_idx, (field, _, _) in enumerate(EXPORT_COLUMNS, 1):
|
||||
if field == 'category':
|
||||
if field == 'id':
|
||||
value = asset.id
|
||||
elif field == 'category':
|
||||
value = str(asset.category) if asset.category else ''
|
||||
elif field == 'status':
|
||||
value = STATUS_MAP.get(asset.status, asset.status)
|
||||
@@ -102,9 +108,10 @@ def generate_import_template():
|
||||
|
||||
# 示例数据行
|
||||
example_data = [
|
||||
'IT-2024-0001', '测试服务器', '服务器', 'Dell', 'PowerEdge R740',
|
||||
'1', 'IT-2024-0001', '测试服务器', '服务器', 'Dell', 'PowerEdge R740',
|
||||
'50000.00', 'ABC123456', '3楼机房A区', 'A01', 'U10-U15', '192.168.1.200',
|
||||
'192.168.1.100', '2024-01-15', '2027-01-15', '戴尔科技', '张三', '研发部', '李四', '在用', '测试备注'
|
||||
'192.168.1.100', 'NVIDIA A100', '8', '2024-01-15', '2027-01-15', '戴尔科技',
|
||||
'张三', '研发部', '李四', 'AI训练', '在用', '测试备注'
|
||||
]
|
||||
for col_idx, value in enumerate(example_data, 1):
|
||||
cell = ws.cell(row=2, column=col_idx, value=value)
|
||||
@@ -193,37 +200,90 @@ def import_assets_from_excel(ws, category_map, operator=None):
|
||||
except (InvalidOperation, ValueError):
|
||||
pass
|
||||
|
||||
asset = Asset.objects.create(
|
||||
asset_number=asset_number,
|
||||
name=data.get('name', ''),
|
||||
category=category,
|
||||
brand=data.get('brand', ''),
|
||||
model=data.get('model', ''),
|
||||
asset_value=asset_value,
|
||||
serial_number=data.get('serial_number', ''),
|
||||
location=data.get('location', ''),
|
||||
cabinet=data.get('cabinet', ''),
|
||||
cabinet_position=data.get('cabinet_position', ''),
|
||||
bmc_address=bmc_address,
|
||||
ip_address=ip_address,
|
||||
purchase_date=purchase_date,
|
||||
warranty_expire=warranty_expire,
|
||||
supplier=data.get('supplier', ''),
|
||||
responsible_person=data.get('responsible_person', ''),
|
||||
department=data.get('department', ''),
|
||||
user=data.get('user', ''),
|
||||
status=status,
|
||||
remark=data.get('remark', ''),
|
||||
created_by=operator,
|
||||
)
|
||||
# 处理ID - 如果提供了ID且已存在,则更新该记录
|
||||
import_id = data.get('id', '').strip()
|
||||
asset = None
|
||||
is_update = False
|
||||
if import_id:
|
||||
try:
|
||||
asset = Asset.objects.get(id=int(import_id))
|
||||
is_update = True
|
||||
except (Asset.DoesNotExist, ValueError):
|
||||
asset = None
|
||||
|
||||
AssetChangeLog.objects.create(
|
||||
asset=asset,
|
||||
asset_number=asset.asset_number,
|
||||
action='import',
|
||||
description=f'通过Excel导入创建',
|
||||
operator=operator,
|
||||
)
|
||||
if asset:
|
||||
# 更新已有记录
|
||||
asset.asset_number = asset_number
|
||||
asset.name = data.get('name', '')
|
||||
asset.category = category
|
||||
asset.brand = data.get('brand', '')
|
||||
asset.model = data.get('model', '')
|
||||
asset.asset_value = asset_value
|
||||
asset.serial_number = data.get('serial_number', '')
|
||||
asset.location = data.get('location', '')
|
||||
asset.cabinet = data.get('cabinet', '')
|
||||
asset.cabinet_position = data.get('cabinet_position', '')
|
||||
asset.bmc_address = bmc_address
|
||||
asset.ip_address = ip_address
|
||||
asset.gpu_type = data.get('gpu_type', '')
|
||||
gpu_count_str = data.get('gpu_count', '').strip()
|
||||
asset.gpu_count = int(gpu_count_str) if gpu_count_str else None
|
||||
asset.purchase_date = purchase_date
|
||||
asset.warranty_expire = warranty_expire
|
||||
asset.supplier = data.get('supplier', '')
|
||||
asset.responsible_person = data.get('responsible_person', '')
|
||||
asset.department = data.get('department', '')
|
||||
asset.user = data.get('user', '')
|
||||
asset.business_type = data.get('business_type', '')
|
||||
asset.status = status
|
||||
asset.remark = data.get('remark', '')
|
||||
asset.save()
|
||||
|
||||
AssetChangeLog.objects.create(
|
||||
asset=asset,
|
||||
asset_number=asset.asset_number,
|
||||
action='import',
|
||||
description=f'通过Excel导入更新(ID:{asset.id})',
|
||||
operator=operator,
|
||||
)
|
||||
else:
|
||||
# 创建新记录
|
||||
gpu_count_str = data.get('gpu_count', '').strip()
|
||||
gpu_count = int(gpu_count_str) if gpu_count_str else None
|
||||
asset = Asset.objects.create(
|
||||
asset_number=asset_number,
|
||||
name=data.get('name', ''),
|
||||
category=category,
|
||||
brand=data.get('brand', ''),
|
||||
model=data.get('model', ''),
|
||||
asset_value=asset_value,
|
||||
serial_number=data.get('serial_number', ''),
|
||||
location=data.get('location', ''),
|
||||
cabinet=data.get('cabinet', ''),
|
||||
cabinet_position=data.get('cabinet_position', ''),
|
||||
bmc_address=bmc_address,
|
||||
ip_address=ip_address,
|
||||
gpu_type=data.get('gpu_type', ''),
|
||||
gpu_count=gpu_count,
|
||||
purchase_date=purchase_date,
|
||||
warranty_expire=warranty_expire,
|
||||
supplier=data.get('supplier', ''),
|
||||
responsible_person=data.get('responsible_person', ''),
|
||||
department=data.get('department', ''),
|
||||
user=data.get('user', ''),
|
||||
business_type=data.get('business_type', ''),
|
||||
status=status,
|
||||
remark=data.get('remark', ''),
|
||||
created_by=operator,
|
||||
)
|
||||
|
||||
AssetChangeLog.objects.create(
|
||||
asset=asset,
|
||||
asset_number=asset.asset_number,
|
||||
action='import',
|
||||
description=f'通过Excel导入创建',
|
||||
operator=operator,
|
||||
)
|
||||
|
||||
results['success'] += 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user