2018-02-08 03:34:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
2018-07-15 10:39:11 +00:00
|
|
|
from orgs.mixins import OrgModelForm
|
|
|
|
|
|
|
|
from ..models import Asset, AdminUser
|
|
|
|
|
2018-02-08 03:34:21 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = ['AssetCreateForm', 'AssetUpdateForm', 'AssetBulkUpdateForm']
|
|
|
|
|
|
|
|
|
2018-07-15 10:39:11 +00:00
|
|
|
class AssetCreateForm(OrgModelForm):
|
2018-02-08 03:34:21 +00:00
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = [
|
|
|
|
'hostname', 'ip', 'public_ip', 'port', 'comment',
|
2018-02-27 11:39:27 +00:00
|
|
|
'nodes', 'is_active', 'admin_user', 'labels', 'platform',
|
2018-06-29 06:59:43 +00:00
|
|
|
'domain', 'protocol',
|
2018-02-08 03:34:21 +00:00
|
|
|
|
|
|
|
]
|
|
|
|
widgets = {
|
|
|
|
'nodes': forms.SelectMultiple(attrs={
|
|
|
|
'class': 'select2', 'data-placeholder': _('Nodes')
|
|
|
|
}),
|
|
|
|
'admin_user': forms.Select(attrs={
|
|
|
|
'class': 'select2', 'data-placeholder': _('Admin user')
|
|
|
|
}),
|
|
|
|
'labels': forms.SelectMultiple(attrs={
|
2018-04-08 12:02:40 +00:00
|
|
|
'class': 'select2', 'data-placeholder': _('Label')
|
2018-02-08 03:34:21 +00:00
|
|
|
}),
|
|
|
|
'port': forms.TextInput(),
|
2018-03-23 11:46:46 +00:00
|
|
|
'domain': forms.Select(attrs={
|
|
|
|
'class': 'select2', 'data-placeholder': _('Domain')
|
|
|
|
}),
|
2018-02-08 03:34:21 +00:00
|
|
|
}
|
2018-04-08 12:02:40 +00:00
|
|
|
labels = {
|
|
|
|
'nodes': _("Node"),
|
|
|
|
}
|
2018-02-08 03:34:21 +00:00
|
|
|
help_texts = {
|
2018-03-07 13:24:13 +00:00
|
|
|
'admin_user': _(
|
2018-03-14 10:14:37 +00:00
|
|
|
'root or other NOPASSWD sudo privilege user existed in asset,'
|
|
|
|
'If asset is windows or other set any one, more see admin user left menu'
|
2018-03-14 05:13:32 +00:00
|
|
|
),
|
2018-10-12 07:04:01 +00:00
|
|
|
'platform': _("Windows 2016 RDP protocol is different, If is window 2016, set it"),
|
2018-03-23 11:46:46 +00:00
|
|
|
'domain': _("If your have some network not connect with each other, you can set domain")
|
2018-02-08 03:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-15 10:39:11 +00:00
|
|
|
class AssetUpdateForm(OrgModelForm):
|
2018-02-08 03:34:21 +00:00
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = [
|
2018-02-27 11:39:27 +00:00
|
|
|
'hostname', 'ip', 'port', 'nodes', 'is_active', 'platform',
|
2018-02-08 03:34:21 +00:00
|
|
|
'public_ip', 'number', 'comment', 'admin_user', 'labels',
|
2018-06-29 06:59:43 +00:00
|
|
|
'domain', 'protocol',
|
2018-02-08 03:34:21 +00:00
|
|
|
]
|
|
|
|
widgets = {
|
|
|
|
'nodes': forms.SelectMultiple(attrs={
|
2018-04-08 12:02:40 +00:00
|
|
|
'class': 'select2', 'data-placeholder': _('Node')
|
2018-02-08 03:34:21 +00:00
|
|
|
}),
|
|
|
|
'admin_user': forms.Select(attrs={
|
|
|
|
'class': 'select2', 'data-placeholder': _('Admin user')
|
|
|
|
}),
|
|
|
|
'labels': forms.SelectMultiple(attrs={
|
2018-04-08 12:02:40 +00:00
|
|
|
'class': 'select2', 'data-placeholder': _('Label')
|
2018-02-08 03:34:21 +00:00
|
|
|
}),
|
|
|
|
'port': forms.TextInput(),
|
2018-03-23 11:46:46 +00:00
|
|
|
'domain': forms.Select(attrs={
|
|
|
|
'class': 'select2', 'data-placeholder': _('Domain')
|
|
|
|
}),
|
2018-02-08 03:34:21 +00:00
|
|
|
}
|
2018-04-08 12:02:40 +00:00
|
|
|
labels = {
|
|
|
|
'nodes': _("Node"),
|
|
|
|
}
|
2018-02-08 03:34:21 +00:00
|
|
|
help_texts = {
|
2018-02-25 10:08:00 +00:00
|
|
|
'admin_user': _(
|
2018-03-14 10:14:37 +00:00
|
|
|
'root or other NOPASSWD sudo privilege user existed in asset,'
|
|
|
|
'If asset is windows or other set any one, more see admin user left menu'
|
2018-03-14 05:13:32 +00:00
|
|
|
),
|
2018-10-12 07:04:01 +00:00
|
|
|
'platform': _("Windows 2016 RDP protocol is different, If is window 2016, set it"),
|
2018-03-23 11:46:46 +00:00
|
|
|
'domain': _("If your have some network not connect with each other, you can set domain")
|
2018-02-08 03:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-15 10:39:11 +00:00
|
|
|
class AssetBulkUpdateForm(OrgModelForm):
|
2018-02-08 03:34:21 +00:00
|
|
|
assets = forms.ModelMultipleChoiceField(
|
2018-12-21 06:57:42 +00:00
|
|
|
required=True,
|
2018-07-14 16:55:05 +00:00
|
|
|
label=_('Select assets'), queryset=Asset.objects.all(),
|
2018-02-08 03:34:21 +00:00
|
|
|
widget=forms.SelectMultiple(
|
|
|
|
attrs={
|
|
|
|
'class': 'select2',
|
|
|
|
'data-placeholder': _('Select assets')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
port = forms.IntegerField(
|
|
|
|
label=_('Port'), required=False, min_value=1, max_value=65535,
|
|
|
|
)
|
2018-02-25 14:36:42 +00:00
|
|
|
admin_user = forms.ModelChoiceField(
|
2018-07-14 16:55:05 +00:00
|
|
|
required=False, queryset=AdminUser.objects,
|
2018-02-25 14:36:42 +00:00
|
|
|
label=_("Admin user"),
|
|
|
|
widget=forms.Select(
|
|
|
|
attrs={
|
|
|
|
'class': 'select2',
|
|
|
|
'data-placeholder': _('Admin user')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2018-02-08 03:34:21 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = [
|
2018-03-14 05:13:32 +00:00
|
|
|
'assets', 'port', 'admin_user', 'labels', 'nodes', 'platform'
|
2018-02-08 03:34:21 +00:00
|
|
|
]
|
|
|
|
widgets = {
|
|
|
|
'labels': forms.SelectMultiple(
|
2018-04-08 12:02:40 +00:00
|
|
|
attrs={'class': 'select2', 'data-placeholder': _('Label')}
|
2018-02-08 03:34:21 +00:00
|
|
|
),
|
2018-02-09 07:24:44 +00:00
|
|
|
'nodes': forms.SelectMultiple(
|
2018-04-08 12:02:40 +00:00
|
|
|
attrs={'class': 'select2', 'data-placeholder': _('Node')}
|
2018-02-09 07:24:44 +00:00
|
|
|
),
|
2018-02-08 03:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
changed_fields = []
|
|
|
|
for field in self._meta.fields:
|
2018-02-25 14:36:42 +00:00
|
|
|
if self.data.get(field) not in [None, '']:
|
2018-02-08 03:34:21 +00:00
|
|
|
changed_fields.append(field)
|
|
|
|
|
|
|
|
cleaned_data = {k: v for k, v in self.cleaned_data.items()
|
|
|
|
if k in changed_fields}
|
|
|
|
assets = cleaned_data.pop('assets')
|
|
|
|
labels = cleaned_data.pop('labels', [])
|
2018-11-30 07:39:13 +00:00
|
|
|
nodes = cleaned_data.pop('nodes', None)
|
2018-02-08 03:34:21 +00:00
|
|
|
assets = Asset.objects.filter(id__in=[asset.id for asset in assets])
|
|
|
|
assets.update(**cleaned_data)
|
|
|
|
|
|
|
|
if labels:
|
2018-11-30 07:39:13 +00:00
|
|
|
for asset in assets:
|
|
|
|
asset.labels.set(labels)
|
2018-02-25 14:36:42 +00:00
|
|
|
if nodes:
|
2018-11-30 07:39:13 +00:00
|
|
|
for asset in assets:
|
|
|
|
asset.nodes.set(nodes)
|
2018-02-08 03:34:21 +00:00
|
|
|
return assets
|