bk-resource 0.4.12b0__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.
@@ -26,13 +26,10 @@ from rest_framework.generics import GenericAPIView
26
26
  from rest_framework.viewsets import ModelViewSet
27
27
 
28
28
  from bk_resource import Resource
29
+ from bk_resource.base import Empty
29
30
  from bk_resource.utils.request import get_mock_request
30
31
 
31
32
 
32
- class Empty:
33
- ...
34
-
35
-
36
33
  class ViewMixin(GenericAPIView):
37
34
  filter_fields = []
38
35
  search_fields = []
@@ -25,7 +25,7 @@ from django.core.cache import cache, caches
25
25
  from django.utils.encoding import force_bytes
26
26
  from django.utils.translation import gettext
27
27
 
28
- from bk_resource.contrib.model import Empty
28
+ from bk_resource.base import Empty
29
29
  from bk_resource.settings import bk_resource_settings
30
30
  from bk_resource.utils.common_utils import count_md5
31
31
  from bk_resource.utils.local import local
@@ -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.12b0
3
+ Version: 0.4.13b0
4
4
  Summary: Bk Resource
5
5
  Home-page: https://bk.tencent.com
6
6
  Author: blueking
@@ -14,12 +14,12 @@ Requires-Dist: drf-yasg >=1.20.0
14
14
  Requires-Dist: pyinstrument >=3.4.2
15
15
  Requires-Dist: arrow >=1.2.0
16
16
  Requires-Dist: django-rest-framework-condition >=0.1.1
17
- Requires-Dist: celery >=5
17
+ Requires-Dist: celery >=4.4.0
18
18
 
19
19
  ![logo.png](assests/logo.png)
20
20
 
21
21
  [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/TencentBlueKing/bk-resource/blob/main/LICENSE.txt)
22
- [![Release Version](https://img.shields.io/badge/release-0.4.12b0-brightgreen.svg)](https://github.com/TencentBlueKing/bk-resource/releases)
22
+ [![Release Version](https://img.shields.io/badge/release-0.4.13b0-brightgreen.svg)](https://github.com/TencentBlueKing/bk-resource/releases)
23
23
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/bk-resource/pulls)
24
24
  [![codecov](https://codecov.io/gh/TencentBlueKing/bk-resource/branch/main/graph/badge.svg)](https://codecov.io/gh/TencentBlueKing/bk-resource)
25
25
  [![Unittest Py3](https://github.com/TencentBlueKing/bk-resource/actions/workflows/unittest.yml/badge.svg)](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=QhCIHiJ227N8njqr6bSJZxP5eBh3z5Q4u1dGzBjHHXs,11614
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
@@ -22,7 +22,7 @@ bk_resource/contrib/__init__.py,sha256=bP18WkBTSVynqrmGO6UDjhTdOCO0w18myXynyHMg6
22
22
  bk_resource/contrib/api.py,sha256=DFcjJsuwU9pKwvQE-nZhN2wApqkaC0feBIh4Es5mUxc,8658
23
23
  bk_resource/contrib/bk_api.py,sha256=HJW5GWM0tT-oR6AnYHjcU9JaymyKPrrx_0aCnZD1BaM,6304
24
24
  bk_resource/contrib/cache.py,sha256=Gp91ajqDUelXKDu2WTC_QXoTJiO--iWEMsNjl_hxAlg,2969
25
- bk_resource/contrib/model.py,sha256=ashkD_NzIR4WSEEQHPWPCp66wvKWwiaL1AHfQgWfyAY,5941
25
+ bk_resource/contrib/model.py,sha256=-zQOjnXllDYDMo6nxcvD8WrfzQuThfzcqf97gnRhwaE,5953
26
26
  bk_resource/locale/en/LC_MESSAGES/django.mo,sha256=2Pf2WZXzYQeGcnnNWo78xZ58vuOr7Tj5sc9v675SwSk,2943
27
27
  bk_resource/locale/en/LC_MESSAGES/django.po,sha256=VIssUCnLPT1BUKjTB93yR7uyc0IvHdgGS93wpV18cCg,4322
28
28
  bk_resource/locale/zh_CN/LC_MESSAGES/django.mo,sha256=3GCXKryGcXqFnoIDIDaz97SBpl5C99nbSzjSkM6bBkg,3022
@@ -36,20 +36,20 @@ bk_resource/management/commands/__init__.py,sha256=zTvK51uqIT4Sy_bCfeXQbIyI-Sskb
36
36
  bk_resource/management/commands/generate_resource_stub_file.py,sha256=M_S1eLxx-1eeF3VwbzJJl0DE2ABB3Z-i75Kb23nyi38,1110
37
37
  bk_resource/management/commands/start_resource.py,sha256=BHohJIXIsT2Ur0GbK1FctrEdY2RbJtIGs2myyRsOb8M,1243
38
38
  bk_resource/utils/__init__.py,sha256=zTvK51uqIT4Sy_bCfeXQbIyI-SskbMWjUfVNrQzE-0k,891
39
- bk_resource/utils/cache.py,sha256=g_d8iD9AWX_kOe_zVSaUVSJG9hgQclp_-gjtqDc5hME,10609
39
+ bk_resource/utils/cache.py,sha256=QNaXdEaAJDPhMvJmNdMB3vVA34bs4cPdkQJ5SZMA0Xk,10600
40
40
  bk_resource/utils/common_utils.py,sha256=uJ2O_64At_LhPexC1ZifMFNauG9oh73v9EdnUB3prUo,13205
41
41
  bk_resource/utils/factory.py,sha256=FtGnGqhi_ubUOxveEkfgrlpYFE2-m-oPFmSV9347UD8,5104
42
42
  bk_resource/utils/generators.py,sha256=qhs0msHJdmVm8z6phfkC-GjibhxJckq6xgM4IM8w5bU,2080
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=5Lfv_LCvE0I3yp3-jxGve8PU8TF4rR0vGTNBYGxzvIA,1593
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.12b0.dist-info/LICENSE.txt,sha256=dnlkOMrlbRgRXlBt-a_QF6cs1MFG6WYmcotYbI6_gpo,859
52
- bk_resource-0.4.12b0.dist-info/METADATA,sha256=w11I_AfW-LozH5LyRbyaVoDZnLNQS3pmVSB_ndtX4Fo,5501
53
- bk_resource-0.4.12b0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
54
- bk_resource-0.4.12b0.dist-info/top_level.txt,sha256=UJxO1tbrqlmnvSTYCLoqg39_gvBesvjp3CcGEPjzXY4,12
55
- bk_resource-0.4.12b0.dist-info/RECORD,,
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,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: setuptools (70.2.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5