2016-08-14 14:10:10 +00:00
|
|
|
# coding:utf-8
|
|
|
|
from django import forms
|
|
|
|
|
|
|
|
from .models import IDC, Asset, AssetGroup
|
2016-09-04 11:12:31 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2016-08-14 14:10:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
|
|
|
|
fields = [
|
2016-09-04 10:06:14 +00:00
|
|
|
"ip", "other_ip", "remote_card_ip", "hostname", "port", "groups", "username", "password",
|
2016-09-04 09:43:03 +00:00
|
|
|
"idc", "mac_addr", "brand", "cpu", "memory", "disk", "os", "cabinet_no", "cabinet_pos",
|
2016-08-14 14:10:10 +00:00
|
|
|
"number", "status", "type", "env", "sn", "is_active", "comment"
|
|
|
|
]
|
|
|
|
|
2016-09-04 11:12:31 +00:00
|
|
|
widgets = {
|
2016-09-06 07:09:00 +00:00
|
|
|
'groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Select asset groups')}),
|
2016-09-04 11:12:31 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 14:10:10 +00:00
|
|
|
|
|
|
|
class AssetGroupForm(forms.ModelForm):
|
2016-09-05 17:09:03 +00:00
|
|
|
assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(),
|
2016-09-06 06:38:19 +00:00
|
|
|
label=_('Asset'),
|
|
|
|
required=False,
|
|
|
|
widget=forms.SelectMultiple(
|
|
|
|
attrs={'class': 'select2', 'data-placeholder': _('Select assets')})
|
|
|
|
)
|
2016-09-05 16:49:42 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if kwargs.get('instance'):
|
|
|
|
initial = kwargs.get('initial', {})
|
|
|
|
initial['assets'] = kwargs['instance'].assets.all()
|
|
|
|
super(AssetGroupForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def _save_m2m(self):
|
|
|
|
super(AssetGroupForm, self)._save_m2m()
|
|
|
|
assets = self.cleaned_data['assets']
|
|
|
|
self.instance.assets.clear()
|
|
|
|
self.instance.assets.add(*tuple(assets))
|
|
|
|
|
2016-08-14 14:10:10 +00:00
|
|
|
class Meta:
|
|
|
|
model = AssetGroup
|
|
|
|
fields = [
|
|
|
|
"name", "comment"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class IdcForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = IDC
|
|
|
|
fields = ['name', "bandwidth", "operator", 'contact', 'phone', 'address', 'network', 'comment']
|
|
|
|
widgets = {
|
|
|
|
'name': forms.TextInput(attrs={'placeholder': 'Name'}),
|
|
|
|
'network': forms.Textarea(
|
|
|
|
attrs={'placeholder': '192.168.1.0/24\n192.168.2.0/24'})
|
|
|
|
}
|