92 lines
3.7 KiB
Python
92 lines
3.7 KiB
Python
from django import forms
|
|
from django.forms import inlineformset_factory
|
|
from .models import UPSHost, Battery
|
|
|
|
|
|
class UPSHostForm(forms.ModelForm):
|
|
class Meta:
|
|
model = UPSHost
|
|
fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact']
|
|
labels = {
|
|
'brand': '品牌',
|
|
'model': '型号',
|
|
'ip_address': 'IP地址',
|
|
'quantity': '数量',
|
|
'location': '存放位置',
|
|
'last_maintenance_date': '上次维保时间',
|
|
'contact': '联系人',
|
|
}
|
|
widgets = {
|
|
'brand': forms.TextInput(attrs={'class': 'form-control'}),
|
|
'model': forms.TextInput(attrs={'class': 'form-control'}),
|
|
'ip_address': forms.TextInput(attrs={'class': 'form-control'}),
|
|
'quantity': forms.NumberInput(attrs={'class': 'form-control', 'min': '1'}),
|
|
'location': forms.TextInput(attrs={'class': 'form-control'}),
|
|
'last_maintenance_date': forms.TextInput(attrs={'class': 'form-control datepicker', 'placeholder': '选择日期'}),
|
|
'contact': forms.Select(attrs={'class': 'form-control'}),
|
|
}
|
|
|
|
def clean_last_maintenance_date(self):
|
|
from datetime import datetime
|
|
date_str = self.cleaned_data.get('last_maintenance_date')
|
|
if date_str:
|
|
try:
|
|
return datetime.strptime(date_str, '%Y-%m-%d').date()
|
|
except ValueError:
|
|
pass
|
|
return None
|
|
|
|
|
|
class BatteryForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Battery
|
|
fields = ['brand', 'model', 'weight', 'quantity', 'location', 'install_date', 'last_maintenance_date']
|
|
labels = {
|
|
'brand': '电池品牌',
|
|
'model': '电池型号',
|
|
'weight': '重量(kg)',
|
|
'quantity': '数量',
|
|
'location': '存放位置',
|
|
'install_date': '安装日期',
|
|
'last_maintenance_date': '上次维保时间',
|
|
}
|
|
widgets = {
|
|
'brand': forms.TextInput(attrs={'class': 'form-control form-control-sm'}),
|
|
'model': forms.TextInput(attrs={'class': 'form-control form-control-sm'}),
|
|
'weight': forms.NumberInput(attrs={'class': 'form-control form-control-sm', 'step': '0.1'}),
|
|
'quantity': forms.NumberInput(attrs={'class': 'form-control form-control-sm', 'min': '1'}),
|
|
'location': forms.TextInput(attrs={'class': 'form-control form-control-sm'}),
|
|
'install_date': forms.TextInput(attrs={'class': 'form-control form-control-sm datepicker-battery', 'placeholder': '选择日期'}),
|
|
'last_maintenance_date': forms.TextInput(attrs={'class': 'form-control form-control-sm datepicker-battery', 'placeholder': '选择日期'}),
|
|
}
|
|
|
|
def clean_install_date(self):
|
|
from datetime import datetime
|
|
date_str = self.cleaned_data.get('install_date')
|
|
if date_str:
|
|
try:
|
|
return datetime.strptime(date_str, '%Y-%m-%d').date()
|
|
except ValueError:
|
|
pass
|
|
return None
|
|
|
|
def clean_last_maintenance_date(self):
|
|
from datetime import datetime
|
|
date_str = self.cleaned_data.get('last_maintenance_date')
|
|
if date_str:
|
|
try:
|
|
return datetime.strptime(date_str, '%Y-%m-%d').date()
|
|
except ValueError:
|
|
pass
|
|
return None
|
|
|
|
|
|
BatteryFormSet = inlineformset_factory(
|
|
UPSHost,
|
|
Battery,
|
|
form=BatteryForm,
|
|
extra=0,
|
|
can_delete=True,
|
|
fields=['brand', 'model', 'weight', 'quantity', 'location', 'install_date', 'last_maintenance_date']
|
|
)
|