mirror of
https://github.com/jumpserver/jumpserver.git
synced 2026-01-16 23:52:41 +00:00
* [Update] 统一url地址 * [Update] 修改api * [Update] 使用规范的签名 * [Update] 修改url * [Update] 修改swagger * [Update] 添加serializer class避免报错 * [Update] 修改token * [Update] 支持api key * [Update] 支持生成api key * [Update] 修改api重定向 * [Update] 修改翻译 * [Update] 添加说明文档 * [Update] 修复浏览器关闭后session不失效的问题 * [Update] 修改一些内容 * [Update] 修改 jms脚本 * [Update] 修改重定向 * [Update] 修改搜索trim * [Update] 修改搜索trim * [Update] 添加sys log * [Bugfix] 修改登陆错误 * [Update] 优化User操作private_token的接口 (#3091) * [Update] 优化User操作private_token的接口 * [Update] 优化User操作private_token的接口 2 * [Bugfix] 解决授权了一个节点,当移动节点后,被移动的节点下的资产会放到未分组节点下的问题 * [Update] 升级jquery * [Update] 默认使用page * [Update] 修改使用Orgmodel view set * [Update] 支持 nv的硬盘 https://github.com/jumpserver/jumpserver/issues/1804 * [UPdate] 解决命令执行宽度问题 * [Update] 优化节点 * [Update] 修改nodes过多时创建比较麻烦 * [Update] 修改导入 * [Update] 节点获取更新 * [Update] 修改nodes * [Update] nodes显示full value * [Update] 统一使用nodes select2 函数 * [Update] 修改磁盘大小小数 * [Update] 修改 Node service * [Update] 优化授权节点 * [Update] 修改 node permission * [Update] 修改asset permission * [Stash] * [Update] 修改node assets api * [Update] 修改tree service,支持资产数量 * [Update] 修改暂时完成 * [Update] 修改一些bug
176 lines
5.9 KiB
Python
176 lines
5.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from common.utils import get_logger
|
|
from orgs.mixins.forms import OrgModelForm
|
|
|
|
from ..models import Asset, Node
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
__all__ = [
|
|
'AssetCreateForm', 'AssetUpdateForm', 'AssetBulkUpdateForm', 'ProtocolForm',
|
|
]
|
|
|
|
|
|
class ProtocolForm(forms.Form):
|
|
name = forms.ChoiceField(
|
|
choices=Asset.PROTOCOL_CHOICES, label=_("Name"), initial='ssh',
|
|
widget=forms.Select(attrs={'class': 'form-control protocol-name'})
|
|
)
|
|
port = forms.IntegerField(
|
|
max_value=65534, min_value=1, label=_("Port"), initial=22,
|
|
widget=forms.TextInput(attrs={'class': 'form-control protocol-port'})
|
|
)
|
|
|
|
|
|
class AssetCreateForm(OrgModelForm):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if self.data:
|
|
return
|
|
nodes_field = self.fields['nodes']
|
|
if self.instance:
|
|
nodes_field.choices = ((n.id, n.full_value) for n in
|
|
self.instance.nodes.all())
|
|
else:
|
|
nodes_field.choices = []
|
|
|
|
class Meta:
|
|
model = Asset
|
|
fields = [
|
|
'hostname', 'ip', 'public_ip', 'protocols', 'comment',
|
|
'nodes', 'is_active', 'admin_user', 'labels', 'platform',
|
|
'domain',
|
|
]
|
|
widgets = {
|
|
'nodes': forms.SelectMultiple(attrs={
|
|
'class': 'nodes-select2', 'data-placeholder': _('Nodes')
|
|
}),
|
|
'admin_user': forms.Select(attrs={
|
|
'class': 'select2', 'data-placeholder': _('Admin user')
|
|
}),
|
|
'labels': forms.SelectMultiple(attrs={
|
|
'class': 'select2', 'data-placeholder': _('Label')
|
|
}),
|
|
'domain': forms.Select(attrs={
|
|
'class': 'select2', 'data-placeholder': _('Domain')
|
|
}),
|
|
}
|
|
labels = {
|
|
'nodes': _("Node"),
|
|
}
|
|
help_texts = {
|
|
'admin_user': _(
|
|
'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'
|
|
),
|
|
'platform': _("Windows 2016 RDP protocol is different, If is window 2016, set it"),
|
|
'domain': _("If your have some network not connect with each other, you can set domain")
|
|
}
|
|
|
|
|
|
class AssetUpdateForm(OrgModelForm):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if self.data:
|
|
return
|
|
nodes_field = self.fields['nodes']
|
|
if self.instance:
|
|
nodes_field.choices = ((n.id, n.full_value) for n in
|
|
self.instance.nodes.all())
|
|
else:
|
|
nodes_field.choices = []
|
|
|
|
class Meta:
|
|
model = Asset
|
|
fields = [
|
|
'hostname', 'ip', 'protocols', 'nodes', 'is_active', 'platform',
|
|
'public_ip', 'number', 'comment', 'admin_user', 'labels',
|
|
'domain',
|
|
]
|
|
widgets = {
|
|
'nodes': forms.SelectMultiple(attrs={
|
|
'class': 'nodes-select2', 'data-placeholder': _('Node')
|
|
}),
|
|
'admin_user': forms.Select(attrs={
|
|
'class': 'select2', 'data-placeholder': _('Admin user')
|
|
}),
|
|
'labels': forms.SelectMultiple(attrs={
|
|
'class': 'select2', 'data-placeholder': _('Label')
|
|
}),
|
|
'domain': forms.Select(attrs={
|
|
'class': 'select2', 'data-placeholder': _('Domain')
|
|
}),
|
|
}
|
|
labels = {
|
|
'nodes': _("Node"),
|
|
}
|
|
help_texts = {
|
|
'admin_user': _(
|
|
'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'
|
|
),
|
|
'platform': _("Windows 2016 RDP protocol is different, If is window 2016, set it"),
|
|
'domain': _("If your have some network not connect with each other, you can set domain")
|
|
}
|
|
|
|
|
|
class AssetBulkUpdateForm(OrgModelForm):
|
|
assets = forms.ModelMultipleChoiceField(
|
|
required=True,
|
|
label=_('Select assets'), queryset=Asset.objects.all(),
|
|
widget=forms.SelectMultiple(
|
|
attrs={
|
|
'class': 'select2',
|
|
'data-placeholder': _('Select assets')
|
|
}
|
|
)
|
|
)
|
|
|
|
class Meta:
|
|
model = Asset
|
|
fields = [
|
|
'assets', 'admin_user', 'labels', 'platform',
|
|
'domain',
|
|
]
|
|
widgets = {
|
|
'labels': forms.SelectMultiple(
|
|
attrs={'class': 'select2', 'data-placeholder': _('Label')}
|
|
),
|
|
'nodes': forms.SelectMultiple(
|
|
attrs={'class': 'select2', 'data-placeholder': _('Node')}
|
|
),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
# 重写其他字段为不再required
|
|
for name, field in self.fields.items():
|
|
if name != 'assets':
|
|
field.required = False
|
|
|
|
def save(self, commit=True):
|
|
changed_fields = []
|
|
for field in self._meta.fields:
|
|
if self.data.get(field) not in [None, '']:
|
|
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', [])
|
|
nodes = cleaned_data.pop('nodes', None)
|
|
assets = Asset.objects.filter(id__in=[asset.id for asset in assets])
|
|
assets.update(**cleaned_data)
|
|
|
|
if labels:
|
|
for asset in assets:
|
|
asset.labels.set(labels)
|
|
if nodes:
|
|
for asset in assets:
|
|
asset.nodes.set(nodes)
|
|
return assets
|