|
|
@@ -6,9 +6,12 @@ from django.utils import timezone
|
|
|
from .models import Category
|
|
|
|
|
|
|
|
|
-# Excel列定义
|
|
|
+# Excel列定义(顺序与资产列表一致)
|
|
|
EXPORT_COLUMNS = [
|
|
|
('id', 'ID', 8),
|
|
|
+ ('location', '机房', 20),
|
|
|
+ ('cabinet', '机柜', 10),
|
|
|
+ ('cabinet_position', '机柜位置', 10),
|
|
|
('asset_number', '资产编号', 18),
|
|
|
('name', '设备名称', 20),
|
|
|
('category', '设备分类', 12),
|
|
|
@@ -16,21 +19,18 @@ EXPORT_COLUMNS = [
|
|
|
('model', '型号', 20),
|
|
|
('asset_value', '资产面值', 12),
|
|
|
('serial_number', '序列号', 25),
|
|
|
- ('location', '机房', 20),
|
|
|
- ('cabinet', '机柜', 10),
|
|
|
- ('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),
|
|
|
+ ('user', '维护人', 10),
|
|
|
('business_type', '业务类型', 15),
|
|
|
('status', '状态', 8),
|
|
|
+ ('purchase_date', '采购日期', 12),
|
|
|
+ ('warranty_expire', '质保到期', 12),
|
|
|
+ ('supplier', '供应商', 15),
|
|
|
('remark', '备注', 30),
|
|
|
]
|
|
|
|
|
|
@@ -106,12 +106,13 @@ def generate_import_template():
|
|
|
cell.border = THIN_BORDER
|
|
|
ws.column_dimensions[get_column_letter(col_idx)].width = width
|
|
|
|
|
|
- # 示例数据行
|
|
|
+ # 示例数据行(顺序与EXPORT_COLUMNS一致)
|
|
|
example_data = [
|
|
|
- '1', 'IT-2024-0001', '测试服务器', '服务器', 'Dell', 'PowerEdge R740',
|
|
|
- '50000.00', 'ABC123456', '3楼机房A区', 'A01', 'U10-U15', '192.168.1.200',
|
|
|
- '192.168.1.100', 'NVIDIA A100', '8', '2024-01-15', '2027-01-15', '戴尔科技',
|
|
|
- '张三', '研发部', '李四', 'AI训练', '在用', '测试备注'
|
|
|
+ '1', '3楼机房A区', 'A01', 'U10-U15', 'IT-2024-0001', '测试服务器',
|
|
|
+ '服务器', 'Dell', 'PowerEdge R740', '50000.00', 'ABC123456',
|
|
|
+ '192.168.1.200', '192.168.1.100', 'NVIDIA A100', '8', '张三',
|
|
|
+ '研发部', '李四', 'AI训练', '在用', '2024-01-15', '2027-01-15',
|
|
|
+ '戴尔科技', '测试备注'
|
|
|
]
|
|
|
for col_idx, value in enumerate(example_data, 1):
|
|
|
cell = ws.cell(row=2, column=col_idx, value=value)
|
|
|
@@ -250,32 +251,43 @@ def import_assets_from_excel(ws, category_map, operator=None):
|
|
|
# 创建新记录
|
|
|
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,
|
|
|
- )
|
|
|
+
|
|
|
+ # 创建参数
|
|
|
+ create_kwargs = {
|
|
|
+ '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,
|
|
|
+ }
|
|
|
+
|
|
|
+ # 如果Excel提供了ID,使用该ID创建
|
|
|
+ if import_id:
|
|
|
+ try:
|
|
|
+ create_kwargs['id'] = int(import_id)
|
|
|
+ except ValueError:
|
|
|
+ pass
|
|
|
+
|
|
|
+ asset = Asset.objects.create(**create_kwargs)
|
|
|
|
|
|
AssetChangeLog.objects.create(
|
|
|
asset=asset,
|