From f7ae23f7d9a6da8da9bc7939664303778d2a3920 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Wed, 22 Mar 2023 18:35:23 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=AF=BC=E5=85=A5=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E6=A8=A1=E7=89=88api=E4=BC=98=E5=8C=96=20(#1?= =?UTF-8?q?0038)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> Co-authored-by: feng626 <57284900+feng626@users.noreply.github.com> --- apps/accounts/serializers/account/account.py | 20 ++++++++++++++------ apps/assets/serializers/asset/common.py | 9 ++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/apps/accounts/serializers/account/account.py b/apps/accounts/serializers/account/account.py index a2ab1e22e..b8a5d84c0 100644 --- a/apps/accounts/serializers/account/account.py +++ b/apps/accounts/serializers/account/account.py @@ -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 diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index dfcd2de82..9f38156b3 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -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