uomp-drf 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- uomp_drf-0.1.2/PKG-INFO +11 -0
- uomp_drf-0.1.2/README.md +0 -0
- uomp_drf-0.1.2/pyproject.toml +20 -0
- uomp_drf-0.1.2/src/uomp_drf/__init__.py +2 -0
- uomp_drf-0.1.2/src/uomp_drf/authentication.py +11 -0
- uomp_drf-0.1.2/src/uomp_drf/exception.py +38 -0
- uomp_drf-0.1.2/src/uomp_drf/pnp.py +18 -0
- uomp_drf-0.1.2/src/uomp_drf/render.py +44 -0
- uomp_drf-0.1.2/src/uomp_drf/viewsets.py +30 -0
uomp_drf-0.1.2/PKG-INFO
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: uomp-drf
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
Author: freeflyday
|
|
6
|
+
Author-email: freeflyday <freeflyday@163.com>
|
|
7
|
+
Requires-Dist: django-filter==25.1
|
|
8
|
+
Requires-Dist: djangorestframework>=3.17
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
uomp_drf-0.1.2/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "uomp-drf"
|
|
3
|
+
version = "0.1.2"
|
|
4
|
+
description = "Add your description here"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "freeflyday", email = "freeflyday@163.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"django-filter==25.1",
|
|
12
|
+
"djangorestframework>=3.17",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
uomp-drf = "uomp_drf:main"
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["uv_build>=0.11.17,<0.12.0"]
|
|
20
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from rest_framework import permissions
|
|
2
|
+
from rest_framework.authentication import SessionAuthentication as DrfSessionAuthentication
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SessionAuthentication(DrfSessionAuthentication):
|
|
6
|
+
def enforce_csrf(self, request):
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MyPermissions(permissions.BasePermission):
|
|
11
|
+
pass
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# uomp_drf/exception.py
|
|
2
|
+
|
|
3
|
+
from rest_framework import status
|
|
4
|
+
from rest_framework.response import Response
|
|
5
|
+
from rest_framework.views import exception_handler as drf_exception_handler
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def exception_handler(exc, context):
|
|
9
|
+
# 调用默认的异常处理获取 response 对象
|
|
10
|
+
response = drf_exception_handler(exc, context)
|
|
11
|
+
|
|
12
|
+
if response is not None:
|
|
13
|
+
# --- 核心修复点:判断数据类型 ---
|
|
14
|
+
if isinstance(response.data, list):
|
|
15
|
+
# 如果是列表,通常取第一个元素作为错误信息
|
|
16
|
+
error_msg = str(response.data[0])
|
|
17
|
+
elif isinstance(response.data, dict):
|
|
18
|
+
# 如果是字典,尝试获取 'detail' 字段
|
|
19
|
+
error_msg = response.data.get('detail', str(response.data))
|
|
20
|
+
else:
|
|
21
|
+
# 兜底,直接转字符串
|
|
22
|
+
error_msg = str(response.data)
|
|
23
|
+
|
|
24
|
+
# 统一包装格式
|
|
25
|
+
response.data = {
|
|
26
|
+
"code": -1,
|
|
27
|
+
"msg": error_msg
|
|
28
|
+
}
|
|
29
|
+
return response
|
|
30
|
+
|
|
31
|
+
# 处理未被捕获的服务器内部错误
|
|
32
|
+
return Response(
|
|
33
|
+
{
|
|
34
|
+
"code": -1,
|
|
35
|
+
"msg": "服务器内部错误"
|
|
36
|
+
},
|
|
37
|
+
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
38
|
+
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from rest_framework.pagination import PageNumberPagination as DrfPageNumberPagination
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class PageNumberPagination(DrfPageNumberPagination):
|
|
5
|
+
"""
|
|
6
|
+
定义分页器
|
|
7
|
+
"""
|
|
8
|
+
page_size = 10 # 默认一页显示10个
|
|
9
|
+
max_page_size = 10000 # 设置单次取的最大值
|
|
10
|
+
page_query_param = 'page' # 获取URL参数中传入的页码key字段
|
|
11
|
+
page_size_query_param = 'size' # 指定单页最大值的字段
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MyMaxPageNumberPagination(PageNumberPagination):
|
|
15
|
+
"""
|
|
16
|
+
定义查询所有,数据不会最大
|
|
17
|
+
"""
|
|
18
|
+
page_size = 10000 # 默认一页显示10000个
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# utils/response.py
|
|
2
|
+
from rest_framework.renderers import JSONRenderer as DrfJSONRenderer
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class JSONRenderer(DrfJSONRenderer):
|
|
6
|
+
"""
|
|
7
|
+
自定义全局渲染器,统一包装成功响应的格式
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def render(self, data, accepted_media_type=None, renderer_context=None):
|
|
11
|
+
# 获取当前的 HTTP 状态码
|
|
12
|
+
status_code = renderer_context['response'].status_code
|
|
13
|
+
|
|
14
|
+
# 判断是否为成功的响应(以 2 开头的状态码)
|
|
15
|
+
if str(status_code).startswith('2'):
|
|
16
|
+
# 将原始数据自动包装为期望的统一格式
|
|
17
|
+
response_data = {
|
|
18
|
+
'code': status_code,
|
|
19
|
+
'msg': 'success',
|
|
20
|
+
'data': data
|
|
21
|
+
}
|
|
22
|
+
# 调用父类方法,将包装后的字典重新渲染为 JSON 字符串
|
|
23
|
+
return super().render(response_data, accepted_media_type, renderer_context)
|
|
24
|
+
|
|
25
|
+
# 如果是非 2xx 状态码(即异常情况),直接返回原数据
|
|
26
|
+
# (因为异常情况已经由全局异常处理器格式化好了,这里无需再次包装)
|
|
27
|
+
renderer_context['response'].status_code = 200
|
|
28
|
+
return super().render(data, accepted_media_type, renderer_context)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class IBSRenderer(JSONRenderer):
|
|
32
|
+
def render(self, data, accepted_media_type=None, renderer_context=None):
|
|
33
|
+
if renderer_context:
|
|
34
|
+
response = renderer_context['response']
|
|
35
|
+
status_code = response.status_code
|
|
36
|
+
if status_code != 200:
|
|
37
|
+
if status_code == 401:
|
|
38
|
+
data = {'error_code': 2, 'error_msg': data.get('detail')}
|
|
39
|
+
else:
|
|
40
|
+
data = {'error_code': 9, 'error_msg': data.get('detail')}
|
|
41
|
+
response.status_code = 200
|
|
42
|
+
return super().render(data, accepted_media_type, renderer_context)
|
|
43
|
+
else:
|
|
44
|
+
return super().render(data, accepted_media_type, renderer_context)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from rest_framework.views import APIView
|
|
2
|
+
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
|
|
3
|
+
|
|
4
|
+
from .render import JSONRenderer, IBSRenderer
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MyAPIView(APIView):
|
|
8
|
+
'''
|
|
9
|
+
Api试图数据返回
|
|
10
|
+
'''
|
|
11
|
+
renderer_classes = [JSONRenderer]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MyModelViewSet(MyAPIView, ModelViewSet):
|
|
15
|
+
'''
|
|
16
|
+
新版本自定义返回规范
|
|
17
|
+
'''
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MyReadOnlyModelViewSet(MyAPIView, ReadOnlyModelViewSet):
|
|
21
|
+
'''
|
|
22
|
+
只读模型数据返回
|
|
23
|
+
'''
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class IbsAPIView(APIView):
|
|
27
|
+
'''
|
|
28
|
+
Api试图数据返回
|
|
29
|
+
'''
|
|
30
|
+
renderer_classes = [IBSRenderer]
|