2022-06-23 05:52:28 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2022-09-07 09:35:23 +00:00
|
|
|
from perms.serializers.permission import ActionsField
|
2022-06-23 05:52:28 +00:00
|
|
|
from perms.models import AssetPermission
|
|
|
|
from orgs.utils import tmp_to_org
|
2022-06-27 02:15:29 +00:00
|
|
|
from assets.models import Asset, Node
|
2022-06-23 05:52:28 +00:00
|
|
|
|
|
|
|
from tickets.models import ApplyAssetTicket
|
|
|
|
from .ticket import TicketApplySerializer
|
|
|
|
from .common import BaseApplyAssetApplicationSerializer
|
|
|
|
|
2022-07-12 07:28:42 +00:00
|
|
|
__all__ = ['ApplyAssetSerializer', 'ApplyAssetDisplaySerializer', 'ApproveAssetSerializer']
|
2022-06-23 05:52:28 +00:00
|
|
|
|
|
|
|
asset_or_node_help_text = _("Select at least one asset or node")
|
|
|
|
|
|
|
|
|
|
|
|
class ApplyAssetSerializer(BaseApplyAssetApplicationSerializer, TicketApplySerializer):
|
2022-07-12 07:28:42 +00:00
|
|
|
apply_actions = ActionsField(required=True, allow_empty=False)
|
2022-06-23 05:52:28 +00:00
|
|
|
permission_model = AssetPermission
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ApplyAssetTicket
|
|
|
|
writeable_fields = [
|
|
|
|
'id', 'title', 'type', 'apply_nodes', 'apply_assets',
|
2022-07-22 08:24:57 +00:00
|
|
|
'apply_system_users', 'apply_actions',
|
2022-06-23 05:52:28 +00:00
|
|
|
'apply_date_start', 'apply_date_expired', 'org_id'
|
|
|
|
]
|
2022-07-22 08:24:57 +00:00
|
|
|
fields = TicketApplySerializer.Meta.fields + \
|
|
|
|
writeable_fields + ['apply_permission_name', 'apply_actions_display']
|
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-07-12 07:28:42 +00:00
|
|
|
'apply_nodes': {'required': False, 'allow_empty': True},
|
|
|
|
'apply_assets': {'required': False, 'allow_empty': True},
|
|
|
|
'apply_system_users': {'required': False, 'allow_empty': True},
|
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):
|
|
|
|
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-07-12 07:28:42 +00:00
|
|
|
class ApproveAssetSerializer(ApplyAssetSerializer):
|
|
|
|
class Meta(ApplyAssetSerializer.Meta):
|
|
|
|
read_only_fields = ApplyAssetSerializer.Meta.read_only_fields + ['title', 'type']
|
|
|
|
|
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
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)
|