2022-06-23 05:52:28 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2022-06-27 02:15:29 +00:00
|
|
|
from assets.models import Asset, Node
|
2022-11-15 02:43:21 +00:00
|
|
|
from common.drf.fields import ObjectRelatedField
|
|
|
|
from perms.models import AssetPermission
|
|
|
|
from perms.serializers.permission import ActionChoicesField
|
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-15 02:43:21 +00:00
|
|
|
__all__ = ['ApplyAssetSerializer']
|
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
|
2022-11-15 02:43:21 +00:00
|
|
|
fields_mini = ['id', 'title']
|
2022-06-23 05:52:28 +00:00
|
|
|
writeable_fields = [
|
2022-11-15 02:43:21 +00:00
|
|
|
'id', 'title', 'apply_nodes', 'apply_assets',
|
2022-10-10 07:17:51 +00:00
|
|
|
'apply_accounts', 'apply_actions', 'org_id', 'comment',
|
2022-09-21 10:19:17 +00:00
|
|
|
'apply_date_start', 'apply_date_expired'
|
|
|
|
]
|
2022-11-15 02:43:21 +00:00
|
|
|
fields = TicketApplySerializer.Meta.fields + writeable_fields + ['apply_permission_name', ]
|
2022-06-23 05:52:28 +00:00
|
|
|
read_only_fields = list(set(fields) - set(writeable_fields))
|
|
|
|
ticket_extra_kwargs = TicketApplySerializer.Meta.extra_kwargs
|
|
|
|
extra_kwargs = {
|
2022-11-15 02:43:21 +00:00
|
|
|
'apply_nodes': {'required': False},
|
|
|
|
'apply_assets': {'required': False},
|
|
|
|
'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 (
|
2022-11-15 02:43:21 +00:00
|
|
|
not attrs.get('apply_nodes')
|
|
|
|
and not attrs.get('apply_assets')
|
2022-07-12 07:28:42 +00:00
|
|
|
):
|
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
|