perf: account template secret (#9368)

Co-authored-by: feng <1304903146@qq.com>
pull/9369/head
fit2bot 2 years ago committed by GitHub
parent 28d117bf3d
commit f18f2df4ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,7 +10,25 @@ from common.serializers import SecretReadableMixin, BulkModelSerializer
from .base import BaseAccountSerializer from .base import BaseAccountSerializer
class AccountSerializerCreateMixin(BulkModelSerializer): class AccountSerializerCreateValidateMixin:
replace_attrs: callable
push_now: bool
def validate(self, attrs):
_id = attrs.pop('id', None)
if _id:
account_template = AccountTemplate.objects.get(id=_id)
attrs['secret'] = account_template.secret
account_template = attrs.pop('template', None)
if account_template:
self.replace_attrs(account_template, attrs)
self.push_now = attrs.pop('push_now', False)
return super().validate(attrs)
class AccountSerializerCreateMixin(
AccountSerializerCreateValidateMixin, 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')
@ -40,13 +58,6 @@ class AccountSerializerCreateMixin(BulkModelSerializer):
for k, v in template_attrs.items(): for k, v in template_attrs.items():
attrs.setdefault(k, v) attrs.setdefault(k, v)
def validate(self, attrs):
account_template = attrs.pop('template', None)
if account_template:
self.replace_attrs(account_template, attrs)
self.push_now = attrs.pop('push_now', False)
return super().validate(attrs)
def create(self, validated_data): def create(self, validated_data):
instance = super().create(validated_data) instance = super().create(validated_data)
if self.push_now: if self.push_now:

@ -7,6 +7,7 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from accounts.models import Account, AccountTemplate from accounts.models import Account, AccountTemplate
from accounts.serializers import AccountSerializerCreateValidateMixin
from common.serializers import WritableNestedModelSerializer, SecretReadableMixin, CommonModelSerializer from common.serializers import WritableNestedModelSerializer, SecretReadableMixin, CommonModelSerializer
from common.serializers.fields import LabeledChoiceField from common.serializers.fields import LabeledChoiceField
from orgs.mixins.serializers import BulkOrgResourceModelSerializer from orgs.mixins.serializers import BulkOrgResourceModelSerializer
@ -46,7 +47,9 @@ class AssetPlatformSerializer(serializers.ModelSerializer):
} }
class AssetAccountSerializer(CommonModelSerializer): class AssetAccountSerializer(
AccountSerializerCreateValidateMixin, CommonModelSerializer
):
add_org_fields = False add_org_fields = False
push_now = serializers.BooleanField( push_now = serializers.BooleanField(
default=False, label=_("Push now"), write_only=True default=False, label=_("Push now"), write_only=True
@ -91,13 +94,6 @@ class AssetAccountSerializer(CommonModelSerializer):
for k, v in template_attrs.items(): for k, v in template_attrs.items():
attrs.setdefault(k, v) attrs.setdefault(k, v)
def validate(self, attrs):
account_template = attrs.pop('template', None)
if account_template:
self.replace_attrs(account_template, attrs)
self.push_now = attrs.pop('push_now', False)
return super().validate(attrs)
def create(self, validated_data): def create(self, validated_data):
from accounts.tasks import push_accounts_to_assets from accounts.tasks import push_accounts_to_assets
instance = super().create(validated_data) instance = super().create(validated_data)
@ -240,7 +236,6 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
class DetailMixin(serializers.Serializer): class DetailMixin(serializers.Serializer):
accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts')) accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts'))
def get_field_names(self, declared_fields, info): def get_field_names(self, declared_fields, info):
names = super().get_field_names(declared_fields, info) names = super().get_field_names(declared_fields, info)
names.extend([ names.extend([

Loading…
Cancel
Save