2022-06-23 05:52:28 +00:00
|
|
|
from rest_framework import serializers
|
2022-11-18 03:30:31 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-06-23 05:52:28 +00:00
|
|
|
|
2022-06-27 02:15:29 +00:00
|
|
|
from assets.models import Asset, Node
|
2022-11-15 02:43:21 +00:00
|
|
|
from perms.models import AssetPermission
|
|
|
|
from perms.serializers.permission import ActionChoicesField
|
2022-11-18 03:30:31 +00:00
|
|
|
from common.drf.fields import ObjectRelatedField
|
2022-06-23 05:52:28 +00:00
|
|
|
from tickets.models import ApplyAssetTicket
|
2022-11-15 02:43:21 +00:00
|
|
|
from .common import BaseApplyAssetSerializer
|
2022-06-23 05:52:28 +00:00
|
|
|
from .ticket import TicketApplySerializer
|
|
|
|
|
2022-11-17 09:34:52 +00:00
|
|
|
__all__ = ['ApplyAssetSerializer', 'ApproveAssetSerializer']
|
2022-06-23 05:52:28 +00:00
|
|
|
|
|
|
|
asset_or_node_help_text = _("Select at least one asset or node")
|
|
|
|
|
|
|
|
|
2022-11-15 02:43:21 +00:00
|
|
|
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"))
|
2022-06-23 05:52:28 +00:00
|
|
|
permission_model = AssetPermission
|
|
|
|
|
2022-11-15 02:43:21 +00:00
|
|
|
class Meta(TicketApplySerializer.Meta):
|
2022-06-23 05:52:28 +00:00
|
|
|
model = ApplyAssetTicket
|
|
|
|
writeable_fields = [
|
2022-12-05 07:03:21 +00:00
|
|
|
'id', 'title', 'type', 'apply_nodes', 'apply_assets', 'apply_accounts',
|
|
|
|
'apply_actions', 'apply_date_start', 'apply_date_expired',
|
|
|
|
'comment', 'org_id'
|
2022-06-23 05:52:28 +00:00
|
|
|
]
|
2022-11-17 05:52:10 +00:00
|
|
|
read_only_fields = TicketApplySerializer.Meta.read_only_fields + ['apply_permission_name', ]
|
|
|
|
fields = TicketApplySerializer.Meta.fields_small + writeable_fields + read_only_fields
|
2022-06-23 05:52:28 +00:00
|
|
|
ticket_extra_kwargs = TicketApplySerializer.Meta.extra_kwargs
|
|
|
|
extra_kwargs = {
|
2022-11-15 02:43:21 +00:00
|
|
|
'apply_accounts': {'required': False},
|
2022-06-23 05:52:28 +00:00
|
|
|
}
|
|
|
|
extra_kwargs.update(ticket_extra_kwargs)
|
|
|
|
|
2022-06-27 02:15:29 +00:00
|
|
|
def validate_apply_nodes(self, nodes):
|
|
|
|
return self.filter_many_to_many_field(Node, nodes)
|
|
|
|
|
|
|
|
def validate_apply_assets(self, assets):
|
|
|
|
return self.filter_many_to_many_field(Asset, assets)
|
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
def validate(self, attrs):
|
2022-11-15 02:43:21 +00:00
|
|
|
attrs['type'] = 'apply_asset'
|
2022-06-23 05:52:28 +00:00
|
|
|
attrs = super().validate(attrs)
|
2022-07-12 07:28:42 +00:00
|
|
|
if self.is_final_approval and (
|
|
|
|
not attrs.get('apply_nodes') and not attrs.get('apply_assets')
|
|
|
|
):
|
2022-06-23 05:52:28 +00:00
|
|
|
raise serializers.ValidationError({
|
|
|
|
'apply_nodes': asset_or_node_help_text,
|
|
|
|
'apply_assets': asset_or_node_help_text,
|
|
|
|
})
|
2022-07-12 07:28:42 +00:00
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
return attrs
|
|
|
|
|
2022-11-15 02:43:21 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
queryset = queryset.prefetch_related('apply_nodes', 'apply_assets')
|
|
|
|
return queryset
|
2022-11-17 09:34:52 +00:00
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
|
2022-07-12 07:28:42 +00:00
|
|
|
class ApproveAssetSerializer(ApplyAssetSerializer):
|
|
|
|
class Meta(ApplyAssetSerializer.Meta):
|
2022-11-17 09:34:52 +00:00
|
|
|
read_only_fields = TicketApplySerializer.Meta.fields_small + \
|
|
|
|
ApplyAssetSerializer.Meta.read_only_fields
|