perf: 创建资产通过账号模版创建账号 (#10234)

Co-authored-by: feng <1304903146@qq.com>
pull/10239/head
fit2bot 2023-04-18 17:03:49 +08:00 committed by GitHub
parent 52891bfca3
commit cf197f7efc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 13 deletions

View File

@ -34,6 +34,7 @@ class AccountCreateUpdateSerializerMixin(serializers.Serializer):
choices=AccountInvalidPolicy.choices, default=AccountInvalidPolicy.ERROR, choices=AccountInvalidPolicy.choices, default=AccountInvalidPolicy.ERROR,
write_only=True, label=_('Exist policy') write_only=True, label=_('Exist policy')
) )
_template = None
class Meta: class Meta:
fields = ['template', 'push_now', 'params', 'on_invalid'] fields = ['template', 'push_now', 'params', 'on_invalid']
@ -69,9 +70,8 @@ class AccountCreateUpdateSerializerMixin(serializers.Serializer):
name = name + '_' + uuid.uuid4().hex[:4] name = name + '_' + uuid.uuid4().hex[:4]
initial_data['name'] = name initial_data['name'] = name
@staticmethod def from_template_if_need(self, initial_data):
def from_template_if_need(initial_data): template_id = initial_data.pop('template', None)
template_id = initial_data.get('template')
if not template_id: if not template_id:
return return
if isinstance(template_id, (str, uuid.UUID)): if isinstance(template_id, (str, uuid.UUID)):
@ -81,6 +81,7 @@ class AccountCreateUpdateSerializerMixin(serializers.Serializer):
if not template: if not template:
raise serializers.ValidationError({'template': 'Template not found'}) raise serializers.ValidationError({'template': 'Template not found'})
self._template = template
# Set initial data from template # Set initial data from template
ignore_fields = ['id', 'date_created', 'date_updated', 'org_id'] ignore_fields = ['id', 'date_created', 'date_updated', 'org_id']
field_names = [ field_names = [
@ -137,20 +138,17 @@ class AccountCreateUpdateSerializerMixin(serializers.Serializer):
else: else:
raise serializers.ValidationError('Account already exists') raise serializers.ValidationError('Account already exists')
def validate(self, attrs): def generate_source_data(self, validated_data):
attrs = super().validate(attrs) template = self._template
if self.instance: if template is None:
return attrs return
validated_data['source'] = Source.TEMPLATE
template = attrs.pop('template', None) validated_data['source_id'] = str(template.id)
if template:
attrs['source'] = Source.TEMPLATE
attrs['source_id'] = str(template.id)
return attrs
def create(self, validated_data): def create(self, validated_data):
push_now = validated_data.pop('push_now', None) push_now = validated_data.pop('push_now', None)
params = validated_data.pop('params', None) params = validated_data.pop('params', None)
self.generate_source_data(validated_data)
instance, stat = self.do_create(validated_data) instance, stat = self.do_create(validated_data)
self.push_account_if_need(instance, push_now, params, stat) self.push_account_if_need(instance, push_now, params, stat)
return instance return instance
@ -368,6 +366,7 @@ class AssetAccountBulkSerializer(
def create(self, validated_data): def create(self, validated_data):
push_now = validated_data.pop('push_now', False) push_now = validated_data.pop('push_now', False)
self.generate_source_data(validated_data)
results = self.perform_bulk_create(validated_data) results = self.perform_bulk_create(validated_data)
self.push_accounts_if_need(results, push_now) self.push_accounts_if_need(results, push_now)
for res in results: for res in results: