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
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2022-05-17 13:12:59 +00:00
|
|
|
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
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
):
|
2021-07-08 06:23:18 +00:00
|
|
|
ip = serializers.ReadOnlyField(label=_("IP"))
|
2022-08-15 10:31:57 +00:00
|
|
|
asset_name = serializers.ReadOnlyField(label=_("Asset"))
|
2022-01-17 07:58:39 +00:00
|
|
|
platform = serializers.ReadOnlyField(label=_("Platform"))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
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', ]
|
2021-07-08 06:23:18 +00:00
|
|
|
|
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
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
@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'))
|
2021-07-08 06:23:18 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2022-05-17 13:12:59 +00:00
|
|
|
class AccountSecretSerializer(SecretReadableMixin, AccountSerializer):
|
2021-07-08 06:23:18 +00:00
|
|
|
class Meta(AccountSerializer.Meta):
|
2022-01-17 07:58:39 +00:00
|
|
|
fields_backup = [
|
2022-08-11 07:45:03 +00:00
|
|
|
'name', 'ip', 'platform', 'protocols', 'username', 'password',
|
2022-01-17 07:58:39 +00:00
|
|
|
'private_key', 'public_key', 'date_created', 'date_updated', 'version'
|
|
|
|
]
|
2021-07-08 06:23:18 +00:00
|
|
|
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')}
|
2021-07-08 06:23:18 +00:00
|
|
|
}
|
2021-07-13 04:18:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccountTaskSerializer(serializers.Serializer):
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
('test', 'test'),
|
|
|
|
)
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
|
|
|
task = serializers.CharField(read_only=True)
|