mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
3.0 KiB
81 lines
3.0 KiB
# coding:utf-8
|
|
from django import forms
|
|
|
|
from .models import IDC, Asset, AssetGroup
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
class AssetForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Asset
|
|
|
|
fields = [
|
|
"ip", "other_ip", "remote_card_ip", "hostname", "port", "groups", "username", "password",
|
|
"idc", "mac_addr", "brand", "cpu", "memory", "disk", "os", "cabinet_no", "cabinet_pos",
|
|
"number", "status", "type", "env", "sn", "is_active", "comment"
|
|
]
|
|
|
|
widgets = {
|
|
'groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Select asset groups')}),
|
|
}
|
|
|
|
|
|
class AssetGroupForm(forms.ModelForm):
|
|
assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(),
|
|
label=_('Asset'),
|
|
required=False,
|
|
widget=forms.SelectMultiple(
|
|
attrs={'class': 'select2', 'data-placeholder': _('Select assets')})
|
|
)
|
|
|
|
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))
|
|
|
|
class Meta:
|
|
model = AssetGroup
|
|
fields = [
|
|
"name", "comment"
|
|
]
|
|
help_texts = {
|
|
'name': '* required',
|
|
}
|
|
|
|
|
|
class IDCForm(forms.ModelForm):
|
|
assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(),
|
|
label=_('Asset'),
|
|
required=False,
|
|
widget=forms.SelectMultiple(
|
|
attrs={'class': 'select2', 'data-placeholder': _('Select assets')})
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
if kwargs.get('instance'):
|
|
initial = kwargs.get('initial', {})
|
|
initial['assets'] = kwargs['instance'].assets.all()
|
|
super(IDCForm, self).__init__(*args, **kwargs)
|
|
|
|
def _save_m2m(self):
|
|
super(IDCForm, self)._save_m2m()
|
|
assets = self.cleaned_data['assets']
|
|
self.instance.assets.clear()
|
|
self.instance.assets.add(*tuple(assets))
|
|
|
|
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'})
|
|
}
|