Merge pull request #2767 from jumpserver/dev

[Update] 解决csv导入导出的问题
pull/2790/head^2
BaiJiangJie 6 years ago committed by GitHub
commit 1631c32868
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@
import json import json
import chardet import chardet
import codecs
import unicodecsv import unicodecsv
from rest_framework.parsers import BaseParser from rest_framework.parsers import BaseParser
@ -82,6 +83,7 @@ class JMSCSVParser(BaseParser):
try: try:
stream_data = stream.read() stream_data = stream.read()
stream_data = stream_data.strip(codecs.BOM_UTF8)
detect_result = chardet.detect(stream_data) detect_result = chardet.detect(stream_data)
encoding = detect_result.get("encoding", "utf-8") encoding = detect_result.get("encoding", "utf-8")
binary = self._universal_newlines(stream_data) binary = self._universal_newlines(stream_data)

@ -2,6 +2,7 @@
# #
import unicodecsv import unicodecsv
import codecs
from datetime import datetime from datetime import datetime
from six import BytesIO from six import BytesIO
@ -73,7 +74,8 @@ class JMSCSVRender(BaseRenderer):
table = self._gen_table(data, header, labels) table = self._gen_table(data, header, labels)
csv_buffer = BytesIO() csv_buffer = BytesIO()
csv_writer = unicodecsv.writer(csv_buffer, encoding='utf-8-sig') csv_buffer.write(codecs.BOM_UTF8)
csv_writer = unicodecsv.writer(csv_buffer, encoding='utf-8')
for row in table: for row in table:
csv_writer.writerow(row) csv_writer.writerow(row)

Loading…
Cancel
Save