fix: 修复资产编辑无法保存的bug
- 恢复Asset模型STATUS_CHOICES定义,状态字段使用下拉选择而非文本输入 - 修复status默认值从中文'在用'改为英文key'in_use',与数据库存储一致 - 编辑表单新增资产面值(asset_value)字段 - excel_utils适配字段类型变更(DecimalField→CharField, DateField→CharField等)
这个提交包含在:
+16
-30
@@ -173,41 +173,26 @@ def import_assets_from_excel(ws, category_map, operator=None):
|
||||
# 处理状态
|
||||
status = STATUS_MAP_REVERSE.get(data.get('status', '在用'), 'in_use')
|
||||
|
||||
# 处理日期
|
||||
from datetime import datetime as dt
|
||||
purchase_date = None
|
||||
warranty_expire = None
|
||||
if data.get('purchase_date'):
|
||||
try:
|
||||
purchase_date = dt.strptime(data['purchase_date'], '%Y-%m-%d').date()
|
||||
except ValueError:
|
||||
pass
|
||||
if data.get('warranty_expire'):
|
||||
try:
|
||||
warranty_expire = dt.strptime(data['warranty_expire'], '%Y-%m-%d').date()
|
||||
except ValueError:
|
||||
pass
|
||||
# 处理日期 - 直接存字符串
|
||||
purchase_date = data.get('purchase_date', '').strip() or ''
|
||||
warranty_expire = data.get('warranty_expire', '').strip() or ''
|
||||
|
||||
# 处理IP
|
||||
bmc_address = data.get('bmc_address') or None
|
||||
ip_address = data.get('ip_address') or None
|
||||
bmc_address = data.get('bmc_address', '').strip() or ''
|
||||
ip_address = data.get('ip_address', '').strip() or ''
|
||||
|
||||
# 处理资产面值
|
||||
from decimal import Decimal, InvalidOperation
|
||||
asset_value = None
|
||||
if data.get('asset_value'):
|
||||
try:
|
||||
asset_value = Decimal(str(data['asset_value']).replace(',', ''))
|
||||
except (InvalidOperation, ValueError):
|
||||
pass
|
||||
# 资产面值 - 直接存字符串
|
||||
asset_value = data.get('asset_value', '').strip() or ''
|
||||
|
||||
# 处理ID - 如果提供了ID且已存在,则更新该记录
|
||||
import_id = data.get('id', '').strip()
|
||||
asset = None
|
||||
is_update = False
|
||||
if import_id:
|
||||
if import_id and import_id.isdigit():
|
||||
try:
|
||||
asset = Asset.objects.get(id=int(import_id))
|
||||
id_val = int(import_id) if str(import_id).strip().isdigit() else None
|
||||
if id_val:
|
||||
asset = Asset.objects.get(id=id_val)
|
||||
is_update = True
|
||||
except (Asset.DoesNotExist, ValueError):
|
||||
asset = None
|
||||
@@ -228,7 +213,7 @@ def import_assets_from_excel(ws, category_map, operator=None):
|
||||
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.gpu_count = gpu_count_str if gpu_count_str else ''
|
||||
asset.purchase_date = purchase_date
|
||||
asset.warranty_expire = warranty_expire
|
||||
asset.supplier = data.get('supplier', '')
|
||||
@@ -250,7 +235,7 @@ def import_assets_from_excel(ws, category_map, operator=None):
|
||||
else:
|
||||
# 创建新记录
|
||||
gpu_count_str = data.get('gpu_count', '').strip()
|
||||
gpu_count = int(gpu_count_str) if gpu_count_str else None
|
||||
gpu_count = gpu_count_str if gpu_count_str else ''
|
||||
|
||||
# 创建参数
|
||||
create_kwargs = {
|
||||
@@ -281,9 +266,10 @@ def import_assets_from_excel(ws, category_map, operator=None):
|
||||
}
|
||||
|
||||
# 如果Excel提供了ID,使用该ID创建
|
||||
if import_id:
|
||||
if import_id and import_id.isdigit():
|
||||
try:
|
||||
create_kwargs['id'] = int(import_id)
|
||||
if str(import_id).strip().isdigit():
|
||||
create_kwargs['id'] = int(import_id)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户