2019-07-01 11:30:18 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
|
2023-02-23 09:44:38 +00:00
|
|
|
|
from django.db.models import F
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-15 02:43:21 +00:00
|
|
|
|
from rest_framework import serializers
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
2023-01-31 10:06:44 +00:00
|
|
|
|
from accounts.models import Account
|
2022-11-09 12:58:45 +00:00
|
|
|
|
from assets.const import Category, AllTypes
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from assets.models import Node, Asset, Platform
|
2024-01-24 07:41:03 +00:00
|
|
|
|
from assets.serializers.asset.common import AssetProtocolsPermsSerializer
|
2023-12-25 08:49:39 +00:00
|
|
|
|
from common.serializers import ResourceLabelsMixin
|
2024-01-24 07:41:03 +00:00
|
|
|
|
from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
|
2023-01-31 10:06:44 +00:00
|
|
|
|
from perms.serializers.permission import ActionChoicesField
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
__all__ = [
|
2022-12-15 08:02:34 +00:00
|
|
|
|
'NodePermedSerializer', 'AssetPermedSerializer',
|
2023-02-24 03:27:38 +00:00
|
|
|
|
'AssetPermedDetailSerializer', 'AccountsPermedSerializer'
|
2019-07-01 11:30:18 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2023-12-25 08:49:39 +00:00
|
|
|
|
class AssetPermedSerializer(OrgResourceModelSerializerMixin, ResourceLabelsMixin):
|
2022-09-15 02:46:57 +00:00
|
|
|
|
""" 被授权资产的数据结构 """
|
2022-11-16 07:07:35 +00:00
|
|
|
|
platform = ObjectRelatedField(required=False, queryset=Platform.objects, label=_('Platform'))
|
2022-11-09 12:58:45 +00:00
|
|
|
|
category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category'))
|
|
|
|
|
type = LabeledChoiceField(choices=AllTypes.choices(), read_only=True, label=_('Type'))
|
2023-01-16 11:02:09 +00:00
|
|
|
|
domain = ObjectRelatedField(required=False, queryset=Node.objects, label=_('Domain'))
|
2020-10-22 10:13:14 +00:00
|
|
|
|
|
2020-10-22 09:05:47 +00:00
|
|
|
|
class Meta:
|
2020-10-22 10:13:14 +00:00
|
|
|
|
model = Asset
|
|
|
|
|
only_fields = [
|
2023-04-25 06:36:01 +00:00
|
|
|
|
'id', 'name', 'address', 'domain', 'platform',
|
|
|
|
|
'comment', 'org_id', 'is_active', 'date_verified',
|
2023-04-25 06:48:09 +00:00
|
|
|
|
'created_by', 'date_created', 'connectivity', 'nodes', 'labels'
|
2020-10-22 10:13:14 +00:00
|
|
|
|
]
|
2023-10-26 11:19:09 +00:00
|
|
|
|
fields = only_fields + ['category', 'type'] + ['org_name']
|
2020-10-22 09:05:47 +00:00
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
2023-02-23 09:44:38 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
|
""" Perform necessary eager loading of data. """
|
|
|
|
|
queryset = queryset.prefetch_related('domain', 'nodes', 'labels') \
|
2023-10-26 11:19:09 +00:00
|
|
|
|
.prefetch_related('platform') \
|
2023-02-23 09:44:38 +00:00
|
|
|
|
.annotate(category=F("platform__category")) \
|
|
|
|
|
.annotate(type=F("platform__type"))
|
|
|
|
|
return queryset
|
|
|
|
|
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
2022-12-15 08:02:34 +00:00
|
|
|
|
class NodePermedSerializer(serializers.ModelSerializer):
|
2020-10-22 10:13:14 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
model = Node
|
|
|
|
|
fields = [
|
2023-10-26 11:19:09 +00:00
|
|
|
|
'id', 'name', 'key', 'value',
|
|
|
|
|
'org_id', "assets_amount"
|
2020-10-22 10:13:14 +00:00
|
|
|
|
]
|
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
|
|
2022-11-16 03:29:02 +00:00
|
|
|
|
class AccountsPermedSerializer(serializers.ModelSerializer):
|
2022-11-11 07:04:31 +00:00
|
|
|
|
actions = ActionChoicesField(read_only=True)
|
2022-09-15 02:46:57 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Account
|
2022-12-15 08:02:34 +00:00
|
|
|
|
fields = [
|
2023-07-26 11:22:55 +00:00
|
|
|
|
'id', 'alias', 'name', 'username', 'has_username',
|
2022-12-15 08:02:34 +00:00
|
|
|
|
'has_secret', 'secret_type', 'actions'
|
|
|
|
|
]
|
2022-09-15 02:46:57 +00:00
|
|
|
|
read_only_fields = fields
|
2023-10-26 11:19:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermedDetailSerializer(AssetPermedSerializer):
|
2023-10-27 09:19:13 +00:00
|
|
|
|
# 前面特意加了 permed,避免返回的是资产本身的
|
2023-10-26 11:19:09 +00:00
|
|
|
|
permed_protocols = AssetProtocolsPermsSerializer(many=True, required=False, label=_('Protocols'))
|
|
|
|
|
permed_accounts = AccountsPermedSerializer(label=_("Accounts"), required=False, many=True)
|
|
|
|
|
|
|
|
|
|
class Meta(AssetPermedSerializer.Meta):
|
|
|
|
|
fields = AssetPermedSerializer.Meta.fields + ['spec_info', 'permed_protocols', 'permed_accounts']
|
|
|
|
|
read_only_fields = fields
|