forms.py 1.9 KB
Newer Older
W
wangyong 已提交
1 2 3 4
# coding:utf-8
from django import forms

from .models import IDC, Asset, AssetGroup
W
wangyong 已提交
5
from django.utils.translation import gettext_lazy as _
W
wangyong 已提交
6 7 8 9 10 11 12


class AssetForm(forms.ModelForm):
    class Meta:
        model = Asset

        fields = [
W
wangyong 已提交
13
            "ip", "other_ip", "remote_card_ip", "hostname", "port", "groups", "username", "password",
W
wangyong 已提交
14
            "idc", "mac_addr", "brand", "cpu", "memory", "disk", "os", "cabinet_no", "cabinet_pos",
W
wangyong 已提交
15 16 17
            "number", "status", "type", "env", "sn", "is_active", "comment"
        ]

W
wangyong 已提交
18 19 20 21
        widgets = {
            'groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Join assetgroups')}),
        }

W
wangyong 已提交
22 23

class AssetGroupForm(forms.ModelForm):
baltery's avatar
baltery 已提交
24 25 26 27
    assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(),
                                            widget=forms.SelectMultiple(attrs={
                                                'class': 'select2',
                                                'data-placeholder': _('Select assets')}))
baltery's avatar
baltery 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40

    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))

W
wangyong 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    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'})
        }