mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-24 04:50:30 +00:00
merge: with v3
This commit is contained in:
@@ -1,39 +1,37 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from perms.serializers.base import ActionsField
|
||||
from perms.models import AssetPermission
|
||||
from orgs.utils import tmp_to_org
|
||||
from assets.models import Asset, Node
|
||||
|
||||
from perms.models import AssetPermission
|
||||
from perms.serializers.permission import ActionChoicesField
|
||||
from common.drf.fields import ObjectRelatedField
|
||||
from tickets.models import ApplyAssetTicket
|
||||
from .common import BaseApplyAssetSerializer
|
||||
from .ticket import TicketApplySerializer
|
||||
from .common import BaseApplyAssetApplicationSerializer
|
||||
|
||||
__all__ = ['ApplyAssetSerializer', 'ApplyAssetDisplaySerializer', 'ApproveAssetSerializer']
|
||||
__all__ = ['ApplyAssetSerializer', 'ApproveAssetSerializer']
|
||||
|
||||
asset_or_node_help_text = _("Select at least one asset or node")
|
||||
|
||||
|
||||
class ApplyAssetSerializer(BaseApplyAssetApplicationSerializer, TicketApplySerializer):
|
||||
apply_actions = ActionsField(required=True, allow_empty=False)
|
||||
class ApplyAssetSerializer(BaseApplyAssetSerializer, TicketApplySerializer):
|
||||
apply_assets = ObjectRelatedField(queryset=Asset.objects, many=True, required=False, label=_('Apply assets'))
|
||||
apply_nodes = ObjectRelatedField(queryset=Node.objects, many=True, required=False, label=_('Apply nodes'))
|
||||
apply_actions = ActionChoicesField(required=False, allow_null=True, label=_("Apply actions"))
|
||||
permission_model = AssetPermission
|
||||
|
||||
class Meta:
|
||||
class Meta(TicketApplySerializer.Meta):
|
||||
model = ApplyAssetTicket
|
||||
writeable_fields = [
|
||||
'id', 'title', 'type', 'apply_nodes', 'apply_assets',
|
||||
'apply_system_users', 'apply_actions', 'comment',
|
||||
'apply_date_start', 'apply_date_expired', 'org_id'
|
||||
'id', 'title', 'type', 'apply_nodes', 'apply_assets', 'apply_accounts',
|
||||
'apply_actions', 'apply_date_start', 'apply_date_expired',
|
||||
'comment', 'org_id'
|
||||
]
|
||||
fields = TicketApplySerializer.Meta.fields + \
|
||||
writeable_fields + ['apply_permission_name', 'apply_actions_display']
|
||||
read_only_fields = list(set(fields) - set(writeable_fields))
|
||||
read_only_fields = TicketApplySerializer.Meta.read_only_fields + ['apply_permission_name', ]
|
||||
fields = TicketApplySerializer.Meta.fields_small + writeable_fields + read_only_fields
|
||||
ticket_extra_kwargs = TicketApplySerializer.Meta.extra_kwargs
|
||||
extra_kwargs = {
|
||||
'apply_nodes': {'required': False, 'allow_empty': True},
|
||||
'apply_assets': {'required': False, 'allow_empty': True},
|
||||
'apply_system_users': {'required': False, 'allow_empty': True},
|
||||
'apply_accounts': {'required': False},
|
||||
}
|
||||
extra_kwargs.update(ticket_extra_kwargs)
|
||||
|
||||
@@ -44,6 +42,7 @@ class ApplyAssetSerializer(BaseApplyAssetApplicationSerializer, TicketApplySeria
|
||||
return self.filter_many_to_many_field(Asset, assets)
|
||||
|
||||
def validate(self, attrs):
|
||||
attrs['type'] = 'apply_asset'
|
||||
attrs = super().validate(attrs)
|
||||
if self.is_final_approval and (
|
||||
not attrs.get('apply_nodes') and not attrs.get('apply_assets')
|
||||
@@ -55,33 +54,13 @@ class ApplyAssetSerializer(BaseApplyAssetApplicationSerializer, TicketApplySeria
|
||||
|
||||
return attrs
|
||||
|
||||
@classmethod
|
||||
def setup_eager_loading(cls, queryset):
|
||||
queryset = queryset.prefetch_related('apply_nodes', 'apply_assets')
|
||||
return queryset
|
||||
|
||||
|
||||
class ApproveAssetSerializer(ApplyAssetSerializer):
|
||||
class Meta(ApplyAssetSerializer.Meta):
|
||||
read_only_fields = ApplyAssetSerializer.Meta.read_only_fields + ['title', 'type']
|
||||
|
||||
|
||||
class ApplyAssetDisplaySerializer(ApplyAssetSerializer):
|
||||
apply_nodes = serializers.SerializerMethodField()
|
||||
apply_assets = serializers.SerializerMethodField()
|
||||
apply_system_users = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = ApplyAssetSerializer.Meta.model
|
||||
fields = ApplyAssetSerializer.Meta.fields
|
||||
read_only_fields = fields
|
||||
|
||||
@staticmethod
|
||||
def get_apply_nodes(instance):
|
||||
with tmp_to_org(instance.org_id):
|
||||
return instance.apply_nodes.values_list('id', flat=True)
|
||||
|
||||
@staticmethod
|
||||
def get_apply_assets(instance):
|
||||
with tmp_to_org(instance.org_id):
|
||||
return instance.apply_assets.values_list('id', flat=True)
|
||||
|
||||
@staticmethod
|
||||
def get_apply_system_users(instance):
|
||||
with tmp_to_org(instance.org_id):
|
||||
return instance.apply_system_users.values_list('id', flat=True)
|
||||
read_only_fields = TicketApplySerializer.Meta.fields_small + \
|
||||
ApplyAssetSerializer.Meta.read_only_fields
|
||||
|
Reference in New Issue
Block a user