From 2981bfffb16bbf4b815717daaae2ee012f7582c1 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Thu, 18 May 2023 18:33:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=99=20view=20=E5=A2=9E=E5=8A=A0=20?= =?UTF-8?q?export=20=E5=B1=9E=E6=80=A7=20(#10495)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- apps/accounts/api/account/account.py | 1 + apps/common/drf/renders/base.py | 11 ++++------- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/apps/accounts/api/account/account.py b/apps/accounts/api/account/account.py index b25dafb9f..6fb699721 100644 --- a/apps/accounts/api/account/account.py +++ b/apps/accounts/api/account/account.py @@ -32,6 +32,7 @@ class AccountViewSet(OrgBulkModelViewSet): 'su_from_accounts': 'accounts.view_account', 'clear_secret': 'accounts.change_account', } + export_as_zip = True @action(methods=['get'], detail=False, url_path='su-from-accounts') def su_from_accounts(self, request, *args, **kwargs): diff --git a/apps/common/drf/renders/base.py b/apps/common/drf/renders/base.py index cc92eb8c2..a9b3b5c8a 100644 --- a/apps/common/drf/renders/base.py +++ b/apps/common/drf/renders/base.py @@ -184,19 +184,16 @@ class BaseFileRenderer(BaseRenderer): self.write_column_titles(column_titles) self.write_rows(rows) self.after_render() - value = self.compress_into_zip_file(view, request, response) + value = self.get_rendered_value() + if getattr(view, 'export_as_zip', False) and self.template == 'export': + value = self.compress_into_zip_file(value, request, response) except Exception as e: logger.debug(e, exc_info=True) value = 'Render error! ({})'.format(self.media_type).encode('utf-8') return value return value - def compress_into_zip_file(self, view, request, response): - value = self.get_rendered_value() - from accounts.models import Account - if str(view.model) not in (str(Account),) or self.template != 'export': - return value - + def compress_into_zip_file(self, value, request, response): filename_pattern = re.compile(r'filename="([^"]+)"') content_disposition = response['Content-Disposition'] match = filename_pattern.search(content_disposition)