fix: 给 view 增加 export 属性 (#10495)

Co-authored-by: feng <1304903146@qq.com>
pull/10497/head
fit2bot 2023-05-18 18:33:22 +08:00 committed by GitHub
parent 0596b74fa1
commit 2981bfffb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 7 deletions

View File

@ -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):

View File

@ -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)