feat: 添加批量导入时将其他格式(csv, excel)解析为Json的接口

pull/5706/head
xinwen 2021-03-08 16:55:21 +08:00 committed by 老广
parent 15b0ad9c12
commit 886393c539
3 changed files with 28 additions and 21 deletions

View File

@ -3,39 +3,35 @@ from rest_framework_bulk import BulkModelViewSet
from ..mixins.api import (
SerializerMixin2, QuerySetMixin, ExtraFilterFieldsMixin, PaginatedResponseMixin,
RelationMixin, AllowBulkDestoryMixin
RelationMixin, AllowBulkDestoryMixin, ParseToJsonMixin,
)
class JmsGenericViewSet(SerializerMixin2,
QuerySetMixin,
ExtraFilterFieldsMixin,
PaginatedResponseMixin,
class CommonMixin(SerializerMixin2,
QuerySetMixin,
ExtraFilterFieldsMixin,
PaginatedResponseMixin,
ParseToJsonMixin):
pass
class JmsGenericViewSet(CommonMixin,
GenericViewSet):
pass
class JMSModelViewSet(SerializerMixin2,
QuerySetMixin,
ExtraFilterFieldsMixin,
PaginatedResponseMixin,
class JMSModelViewSet(CommonMixin,
ModelViewSet):
pass
class JMSBulkModelViewSet(SerializerMixin2,
QuerySetMixin,
ExtraFilterFieldsMixin,
PaginatedResponseMixin,
class JMSBulkModelViewSet(CommonMixin,
AllowBulkDestoryMixin,
BulkModelViewSet):
pass
class JMSBulkRelationModelViewSet(SerializerMixin2,
QuerySetMixin,
ExtraFilterFieldsMixin,
PaginatedResponseMixin,
class JMSBulkRelationModelViewSet(CommonMixin,
RelationMixin,
AllowBulkDestoryMixin,
BulkModelViewSet):

View File

@ -22,6 +22,7 @@ class BaseFileParser(BaseParser):
FILE_CONTENT_MAX_LENGTH = 1024 * 1024 * 10
serializer_cls = None
serializer_fields = None
def check_content_length(self, meta):
content_length = int(meta.get('CONTENT_LENGTH', meta.get('HTTP_CONTENT_LENGTH', 0)))
@ -45,7 +46,7 @@ class BaseFileParser(BaseParser):
def convert_to_field_names(self, column_titles):
fields_map = {}
fields = self.serializer_cls().fields
fields = self.serializer_fields
fields_map.update({v.label: k for k, v in fields.items()})
fields_map.update({k: k for k, _ in fields.items()})
field_names = [
@ -89,7 +90,7 @@ class BaseFileParser(BaseParser):
构建json数据后的行数据处理
"""
new_row_data = {}
serializer_fields = self.serializer_cls().fields
serializer_fields = self.serializer_fields
for k, v in row_data.items():
if isinstance(v, list) or isinstance(v, dict) or isinstance(v, str) and k.strip() and v.strip():
# 解决类似disk_info为字符串的'{}'的问题
@ -117,6 +118,7 @@ class BaseFileParser(BaseParser):
view = parser_context['view']
meta = view.request.META
self.serializer_cls = view.get_serializer_class()
self.serializer_fields = self.serializer_cls().fields
except Exception as e:
logger.debug(e, exc_info=True)
raise ParseError('The resource does not support imports!')

View File

@ -11,13 +11,16 @@ from django.core.cache import cache
from django.http import JsonResponse
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.decorators import action
from rest_framework.request import Request
from common.const.http import POST
from common.drf.filters import IDSpmFilter, CustomFilter, IDInFilter
from ..utils import lazyproperty
__all__ = [
'JSONResponseMixin', 'CommonApiMixin', 'AsyncApiMixin', 'RelationMixin',
'SerializerMixin2', 'QuerySetMixin', 'ExtraFilterFieldsMixin'
'SerializerMixin2', 'QuerySetMixin', 'ExtraFilterFieldsMixin', 'ParseToJsonMixin',
]
@ -32,6 +35,12 @@ class JSONResponseMixin(object):
# ----------------------
class ParseToJsonMixin:
@action(methods=[POST], detail=False, url_path='render-to-json')
def render_to_json(self, request: Request):
return Response(data=request.data)
class SerializerMixin:
""" 根据用户请求动作的不同,获取不同的 `serializer_class `"""
@ -98,7 +107,7 @@ class PaginatedResponseMixin:
return Response(serializer.data)
class CommonApiMixin(SerializerMixin, ExtraFilterFieldsMixin):
class CommonApiMixin(SerializerMixin, ExtraFilterFieldsMixin, ParseToJsonMixin):
pass