mirror of https://github.com/jumpserver/jumpserver
feat: 批量导入解析为Json的接口添加 `title` 字段
parent
886393c539
commit
ccb0509d85
|
@ -3,7 +3,7 @@ from rest_framework_bulk import BulkModelViewSet
|
||||||
|
|
||||||
from ..mixins.api import (
|
from ..mixins.api import (
|
||||||
SerializerMixin2, QuerySetMixin, ExtraFilterFieldsMixin, PaginatedResponseMixin,
|
SerializerMixin2, QuerySetMixin, ExtraFilterFieldsMixin, PaginatedResponseMixin,
|
||||||
RelationMixin, AllowBulkDestoryMixin, ParseToJsonMixin,
|
RelationMixin, AllowBulkDestoryMixin, RenderToJsonMixin,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ class CommonMixin(SerializerMixin2,
|
||||||
QuerySetMixin,
|
QuerySetMixin,
|
||||||
ExtraFilterFieldsMixin,
|
ExtraFilterFieldsMixin,
|
||||||
PaginatedResponseMixin,
|
PaginatedResponseMixin,
|
||||||
ParseToJsonMixin):
|
RenderToJsonMixin):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -112,11 +112,13 @@ class BaseFileParser(BaseParser):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
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 {}
|
assert parser_context is not None, '`parser_context` should not be `None`'
|
||||||
|
|
||||||
|
view = parser_context['view']
|
||||||
|
request = view.request
|
||||||
|
|
||||||
try:
|
try:
|
||||||
view = parser_context['view']
|
meta = request.META
|
||||||
meta = view.request.META
|
|
||||||
self.serializer_cls = view.get_serializer_class()
|
self.serializer_cls = view.get_serializer_class()
|
||||||
self.serializer_fields = self.serializer_cls().fields
|
self.serializer_fields = self.serializer_cls().fields
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -130,6 +132,13 @@ class BaseFileParser(BaseParser):
|
||||||
rows = self.generate_rows(stream_data)
|
rows = self.generate_rows(stream_data)
|
||||||
column_titles = self.get_column_titles(rows)
|
column_titles = self.get_column_titles(rows)
|
||||||
field_names = self.convert_to_field_names(column_titles)
|
field_names = self.convert_to_field_names(column_titles)
|
||||||
|
|
||||||
|
# 给 `common.mixins.api.RenderToJsonMixin` 提供,暂时只能耦合
|
||||||
|
column_title_field_pairs = list(zip(column_titles, field_names))
|
||||||
|
if not hasattr(request, 'jms_context'):
|
||||||
|
request.jms_context = {}
|
||||||
|
request.jms_context['column_title_field_pairs'] = column_title_field_pairs
|
||||||
|
|
||||||
data = self.generate_data(field_names, rows)
|
data = self.generate_data(field_names, rows)
|
||||||
return data
|
return data
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -20,7 +20,7 @@ from ..utils import lazyproperty
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'JSONResponseMixin', 'CommonApiMixin', 'AsyncApiMixin', 'RelationMixin',
|
'JSONResponseMixin', 'CommonApiMixin', 'AsyncApiMixin', 'RelationMixin',
|
||||||
'SerializerMixin2', 'QuerySetMixin', 'ExtraFilterFieldsMixin', 'ParseToJsonMixin',
|
'SerializerMixin2', 'QuerySetMixin', 'ExtraFilterFieldsMixin', 'RenderToJsonMixin',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,10 +35,19 @@ class JSONResponseMixin(object):
|
||||||
# ----------------------
|
# ----------------------
|
||||||
|
|
||||||
|
|
||||||
class ParseToJsonMixin:
|
class RenderToJsonMixin:
|
||||||
@action(methods=[POST], detail=False, url_path='render-to-json')
|
@action(methods=[POST], detail=False, url_path='render-to-json')
|
||||||
def render_to_json(self, request: Request):
|
def render_to_json(self, request: Request):
|
||||||
return Response(data=request.data)
|
data = {
|
||||||
|
'title': (),
|
||||||
|
'data': request.data,
|
||||||
|
}
|
||||||
|
|
||||||
|
jms_context = getattr(request, 'jms_context', {})
|
||||||
|
column_title_field_pairs = jms_context.get('column_title_field_pairs', ())
|
||||||
|
data['title'] = column_title_field_pairs
|
||||||
|
|
||||||
|
return Response(data=data)
|
||||||
|
|
||||||
|
|
||||||
class SerializerMixin:
|
class SerializerMixin:
|
||||||
|
@ -107,7 +116,7 @@ class PaginatedResponseMixin:
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
class CommonApiMixin(SerializerMixin, ExtraFilterFieldsMixin, ParseToJsonMixin):
|
class CommonApiMixin(SerializerMixin, ExtraFilterFieldsMixin, RenderToJsonMixin):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue