mirror of https://github.com/jumpserver/jumpserver
commit
d2b1b19404
|
@ -2,6 +2,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import chardet
|
||||||
import unicodecsv
|
import unicodecsv
|
||||||
|
|
||||||
from rest_framework.parsers import BaseParser
|
from rest_framework.parsers import BaseParser
|
||||||
|
@ -73,7 +74,6 @@ class JMSCSVParser(BaseParser):
|
||||||
|
|
||||||
def parse(self, stream, media_type=None, parser_context=None):
|
def parse(self, stream, media_type=None, parser_context=None):
|
||||||
parser_context = parser_context or {}
|
parser_context = parser_context or {}
|
||||||
encoding = parser_context.get('encoding', 'utf-8')
|
|
||||||
try:
|
try:
|
||||||
serializer = parser_context["view"].get_serializer()
|
serializer = parser_context["view"].get_serializer()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -82,6 +82,8 @@ class JMSCSVParser(BaseParser):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stream_data = stream.read()
|
stream_data = stream.read()
|
||||||
|
detect_result = chardet.detect(stream_data)
|
||||||
|
encoding = detect_result.get("encoding", "utf-8")
|
||||||
binary = self._universal_newlines(stream_data)
|
binary = self._universal_newlines(stream_data)
|
||||||
rows = self._gen_rows(binary, charset=encoding)
|
rows = self._gen_rows(binary, charset=encoding)
|
||||||
|
|
||||||
|
@ -97,5 +99,5 @@ class JMSCSVParser(BaseParser):
|
||||||
data.append(row_data)
|
data.append(row_data)
|
||||||
return data
|
return data
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
raise ParseError('CSV parse error!')
|
raise ParseError('CSV parse error!')
|
||||||
|
|
|
@ -53,7 +53,6 @@ class JMSCSVRender(BaseRenderer):
|
||||||
|
|
||||||
def render(self, data, media_type=None, renderer_context=None):
|
def render(self, data, media_type=None, renderer_context=None):
|
||||||
renderer_context = renderer_context or {}
|
renderer_context = renderer_context or {}
|
||||||
encoding = renderer_context.get('encoding', 'utf-8')
|
|
||||||
request = renderer_context['request']
|
request = renderer_context['request']
|
||||||
template = request.query_params.get('template', 'export')
|
template = request.query_params.get('template', 'export')
|
||||||
view = renderer_context['view']
|
view = renderer_context['view']
|
||||||
|
@ -74,7 +73,7 @@ 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=encoding)
|
csv_writer = unicodecsv.writer(csv_buffer, encoding='utf-8-sig')
|
||||||
for row in table:
|
for row in table:
|
||||||
csv_writer.writerow(row)
|
csv_writer.writerow(row)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue