You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.8 KiB
52 lines
1.8 KiB
#!/usr/bin/env python
|
|
# -*- coding:utf-8 -*-
|
|
# project: 1月
|
|
# author: NinEveN
|
|
# date: 2022/1/6
|
|
from rest_framework.pagination import PageNumberPagination
|
|
|
|
from rest_framework.response import Response
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
|
|
class AppsPageNumber(PageNumberPagination):
|
|
page_size = 20 # 每页显示多少条
|
|
page_size_query_param = 'limit' # URL中每页显示条数的参数
|
|
page_query_param = 'page' # URL中页码的参数
|
|
max_page_size = 100 # 最大页码数限制
|
|
|
|
|
|
class BaseModelSet(ModelViewSet):
|
|
def retrieve(self, request, *args, **kwargs):
|
|
data = super().retrieve(request, *args, **kwargs).data
|
|
return ApiResponse(data=data)
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
data = super().list(request, *args, **kwargs).data
|
|
return ApiResponse(data=data)
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
instance = self.get_object()
|
|
self.perform_destroy(instance)
|
|
return ApiResponse()
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
data = super().update(request, *args, **kwargs).data
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
class ApiResponse(Response):
|
|
def __init__(self, code=1000, msg='success', data=None, status=None, headers=None, content_type=None, **kwargs):
|
|
dic = {
|
|
'code': code,
|
|
'msg': msg
|
|
}
|
|
if data is not None:
|
|
dic['data'] = data
|
|
dic.update(kwargs)
|
|
self._data = data
|
|
# 对象来调用对象的绑定方法,会自动传值
|
|
super().__init__(data=dic, status=status, headers=headers, content_type=content_type)
|
|
|
|
# 类来调用对象的绑定方法,这个方法就是一个普通函数,有几个参数就要传几个参数
|
|
# Response.__init__(data=dic,status=status,headers=headers,content_type=content_type)
|
|
|