bk-resource 0.4.12__py3-none-any.whl → 0.4.13b0__py3-none-any.whl
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.
- bk_resource/utils/request.py +32 -0
- bk_resource/viewsets.py +13 -0
- {bk_resource-0.4.12.dist-info → bk_resource-0.4.13b0.dist-info}/METADATA +2 -2
- {bk_resource-0.4.12.dist-info → bk_resource-0.4.13b0.dist-info}/RECORD +7 -7
- {bk_resource-0.4.12.dist-info → bk_resource-0.4.13b0.dist-info}/LICENSE.txt +0 -0
- {bk_resource-0.4.12.dist-info → bk_resource-0.4.13b0.dist-info}/WHEEL +0 -0
- {bk_resource-0.4.12.dist-info → bk_resource-0.4.13b0.dist-info}/top_level.txt +0 -0
bk_resource/utils/request.py
CHANGED
@@ -15,8 +15,11 @@ specific language governing permissions and limitations under the License.
|
|
15
15
|
We undertake not to change the open source license (MIT license) applicable
|
16
16
|
to the current version of the project delivered to anyone in the future.
|
17
17
|
"""
|
18
|
+
from typing import Optional, Type
|
18
19
|
|
20
|
+
from django.http import HttpRequest
|
19
21
|
from django.test import RequestFactory
|
22
|
+
from django.urls import Resolver404, resolve
|
20
23
|
|
21
24
|
from bk_resource.utils.local import local
|
22
25
|
|
@@ -44,3 +47,32 @@ def set_local_username(username):
|
|
44
47
|
|
45
48
|
def get_mock_request(**kwargs):
|
46
49
|
return RequestFactory().request(**kwargs)
|
50
|
+
|
51
|
+
|
52
|
+
def get_resource_by_request(request: HttpRequest = None) -> Optional[Type["bk_resource.Resource"]]:
|
53
|
+
"""根据 request 对象获取具体的 Resource 类"""
|
54
|
+
|
55
|
+
from blueapps.utils.request_provider import get_local_request
|
56
|
+
|
57
|
+
request = request or get_local_request()
|
58
|
+
if not request:
|
59
|
+
return None
|
60
|
+
|
61
|
+
try:
|
62
|
+
match = resolve(request.path_info)
|
63
|
+
except Resolver404:
|
64
|
+
try:
|
65
|
+
match = resolve(f"{request.path_info}/")
|
66
|
+
except Resolver404:
|
67
|
+
return None
|
68
|
+
|
69
|
+
try:
|
70
|
+
from bk_resource.viewsets import RESOURCE_MAPPING
|
71
|
+
|
72
|
+
view_set_path = f"{match.func.cls.__module__}.{match.func.cls.__name__}"
|
73
|
+
resource_clz = RESOURCE_MAPPING.get(
|
74
|
+
(request.method, f"{view_set_path}.{match.func.actions.get(request.method.lower())}")
|
75
|
+
)
|
76
|
+
return resource_clz
|
77
|
+
except Exception: # pylint: disable=broad-except
|
78
|
+
return None
|
bk_resource/viewsets.py
CHANGED
@@ -35,6 +35,9 @@ from bk_resource.base import Resource
|
|
35
35
|
from bk_resource.settings import bk_resource_settings
|
36
36
|
|
37
37
|
|
38
|
+
RESOURCE_MAPPING = {}
|
39
|
+
|
40
|
+
|
38
41
|
class ResourceRoute(object):
|
39
42
|
"""
|
40
43
|
Resource的视图配置,应用于viewsets
|
@@ -124,6 +127,7 @@ class ResourceViewSet(viewsets.GenericViewSet):
|
|
124
127
|
|
125
128
|
@classmethod
|
126
129
|
def generate_endpoint(cls):
|
130
|
+
|
127
131
|
for resource_route in cls.resource_routes:
|
128
132
|
# 生成方法模版
|
129
133
|
function = cls._generate_function_template(resource_route)
|
@@ -202,6 +206,9 @@ class ResourceViewSet(viewsets.GenericViewSet):
|
|
202
206
|
setattr(cls, cls.EMPTY_ENDPOINT_METHODS[resource_route.method], function)
|
203
207
|
else:
|
204
208
|
raise AssertionError(gettext("不支持的请求方法: %s,请确认resource_routes配置是否正确!") % resource_route.method)
|
209
|
+
|
210
|
+
resource_action = cls.EMPTY_ENDPOINT_METHODS[resource_route.method]
|
211
|
+
|
205
212
|
else:
|
206
213
|
function = method_decorator(cache_control(max_age=0, private=True))(function)
|
207
214
|
function.__name__ = resource_route.endpoint
|
@@ -217,6 +224,12 @@ class ResourceViewSet(viewsets.GenericViewSet):
|
|
217
224
|
function = decorator_function(function)
|
218
225
|
setattr(cls, resource_route.endpoint, function)
|
219
226
|
|
227
|
+
resource_action = resource_route.endpoint
|
228
|
+
|
229
|
+
RESOURCE_MAPPING[
|
230
|
+
(resource_route.method, f"{cls.__module__}.{cls.__name__}.{resource_action}")
|
231
|
+
] = resource_route.resource_class
|
232
|
+
|
220
233
|
@classmethod
|
221
234
|
def _generate_function_template(cls, resource_route: ResourceRoute):
|
222
235
|
"""
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: bk_resource
|
3
|
-
Version: 0.4.
|
3
|
+
Version: 0.4.13b0
|
4
4
|
Summary: Bk Resource
|
5
5
|
Home-page: https://bk.tencent.com
|
6
6
|
Author: blueking
|
@@ -19,7 +19,7 @@ Requires-Dist: celery >=4.4.0
|
|
19
19
|

|
20
20
|
|
21
21
|
[](https://github.com/TencentBlueKing/bk-resource/blob/main/LICENSE.txt)
|
22
|
-
[](https://github.com/TencentBlueKing/bk-resource/releases)
|
23
23
|
[](https://github.com/TencentBlueKing/bk-resource/pulls)
|
24
24
|
[](https://codecov.io/gh/TencentBlueKing/bk-resource)
|
25
25
|
[](https://github.com/TencentBlueKing/bk-resource/actions/workflows/unittest.yml)
|
@@ -7,7 +7,7 @@ bk_resource/serializers.py,sha256=Az_2Qsp_5gUrhU_0iZS45komm5upU5u1UpNE-ArSX4s,26
|
|
7
7
|
bk_resource/settings.py,sha256=QCGKeetjdDIWHHu1_93MKPeJ_VpGlQXvW7gpln1pGlU,3827
|
8
8
|
bk_resource/tasks.py,sha256=OeBy41GfSDNL_0adGeKBG6LlVRqNUfP7KzXe7pxpr3I,4090
|
9
9
|
bk_resource/tools.py,sha256=fPmiDNxh2FWo5Yo7CXtvYyHxyG3_JrGs5wYZMaVFAhE,8166
|
10
|
-
bk_resource/viewsets.py,sha256=
|
10
|
+
bk_resource/viewsets.py,sha256=KWx2OaPGTZVFCTJTN4qCOM5SGz6UZ0hVOdWC_-6Vsbg,11955
|
11
11
|
bk_resource/conf/__init__.py,sha256=39m-gTk6pr0j_6xwdfTFEFuj_ER_MDoEll2zkEdCPBo,891
|
12
12
|
bk_resource/conf/app_template/__init__.py-tpl,sha256=iwhKnzeBJLKxpRVjvzwiRE63_zNpIBfaKLITauVph-0,24
|
13
13
|
bk_resource/conf/app_template/admin.py-tpl,sha256=n0NQrWDbx74lHja8g2s5vTKjR954SCtizSehZCQLBK0,60
|
@@ -43,13 +43,13 @@ bk_resource/utils/generators.py,sha256=qhs0msHJdmVm8z6phfkC-GjibhxJckq6xgM4IM8w5
|
|
43
43
|
bk_resource/utils/inspectors.py,sha256=xxuB2iHInCGjl9A4QRz9gCw-iD5GFkXlhp_nCsnnJPw,1176
|
44
44
|
bk_resource/utils/local.py,sha256=qPxDc5lYA_7ffmpUfGqYVsUG-WxBQ0jGCvc6Ti0WoCw,3337
|
45
45
|
bk_resource/utils/logger.py,sha256=VPp5nGsKawMtH5pKctLmdDpZpfYtjCr3DUYHhEsuXBU,950
|
46
|
-
bk_resource/utils/request.py,sha256=
|
46
|
+
bk_resource/utils/request.py,sha256=Ipah61TzZF1ArjW5nj6ThaG1hsl2Swj7kwvbJBGtsF4,2623
|
47
47
|
bk_resource/utils/request_log.py,sha256=VMSFu0c8e53e7TUFJtdKXpkM0_pl-mk1UaFFdrGYzPo,3317
|
48
48
|
bk_resource/utils/text.py,sha256=exu7gEFANcU6apQOut_1VSzoddOvcKR3CXEF1Z19M4Y,3313
|
49
49
|
bk_resource/utils/thread_backend.py,sha256=AdMWzXreFARinPdDaMiRJwwxSWL1EB4MUSz1Q7kYz90,5017
|
50
50
|
bk_resource/utils/time_tools.py,sha256=y-eP-6Gu5ur3ET0bOp2ML8Z0HtrguvTPlbsSA1dNhWU,10967
|
51
|
-
bk_resource-0.4.
|
52
|
-
bk_resource-0.4.
|
53
|
-
bk_resource-0.4.
|
54
|
-
bk_resource-0.4.
|
55
|
-
bk_resource-0.4.
|
51
|
+
bk_resource-0.4.13b0.dist-info/LICENSE.txt,sha256=dnlkOMrlbRgRXlBt-a_QF6cs1MFG6WYmcotYbI6_gpo,859
|
52
|
+
bk_resource-0.4.13b0.dist-info/METADATA,sha256=mKLna1KHSTKdn_-3LmosYThky7alFkVurgGvF7QEgZY,5505
|
53
|
+
bk_resource-0.4.13b0.dist-info/WHEEL,sha256=y4mX-SOX4fYIkonsAGA5N0Oy-8_gI4FXw5HNI1xqvWg,91
|
54
|
+
bk_resource-0.4.13b0.dist-info/top_level.txt,sha256=UJxO1tbrqlmnvSTYCLoqg39_gvBesvjp3CcGEPjzXY4,12
|
55
|
+
bk_resource-0.4.13b0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|