jumpserver/apps/assets/serializers/account/account.py

60 lines
2.1 KiB
Python
Raw Normal View History

2022-07-15 10:03:32 +00:00
from django.db.models import F
2022-08-24 08:36:42 +00:00
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from common.drf.serializers import SecretReadableMixin
2022-08-24 08:36:42 +00:00
from assets.models import Account
from assets.serializers.base import AuthSerializerMixin
from .account_template import AccountTemplateSerializerMixin
from .common import BaseAccountSerializer
2022-08-24 08:36:42 +00:00
class AccountSerializer(
2022-09-01 06:46:31 +00:00
AccountTemplateSerializerMixin,
AuthSerializerMixin,
2022-08-30 03:56:56 +00:00
BulkOrgResourceModelSerializer
2022-08-24 08:36:42 +00:00
):
ip = serializers.ReadOnlyField(label=_("IP"))
2022-08-15 10:31:57 +00:00
asset_name = serializers.ReadOnlyField(label=_("Asset"))
platform = serializers.ReadOnlyField(label=_("Platform"))
2022-08-24 08:36:42 +00:00
class Meta(BaseAccountSerializer.Meta):
2022-07-15 10:03:32 +00:00
model = Account
2022-08-24 08:36:42 +00:00
fields = BaseAccountSerializer.Meta.fields + ['account_template', ]
2022-02-15 02:46:16 +00:00
def validate(self, attrs):
attrs = self._validate_gen_key(attrs)
2022-08-24 08:36:42 +00:00
attrs = super()._validate(attrs)
2022-02-15 02:46:16 +00:00
return attrs
@classmethod
def setup_eager_loading(cls, queryset):
""" Perform necessary eager loading of data. """
2022-08-24 08:36:42 +00:00
queryset = queryset.prefetch_related('asset') \
2022-07-15 10:03:32 +00:00
.annotate(ip=F('asset__ip')) \
2022-08-15 10:31:57 +00:00
.annotate(asset_name=F('asset__name'))
return queryset
class AccountSecretSerializer(SecretReadableMixin, AccountSerializer):
class Meta(AccountSerializer.Meta):
fields_backup = [
2022-08-11 07:45:03 +00:00
'name', 'ip', 'platform', 'protocols', 'username', 'password',
'private_key', 'public_key', 'date_created', 'date_updated', 'version'
]
extra_kwargs = {
'password': {'write_only': False},
'private_key': {'write_only': False},
'public_key': {'write_only': False},
2021-08-19 09:24:31 +00:00
'systemuser_display': {'label': _('System user display')}
}
class AccountTaskSerializer(serializers.Serializer):
ACTION_CHOICES = (
('test', 'test'),
)
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
task = serializers.CharField(read_only=True)