Skip to content

Commit

Permalink
refactor: 剔除django-rest-framework-condition,django-cors-headers升级
Browse files Browse the repository at this point in the history
  • Loading branch information
unique0lai committed Jan 10, 2025
1 parent bc8c67a commit 9a4aa66
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bkmonitor/config/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@
INSTALLED_APPS += ("corsheaders",)
# 该跨域中间件需要放在前面
MIDDLEWARE = ("corsheaders.middleware.CorsMiddleware",) + MIDDLEWARE
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
2 changes: 1 addition & 1 deletion bkmonitor/config/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
INSTALLED_APPS += ("corsheaders",)
# 该跨域中间件需要放在前面
MIDDLEWARE = ("corsheaders.middleware.CorsMiddleware",) + MIDDLEWARE
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
2 changes: 1 addition & 1 deletion bkmonitor/config/stag.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
INSTALLED_APPS += ("corsheaders",)
# 该跨域中间件需要放在前面
MIDDLEWARE = ("corsheaders.middleware.CorsMiddleware",) + MIDDLEWARE
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
8 changes: 0 additions & 8 deletions bkmonitor/core/drf_resource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,3 @@ def generate_doc(cls):
"request_params": render_schema(request_params),
"response_params": render_schema(response_params, using_source=True),
}

@classmethod
def etag(cls, request, *args, **kwargs):
return

@classmethod
def last_modified(cls, request, *args, **kwargs):
return
14 changes: 7 additions & 7 deletions bkmonitor/core/drf_resource/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
specific language governing permissions and limitations under the License.
"""


import six
from django.core.exceptions import ImproperlyConfigured
from rest_framework.routers import DefaultRouter
from rest_framework.viewsets import GenericViewSet

Expand All @@ -33,16 +32,17 @@ def register(self, prefix, viewset, basename=None):
注册单个ResourceViewset
"""
self._init_resource_viewset(viewset)
# 如果已经注册过了viewset,则不进行注册
if (prefix, viewset, basename or self.get_default_basename(viewset)) in self.registry:
return
super(ResourceRouter, self).register(prefix, viewset, basename)

try:
super(ResourceRouter, self).register(prefix, viewset, basename)
except ImproperlyConfigured:
pass

def register_module(self, viewset_module):
"""
注册整个ResourceViewset模块,并根据类的命名规则自动生成对应的url
"""
for attr, viewset in six.iteritems(viewset_module.__dict__):
for attr, viewset in viewset_module.__dict__.items():
# 全小写的属性不是类,忽略
if attr.startswith("_") or attr[0].islower():
continue
Expand Down
5 changes: 0 additions & 5 deletions bkmonitor/core/drf_resource/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.serializers import Serializer
from rest_framework_condition import condition

from core.drf_resource.base import Resource

Expand Down Expand Up @@ -181,10 +180,6 @@ def generate_endpoint(cls):
url_path=resource_route.endpoint,
url_name=resource_route.endpoint.replace("_", "-"),
)(function)
function = condition(
etag_func=resource_route.resource_class.etag,
last_modified_func=resource_route.resource_class.last_modified,
)(function)

function = decorator_function(function)
setattr(cls, function.__name__, function)
Expand Down
3 changes: 1 addition & 2 deletions bkmonitor/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Django==3.2.25
whitenoise==5.0
django-mysql==4.3.0
django-db-connection-pool==1.2.5
django-cors-headers==3.2.1
django-cors-headers==4.1.0
django-versionlog==1.6.0

# prometheus & django
Expand All @@ -25,7 +25,6 @@ django-jinja==2.7.1

# django rest framework
djangorestframework==3.12.4
django-rest-framework-condition==0.1.1
django-filter==2.4.0
drf-yasg==1.21.5

Expand Down

0 comments on commit 9a4aa66

Please sign in to comment.