perf: 修复资产导入账号模版失败问题 导入文件不区分大小写 (#11031)

Co-authored-by: feng <1304903146@qq.com>
pull/11033/head
fit2bot 2023-07-20 14:57:51 +08:00 committed by GitHub
parent c50330e055
commit 19b91a6c1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -124,6 +124,7 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'), default=()) protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'), default=())
accounts = AssetAccountSerializer(many=True, required=False, allow_null=True, write_only=True, label=_('Account')) accounts = AssetAccountSerializer(many=True, required=False, allow_null=True, write_only=True, label=_('Account'))
nodes_display = serializers.ListField(read_only=False, required=False, label=_("Node path")) nodes_display = serializers.ListField(read_only=False, required=False, label=_("Node path"))
_accounts = None
class Meta: class Meta:
model = Asset model = Asset
@ -151,6 +152,13 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._init_field_choices() self._init_field_choices()
self._extract_accounts()
def _extract_accounts(self):
if not getattr(self, 'initial_data', None):
return
accounts = self.initial_data.pop('accounts', None)
self._accounts = accounts
def _get_protocols_required_default(self): def _get_protocols_required_default(self):
platform = self._asset_platform platform = self._asset_platform
@ -276,7 +284,6 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
return return
for data in accounts_data: for data in accounts_data:
data['asset'] = asset.id data['asset'] = asset.id
s = AssetAccountSerializer(data=accounts_data, many=True) s = AssetAccountSerializer(data=accounts_data, many=True)
s.is_valid(raise_exception=True) s.is_valid(raise_exception=True)
s.save() s.save()
@ -284,16 +291,13 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
@atomic @atomic
def create(self, validated_data): def create(self, validated_data):
nodes_display = validated_data.pop('nodes_display', '') nodes_display = validated_data.pop('nodes_display', '')
accounts = validated_data.pop('accounts', [])
instance = super().create(validated_data) instance = super().create(validated_data)
self.accounts_create(accounts, instance) self.accounts_create(self._accounts, instance)
self.perform_nodes_display_create(instance, nodes_display) self.perform_nodes_display_create(instance, nodes_display)
return instance return instance
@atomic @atomic
def update(self, instance, validated_data): def update(self, instance, validated_data):
if not validated_data.get('accounts'):
validated_data.pop('accounts', None)
nodes_display = validated_data.pop('nodes_display', '') nodes_display = validated_data.pop('nodes_display', '')
instance = super().update(instance, validated_data) instance = super().update(instance, validated_data)
self.perform_nodes_display_create(instance, nodes_display) self.perform_nodes_display_create(instance, nodes_display)

View File

@ -59,8 +59,9 @@ class BaseFileParser(BaseParser):
v.label: k, v.label: k,
k: k k: k
}) })
lowercase_fields_map = {k.lower(): v for k, v in fields_map.items()}
field_names = [ field_names = [
fields_map.get(column_title.strip('*').lower(), '') lowercase_fields_map.get(column_title.strip('*').lower(), '')
for column_title in column_titles for column_title in column_titles
] ]
return field_names return field_names