perf: 导入资产账号模版api优化 (#10038)

Co-authored-by: feng <1304903146@qq.com>
Co-authored-by: feng626 <57284900+feng626@users.noreply.github.com>
pull/10039/head
fit2bot 2 years ago committed by GitHub
parent 402c68edd0
commit f7ae23f7d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,8 +1,7 @@
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from rest_framework.validators import (
UniqueTogetherValidator
)
from rest_framework.generics import get_object_or_404
from rest_framework.validators import UniqueTogetherValidator
from accounts import validator
from accounts.const import SecretType, Source, BulkCreateStrategy
@ -29,16 +28,25 @@ class AccountSerializerCreateValidateMixin:
ret = super().to_internal_value(data)
self.from_id = from_id
return ret
@staticmethod
def related_template_values(template: AccountTemplate, attrs):
ignore_fields = ['id', 'date_created', 'date_updated', 'org_id']
field_names = [
field.name for field in template._meta.fields
if field.name not in ignore_fields
]
for name in field_names:
attrs[name] = attrs.get(name) or getattr(template, name)
def set_secret(self, attrs):
_id = self.from_id
template = attrs.pop('template', None)
if _id and template:
account_template = AccountTemplate.objects.get(id=_id)
attrs['secret'] = account_template.secret
account_template = get_object_or_404(AccountTemplate, id=_id)
self.related_template_values(account_template, attrs)
elif _id and not template:
account = Account.objects.get(id=_id)
account = get_object_or_404(Account, id=_id)
attrs['secret'] = account.secret
return attrs

@ -6,9 +6,9 @@ from django.db.transaction import atomic
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from accounts.const import SecretType
from accounts.models import Account
from accounts.serializers import AccountSerializerCreateValidateMixin
from accounts.serializers import AuthValidateMixin
from accounts.serializers import AuthValidateMixin, AccountSerializerCreateValidateMixin
from common.serializers import WritableNestedModelSerializer, SecretReadableMixin, CommonModelSerializer
from common.serializers.fields import LabeledChoiceField
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
@ -72,7 +72,10 @@ class AssetAccountSerializer(
default=False, label=_("Template"), write_only=True
)
name = serializers.CharField(max_length=128, required=False, label=_("Name"))
secret_type = serializers.CharField(max_length=64, default='password', label=_("Secret type"))
secret_type = LabeledChoiceField(
choices=SecretType.choices, default=SecretType.PASSWORD,
required=False, label=_('Secret type')
)
class Meta:
model = Account

Loading…
Cancel
Save