|
|
@ -1,15 +1,15 @@
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
|
|
|
|
from common.drf.serializers import SecretReadableMixin
|
|
|
|
from assets.const import SecretType
|
|
|
|
from common.drf.fields import ObjectRelatedField, LabeledChoiceField
|
|
|
|
|
|
|
|
from assets.tasks import push_accounts_to_assets
|
|
|
|
|
|
|
|
from assets.models import Account, AccountTemplate, Asset
|
|
|
|
from assets.models import Account, AccountTemplate, Asset
|
|
|
|
|
|
|
|
from assets.tasks import push_accounts_to_assets
|
|
|
|
|
|
|
|
from common.drf.fields import ObjectRelatedField, LabeledChoiceField
|
|
|
|
|
|
|
|
from common.drf.serializers import SecretReadableMixin, BulkModelSerializer
|
|
|
|
from .base import BaseAccountSerializer
|
|
|
|
from .base import BaseAccountSerializer
|
|
|
|
from assets.const import SecretType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountSerializerCreateMixin(serializers.ModelSerializer):
|
|
|
|
class AccountSerializerCreateMixin(BulkModelSerializer):
|
|
|
|
template = serializers.UUIDField(
|
|
|
|
template = serializers.UUIDField(
|
|
|
|
required=False, allow_null=True, write_only=True,
|
|
|
|
required=False, allow_null=True, write_only=True,
|
|
|
|
label=_('Account template')
|
|
|
|
label=_('Account template')
|
|
|
@ -53,11 +53,27 @@ class AccountSerializerCreateMixin(serializers.ModelSerializer):
|
|
|
|
return instance
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountAssetSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
platform = ObjectRelatedField(read_only=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = Asset
|
|
|
|
|
|
|
|
fields = ['id', 'name', 'address', 'platform']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
|
|
|
|
if isinstance(data, dict):
|
|
|
|
|
|
|
|
i = data.get('id')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
i = data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
return Asset.objects.get(id=i)
|
|
|
|
|
|
|
|
except Asset.DoesNotExist:
|
|
|
|
|
|
|
|
raise serializers.ValidationError(_('Asset not found'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountSerializer(AccountSerializerCreateMixin, BaseAccountSerializer):
|
|
|
|
class AccountSerializer(AccountSerializerCreateMixin, BaseAccountSerializer):
|
|
|
|
asset = ObjectRelatedField(
|
|
|
|
asset = AccountAssetSerializer(label=_('Asset'))
|
|
|
|
required=False, queryset=Asset.objects,
|
|
|
|
|
|
|
|
label=_('Asset'), attrs=('id', 'name', 'address', 'platform_id')
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
su_from = ObjectRelatedField(
|
|
|
|
su_from = ObjectRelatedField(
|
|
|
|
required=False, queryset=Account.objects, allow_null=True, allow_empty=True,
|
|
|
|
required=False, queryset=Account.objects, allow_null=True, allow_empty=True,
|
|
|
|
label=_('Su from'), attrs=('id', 'name', 'username')
|
|
|
|
label=_('Su from'), attrs=('id', 'name', 'username')
|
|
|
@ -66,22 +82,17 @@ class AccountSerializer(AccountSerializerCreateMixin, BaseAccountSerializer):
|
|
|
|
class Meta(BaseAccountSerializer.Meta):
|
|
|
|
class Meta(BaseAccountSerializer.Meta):
|
|
|
|
model = Account
|
|
|
|
model = Account
|
|
|
|
fields = BaseAccountSerializer.Meta.fields \
|
|
|
|
fields = BaseAccountSerializer.Meta.fields \
|
|
|
|
+ ['su_from', 'version', 'asset'] \
|
|
|
|
+ ['su_from', 'version', 'asset'] \
|
|
|
|
+ ['template', 'push_now']
|
|
|
|
+ ['template', 'push_now']
|
|
|
|
extra_kwargs = {
|
|
|
|
extra_kwargs = {
|
|
|
|
**BaseAccountSerializer.Meta.extra_kwargs,
|
|
|
|
**BaseAccountSerializer.Meta.extra_kwargs,
|
|
|
|
'name': {'required': False, 'allow_null': True},
|
|
|
|
'name': {'required': False, 'allow_null': True},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, data=None, **kwargs):
|
|
|
|
def validate_name(self, value):
|
|
|
|
super().__init__(*args, data=data, **kwargs)
|
|
|
|
if not value:
|
|
|
|
if data and 'name' not in data:
|
|
|
|
value = self.initial_data.get('username')
|
|
|
|
username = data.get('username')
|
|
|
|
return value
|
|
|
|
if username is not None:
|
|
|
|
|
|
|
|
data['name'] = username
|
|
|
|
|
|
|
|
if hasattr(self, 'initial_data') and \
|
|
|
|
|
|
|
|
not getattr(self, 'initial_data', None):
|
|
|
|
|
|
|
|
delattr(self, 'initial_data')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|