2016-09-10 13:08:10 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.forms import OrgModelForm
|
2019-12-19 07:28:17 +00:00
|
|
|
from assets.models import Asset, Node, SystemUser
|
2019-07-02 08:45:26 +00:00
|
|
|
from ..models import AssetPermission, Action
|
2016-09-10 13:08:10 +00:00
|
|
|
|
2019-05-20 11:39:53 +00:00
|
|
|
__all__ = [
|
|
|
|
'AssetPermissionForm',
|
|
|
|
]
|
|
|
|
|
2016-09-10 13:08:10 +00:00
|
|
|
|
2019-06-30 12:10:34 +00:00
|
|
|
class ActionField(forms.MultipleChoiceField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-07-02 08:45:26 +00:00
|
|
|
kwargs['choices'] = Action.CHOICES
|
|
|
|
kwargs['initial'] = Action.ALL
|
2019-06-30 12:10:34 +00:00
|
|
|
kwargs['label'] = _("Action")
|
|
|
|
kwargs['widget'] = forms.CheckboxSelectMultiple()
|
2019-10-24 10:35:03 +00:00
|
|
|
kwargs['help_text'] = _(
|
|
|
|
'Tips: The RDP protocol does not support separate controls '
|
|
|
|
'for uploading or downloading files'
|
|
|
|
)
|
2019-06-30 12:10:34 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
value = super().to_python(value)
|
2019-07-02 08:45:26 +00:00
|
|
|
return Action.choices_to_value(value)
|
2019-06-30 12:10:34 +00:00
|
|
|
|
|
|
|
def prepare_value(self, value):
|
|
|
|
if value is None:
|
|
|
|
return value
|
2019-07-02 08:45:26 +00:00
|
|
|
value = Action.value_to_choices(value)
|
2019-06-30 12:10:34 +00:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2018-07-18 04:57:08 +00:00
|
|
|
class AssetPermissionForm(OrgModelForm):
|
2019-07-05 08:34:37 +00:00
|
|
|
actions = ActionField()
|
2019-06-30 12:10:34 +00:00
|
|
|
|
2018-07-18 04:57:08 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-12-25 05:33:37 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
if self.data:
|
|
|
|
return
|
2018-12-25 05:33:37 +00:00
|
|
|
# 前端渲染优化, 防止过多资产
|
2019-09-27 06:18:51 +00:00
|
|
|
users_field = self.fields.get('users')
|
2019-08-21 12:27:21 +00:00
|
|
|
assets_field = self.fields['assets']
|
|
|
|
nodes_field = self.fields['nodes']
|
|
|
|
if self.instance:
|
|
|
|
assets_field.queryset = self.instance.assets.all()
|
|
|
|
nodes_field.queryset = self.instance.nodes.all()
|
2019-09-27 06:18:51 +00:00
|
|
|
users_field.queryset = self.instance.users.all()
|
2019-08-21 12:27:21 +00:00
|
|
|
else:
|
|
|
|
assets_field.queryset = Asset.objects.none()
|
|
|
|
nodes_field.queryset = Node.objects.none()
|
2019-09-27 06:18:51 +00:00
|
|
|
users_field.queryset = []
|
2019-06-28 14:07:22 +00:00
|
|
|
|
2019-12-19 07:28:17 +00:00
|
|
|
# 过滤系统用户
|
|
|
|
system_users_field = self.fields.get('system_users')
|
|
|
|
system_users_field.queryset = SystemUser.objects.exclude(
|
|
|
|
protocol=SystemUser.PROTOCOL_MYSQL
|
|
|
|
)
|
|
|
|
|
2019-09-12 09:58:52 +00:00
|
|
|
def set_nodes_initial(self, nodes):
|
|
|
|
field = self.fields['nodes']
|
|
|
|
field.choices = [(n.id, n.full_value) for n in nodes]
|
|
|
|
field.initial = nodes
|
|
|
|
|
|
|
|
def set_assets_initial(self, assets):
|
|
|
|
field = self.fields['assets']
|
|
|
|
field.choices = [(a.id, a.hostname) for a in assets]
|
|
|
|
field.initial = assets
|
|
|
|
|
2016-09-10 13:08:10 +00:00
|
|
|
class Meta:
|
2018-04-07 16:16:37 +00:00
|
|
|
model = AssetPermission
|
|
|
|
exclude = (
|
2018-07-25 03:28:20 +00:00
|
|
|
'id', 'date_created', 'created_by', 'org_id'
|
2018-04-07 16:16:37 +00:00
|
|
|
)
|
2016-09-11 08:59:19 +00:00
|
|
|
widgets = {
|
2018-04-07 16:16:37 +00:00
|
|
|
'users': forms.SelectMultiple(
|
2019-09-27 06:18:51 +00:00
|
|
|
attrs={'class': 'users-select2', 'data-placeholder': _("User")}
|
2018-02-01 09:14:15 +00:00
|
|
|
),
|
2018-04-07 16:16:37 +00:00
|
|
|
'user_groups': forms.SelectMultiple(
|
2018-02-01 09:14:15 +00:00
|
|
|
attrs={'class': 'select2', 'data-placeholder': _("User group")}
|
|
|
|
),
|
2018-04-07 16:16:37 +00:00
|
|
|
'assets': forms.SelectMultiple(
|
|
|
|
attrs={'class': 'select2', 'data-placeholder': _("Asset")}
|
|
|
|
),
|
|
|
|
'nodes': forms.SelectMultiple(
|
2019-08-21 12:27:21 +00:00
|
|
|
attrs={'class': 'nodes-select2', 'data-placeholder': _("Node")}
|
2018-04-07 16:16:37 +00:00
|
|
|
),
|
|
|
|
'system_users': forms.SelectMultiple(
|
2018-02-01 09:14:15 +00:00
|
|
|
attrs={'class': 'select2', 'data-placeholder': _('System user')}
|
|
|
|
),
|
2016-09-11 14:45:24 +00:00
|
|
|
}
|
2018-04-07 16:16:37 +00:00
|
|
|
labels = {
|
|
|
|
'nodes': _("Node"),
|
|
|
|
}
|
2018-04-10 12:29:06 +00:00
|
|
|
|
|
|
|
def clean_user_groups(self):
|
|
|
|
users = self.cleaned_data.get('users')
|
|
|
|
user_groups = self.cleaned_data.get('user_groups')
|
|
|
|
|
|
|
|
if not users and not user_groups:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
_("User or group at least one required"))
|
|
|
|
return self.cleaned_data["user_groups"]
|
|
|
|
|
|
|
|
def clean_asset_groups(self):
|
|
|
|
assets = self.cleaned_data.get('assets')
|
|
|
|
asset_groups = self.cleaned_data.get('asset_groups')
|
|
|
|
|
|
|
|
if not assets and not asset_groups:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
_("Asset or group at least one required"))
|
|
|
|
|
|
|
|
return self.cleaned_data["asset_groups"]
|