Initial commit: UPS management system
This commit is contained in:
60
ups_management/split_ups.py
Normal file
60
ups_management/split_ups.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import os
|
||||
import django
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ups_management.settings')
|
||||
django.setup()
|
||||
|
||||
from ups_manager.models import UPSHost, Battery
|
||||
|
||||
print("=== 开始拆分UPS主机记录 ===")
|
||||
|
||||
ups_to_split = UPSHost.objects.filter(quantity__gt=1)
|
||||
print(f"\n找到 {ups_to_split.count()} 条数量大于1的UPS主机记录")
|
||||
|
||||
for ups in ups_to_split:
|
||||
print(f"\n处理: {ups.brand} {ups.model} - {ups.location} - 数量: {ups.quantity}")
|
||||
|
||||
original_quantity = ups.quantity
|
||||
|
||||
ups.quantity = 1
|
||||
ups.save()
|
||||
print(f" 保留第一条记录: IP={ups.ip_address}, 数量=1")
|
||||
|
||||
batteries = list(ups.battery_set.all())
|
||||
battery_per_ups = {}
|
||||
for battery in batteries:
|
||||
if battery.quantity > original_quantity:
|
||||
battery_per_ups[battery] = battery.quantity // original_quantity
|
||||
battery.quantity = battery.quantity // original_quantity
|
||||
battery.save()
|
||||
else:
|
||||
battery_per_ups[battery] = 1
|
||||
|
||||
for i in range(1, original_quantity):
|
||||
new_ups = UPSHost.objects.create(
|
||||
brand=ups.brand,
|
||||
model=ups.model,
|
||||
ip_address=f"{ups.ip_address}-{i+1}",
|
||||
quantity=1,
|
||||
location=ups.location,
|
||||
last_maintenance_date=ups.last_maintenance_date,
|
||||
contact=ups.contact
|
||||
)
|
||||
|
||||
for battery, qty in battery_per_ups.items():
|
||||
Battery.objects.create(
|
||||
brand=battery.brand,
|
||||
model=battery.model,
|
||||
weight=battery.weight,
|
||||
quantity=qty,
|
||||
location=battery.location,
|
||||
install_date=battery.install_date,
|
||||
last_maintenance_date=battery.last_maintenance_date,
|
||||
ups_host=new_ups
|
||||
)
|
||||
|
||||
print(f" 创建第 {i+1} 条记录: IP={new_ups.ip_address}, 数量=1")
|
||||
|
||||
print(f"\n=== 拆分完成 ===")
|
||||
print(f"当前UPS主机总数: {UPSHost.objects.count()}")
|
||||
print(f"当前电池总数: {Battery.objects.count()}")
|
||||
Reference in New Issue
Block a user