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 _
|
|
|
|
|
2018-07-18 04:57:08 +00:00
|
|
|
from orgs.mixins import OrgModelForm
|
2018-07-20 09:49:47 +00:00
|
|
|
from orgs.utils import current_org
|
2018-04-07 16:16:37 +00:00
|
|
|
from .models import AssetPermission
|
2018-12-25 05:33:37 +00:00
|
|
|
from assets.models import Asset
|
2016-09-10 13:08:10 +00:00
|
|
|
|
|
|
|
|
2018-07-18 04:57:08 +00:00
|
|
|
class AssetPermissionForm(OrgModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
users_field = self.fields.get('users')
|
|
|
|
if hasattr(users_field, 'queryset'):
|
2018-07-27 08:21:55 +00:00
|
|
|
users_field.queryset = current_org.get_org_users()
|
2018-12-25 05:33:37 +00:00
|
|
|
assets_field = self.fields.get('assets')
|
|
|
|
|
|
|
|
# 前端渲染优化, 防止过多资产
|
|
|
|
if not self.data:
|
|
|
|
instance = kwargs.get('instance')
|
|
|
|
if instance:
|
|
|
|
assets_field.queryset = instance.assets.all()
|
|
|
|
else:
|
|
|
|
assets_field.queryset = Asset.objects.none()
|
2018-07-18 04:57:08 +00:00
|
|
|
|
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(
|
|
|
|
attrs={'class': '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(
|
|
|
|
attrs={'class': 'select2', 'data-placeholder': _("Node")}
|
|
|
|
),
|
|
|
|
'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"]
|