116 lines
4.6 KiB
Python
116 lines
4.6 KiB
Python
from django import forms
|
|
from django.forms import inlineformset_factory
|
|
from .models import UPSHost, Battery
|
|
|
|
|
|
class UPSHostForm(forms.ModelForm):
|
|
last_maintenance_date = forms.CharField(
|
|
required=False,
|
|
label='上次维保时间',
|
|
widget=forms.TextInput(attrs={'class': 'form-control datepicker', 'placeholder': '选择日期'}),
|
|
)
|
|
|
|
class Meta:
|
|
model = UPSHost
|
|
fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact']
|
|
labels = {
|
|
'brand': '品牌',
|
|
'model': '型号',
|
|
'ip_address': 'IP地址',
|
|
'quantity': '数量',
|
|
'location': '存放位置',
|
|
'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'}),
|
|
'contact': forms.Select(attrs={'class': 'form-control'}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if self.instance and self.instance.pk and self.instance.last_maintenance_date:
|
|
self.fields['last_maintenance_date'].initial = self.instance.last_maintenance_date.strftime('%Y-%m-%d')
|
|
|
|
def clean_last_maintenance_date(self):
|
|
date_str = self.cleaned_data.get('last_maintenance_date')
|
|
if date_str:
|
|
from datetime import datetime
|
|
try:
|
|
return datetime.strptime(date_str, '%Y-%m-%d').date()
|
|
except ValueError:
|
|
return date_str
|
|
return None
|
|
|
|
|
|
class BatteryForm(forms.ModelForm):
|
|
install_date = forms.CharField(
|
|
required=False,
|
|
label='安装日期',
|
|
widget=forms.TextInput(attrs={'class': 'form-control form-control-sm datepicker-battery', 'placeholder': '选择日期'}),
|
|
)
|
|
last_maintenance_date = forms.CharField(
|
|
required=False,
|
|
label='上次维保时间',
|
|
widget=forms.TextInput(attrs={'class': 'form-control form-control-sm datepicker-battery', 'placeholder': '选择日期'}),
|
|
)
|
|
|
|
class Meta:
|
|
model = Battery
|
|
fields = ['brand', 'model', 'weight', 'quantity', 'location', 'install_date', 'last_maintenance_date']
|
|
labels = {
|
|
'brand': '电池品牌',
|
|
'model': '电池型号',
|
|
'weight': '重量(kg)',
|
|
'quantity': '数量',
|
|
'location': '存放位置',
|
|
}
|
|
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'}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if self.instance and self.instance.pk:
|
|
if self.instance.install_date:
|
|
self.fields['install_date'].initial = self.instance.install_date.strftime('%Y-%m-%d')
|
|
if self.instance.last_maintenance_date:
|
|
self.fields['last_maintenance_date'].initial = self.instance.last_maintenance_date.strftime('%Y-%m-%d')
|
|
|
|
def clean_install_date(self):
|
|
date_str = self.cleaned_data.get('install_date')
|
|
if date_str:
|
|
from datetime import datetime
|
|
try:
|
|
return datetime.strptime(date_str, '%Y-%m-%d').date()
|
|
except ValueError:
|
|
return date_str
|
|
return None
|
|
|
|
def clean_last_maintenance_date(self):
|
|
date_str = self.cleaned_data.get('last_maintenance_date')
|
|
if date_str:
|
|
from datetime import datetime
|
|
try:
|
|
return datetime.strptime(date_str, '%Y-%m-%d').date()
|
|
except ValueError:
|
|
return date_str
|
|
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']
|
|
)
|