feat: base 使用custommodelmixin替代listmodelmixin以定制返回数据
This commit is contained in:
parent
a9b2d67302
commit
ac524778e3
|
@ -1,5 +1,5 @@
|
||||||
import uuid
|
import uuid
|
||||||
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin, DestroyModelMixin
|
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin, DestroyModelMixin, ListModelMixin
|
||||||
import ast
|
import ast
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -14,6 +14,7 @@ from rest_framework.exceptions import ParseError, ValidationError
|
||||||
from apps.utils.errors import PKS_ERROR
|
from apps.utils.errors import PKS_ERROR
|
||||||
from rest_framework.generics import get_object_or_404
|
from rest_framework.generics import get_object_or_404
|
||||||
from drf_yasg.utils import swagger_auto_schema
|
from drf_yasg.utils import swagger_auto_schema
|
||||||
|
from drf_yasg import openapi
|
||||||
from apps.utils.serializers import PkSerializer
|
from apps.utils.serializers import PkSerializer
|
||||||
|
|
||||||
# 实例化myLogger
|
# 实例化myLogger
|
||||||
|
@ -173,6 +174,30 @@ class BulkDestroyModelMixin(DestroyModelMixin):
|
||||||
self.perform_destroy(instance)
|
self.perform_destroy(instance)
|
||||||
return Response(status=204)
|
return Response(status=204)
|
||||||
|
|
||||||
|
class CustomListModelMixin(ListModelMixin):
|
||||||
|
@swagger_auto_schema(manual_parameters=[
|
||||||
|
openapi.Parameter(name="query", in_=openapi.IN_QUERY, description="定制返回数据",
|
||||||
|
type=openapi.TYPE_STRING, required=False),
|
||||||
|
])
|
||||||
|
def list(self, request, *args, **kwargs):
|
||||||
|
queryset = self.filter_queryset(self.get_queryset())
|
||||||
|
|
||||||
|
page = self.paginate_queryset(queryset)
|
||||||
|
if page is not None:
|
||||||
|
serializer = self.get_serializer(page, many=True)
|
||||||
|
data = self.add_info_for_list(serializer.data)
|
||||||
|
return self.get_paginated_response(data)
|
||||||
|
|
||||||
|
serializer = self.get_serializer(queryset, many=True)
|
||||||
|
data = self.add_info_for_list(serializer.data)
|
||||||
|
return Response(data)
|
||||||
|
|
||||||
|
def add_info_for_list(self, data):
|
||||||
|
"""给list返回数据添加额外信息
|
||||||
|
|
||||||
|
给list返回数据添加额外信息
|
||||||
|
"""
|
||||||
|
return data
|
||||||
|
|
||||||
class MyLoggingMixin(object):
|
class MyLoggingMixin(object):
|
||||||
"""Mixin to log requests"""
|
"""Mixin to log requests"""
|
||||||
|
|
|
@ -1,26 +1,20 @@
|
||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.exceptions import ValidationError, ParseError
|
from rest_framework.exceptions import ParseError
|
||||||
from rest_framework.mixins import (CreateModelMixin, ListModelMixin,
|
from rest_framework.mixins import RetrieveModelMixin
|
||||||
RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin)
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.permissions import IsAuthenticated, IsAdminUser
|
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.viewsets import GenericViewSet
|
from rest_framework.viewsets import GenericViewSet
|
||||||
|
|
||||||
from apps.system.models import DataFilter, Dept, User
|
from apps.system.models import DataFilter, Dept
|
||||||
from apps.utils.errors import PKS_ERROR
|
from apps.utils.mixins import MyLoggingMixin, BulkCreateModelMixin, BulkUpdateModelMixin, BulkDestroyModelMixin, CustomListModelMixin
|
||||||
from apps.utils.mixins import MyLoggingMixin, BulkCreateModelMixin, BulkUpdateModelMixin, BulkDestroyModelMixin
|
|
||||||
from apps.utils.permission import ALL_PERMS, RbacPermission, get_user_perms_map
|
from apps.utils.permission import ALL_PERMS, RbacPermission, get_user_perms_map
|
||||||
from apps.utils.queryset import get_child_queryset2, get_child_queryset_u
|
from apps.utils.queryset import get_child_queryset2, get_child_queryset_u
|
||||||
from apps.utils.serializers import PkSerializer, ComplexSerializer
|
from apps.utils.serializers import ComplexSerializer
|
||||||
from rest_framework.throttling import UserRateThrottle
|
from rest_framework.throttling import UserRateThrottle
|
||||||
from drf_yasg.utils import swagger_auto_schema
|
from drf_yasg.utils import swagger_auto_schema
|
||||||
from drf_yasg import openapi
|
|
||||||
from apps.utils.decorators import idempotent
|
|
||||||
from django.db import transaction
|
|
||||||
import json
|
import json
|
||||||
from rest_framework.generics import get_object_or_404
|
|
||||||
|
|
||||||
|
|
||||||
class CustomGenericViewSet(MyLoggingMixin, GenericViewSet):
|
class CustomGenericViewSet(MyLoggingMixin, GenericViewSet):
|
||||||
|
@ -184,7 +178,7 @@ class CustomGenericViewSet(MyLoggingMixin, GenericViewSet):
|
||||||
return queryset.filter(create_by=self.request.user)
|
return queryset.filter(create_by=self.request.user)
|
||||||
|
|
||||||
|
|
||||||
class CustomModelViewSet(BulkCreateModelMixin, BulkUpdateModelMixin, ListModelMixin,
|
class CustomModelViewSet(BulkCreateModelMixin, BulkUpdateModelMixin, CustomListModelMixin,
|
||||||
RetrieveModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
|
RetrieveModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
|
||||||
"""
|
"""
|
||||||
增强的ModelViewSet
|
增强的ModelViewSet
|
||||||
|
@ -201,29 +195,6 @@ class CustomModelViewSet(BulkCreateModelMixin, BulkUpdateModelMixin, ListModelMi
|
||||||
if v not in ALL_PERMS and v != '*':
|
if v not in ALL_PERMS and v != '*':
|
||||||
ALL_PERMS.append(v)
|
ALL_PERMS.append(v)
|
||||||
|
|
||||||
@swagger_auto_schema(manual_parameters=[
|
|
||||||
openapi.Parameter(name="query", in_=openapi.IN_QUERY, description="定制返回数据",
|
|
||||||
type=openapi.TYPE_STRING, required=False),
|
|
||||||
])
|
|
||||||
def list(self, request, *args, **kwargs):
|
|
||||||
queryset = self.filter_queryset(self.get_queryset())
|
|
||||||
|
|
||||||
page = self.paginate_queryset(queryset)
|
|
||||||
if page is not None:
|
|
||||||
serializer = self.get_serializer(page, many=True)
|
|
||||||
data = self.add_info_for_list(serializer.data)
|
|
||||||
return self.get_paginated_response(data)
|
|
||||||
|
|
||||||
serializer = self.get_serializer(queryset, many=True)
|
|
||||||
data = self.add_info_for_list(serializer.data)
|
|
||||||
return Response(data)
|
|
||||||
|
|
||||||
def add_info_for_list(self, data):
|
|
||||||
"""给list返回数据添加额外信息
|
|
||||||
|
|
||||||
给list返回数据添加额外信息
|
|
||||||
"""
|
|
||||||
return data
|
|
||||||
|
|
||||||
@swagger_auto_schema(request_body=ComplexSerializer, responses={200: {}})
|
@swagger_auto_schema(request_body=ComplexSerializer, responses={200: {}})
|
||||||
@action(methods=['post'], detail=False, perms_map={'post': '*'})
|
@action(methods=['post'], detail=False, perms_map={'post': '*'})
|
||||||
|
|
Loading…
Reference in New Issue