Merge pull request #9931 from jumpserver/pr@dev@fix_excel_import_export

perf: 优化 csv import export
pull/9932/head
老广 2023-03-13 18:07:57 +08:00 committed by GitHub
commit 4a3196e193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 4 deletions

View File

@ -271,6 +271,8 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
@staticmethod @staticmethod
def accounts_create(accounts_data, asset): def accounts_create(accounts_data, asset):
if not accounts_data:
return
for data in accounts_data: for data in accounts_data:
data['asset'] = asset data['asset'] = asset
AssetAccountSerializer().create(data) AssetAccountSerializer().create(data)

View File

@ -1,13 +1,17 @@
import pyexcel import pyexcel
from django.utils.translation import gettext as _
from .base import BaseFileParser from .base import BaseFileParser
class ExcelFileParser(BaseFileParser): class ExcelFileParser(BaseFileParser):
media_type = 'text/xlsx' media_type = 'text/xlsx'
def generate_rows(self, stream_data): def generate_rows(self, stream_data):
workbook = pyexcel.get_book(file_type='xlsx', file_content=stream_data) try:
workbook = pyexcel.get_book(file_type='xlsx', file_content=stream_data)
except Exception:
raise Exception(_('Invalid excel file'))
# 默认获取第一个工作表sheet # 默认获取第一个工作表sheet
sheet = workbook.sheet_by_index(0) sheet = workbook.sheet_by_index(0)
rows = sheet.rows() rows = sheet.rows()

View File

@ -5,7 +5,7 @@ from rest_framework import serializers
from rest_framework.renderers import BaseRenderer from rest_framework.renderers import BaseRenderer
from rest_framework.utils import encoders, json from rest_framework.utils import encoders, json
from common.serializers.fields import ObjectRelatedField from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
from common.utils import get_logger from common.utils import get_logger
logger = get_logger(__file__) logger = get_logger(__file__)
@ -100,7 +100,7 @@ class BaseFileRenderer(BaseRenderer):
value = field.to_file_representation(value) value = field.to_file_representation(value)
elif isinstance(value, bool): elif isinstance(value, bool):
value = 'Yes' if value else 'No' value = 'Yes' if value else 'No'
elif isinstance(field, serializers.ChoiceField): elif isinstance(field, LabeledChoiceField):
value = value.get('value', '') value = value.get('value', '')
elif isinstance(field, ObjectRelatedField): elif isinstance(field, ObjectRelatedField):
if field.many: if field.many:

View File

@ -20,6 +20,12 @@ class ActionChoicesField(BitChoicesField):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(choice_cls=ActionChoices, **kwargs) super().__init__(choice_cls=ActionChoices, **kwargs)
def to_file_representation(self, value):
return [v['value'] for v in value]
def to_file_internal_value(self, data):
return data
class AssetPermissionSerializer(BulkOrgResourceModelSerializer): class AssetPermissionSerializer(BulkOrgResourceModelSerializer):
users = ObjectRelatedField(queryset=User.objects, many=True, required=False, label=_('User')) users = ObjectRelatedField(queryset=User.objects, many=True, required=False, label=_('User'))