From 6a573138e1ee10e95b09c29e267c4b233c01381c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E8=8F=9C?= Date: Thu, 15 Feb 2024 16:04:18 +0800 Subject: [PATCH] chore(FasterRunner): upgrade django version from 2.2.28 to 4.1, replace mysqlclient by pymysql (#171) * chore(FasterRunner): upgrade django version from 2.2.28 to 4.1, replace mysqlclient by pymysql * fix(requirements): add drf-yasg deps * fix(requirements): add drf-yasg deps * fix(requirements): add pymysql * fix: collect static cannot import name 'ugettext_lazy' --- FasterRunner/__init__.py | 6 ++++++ FasterRunner/auth.py | 2 +- FasterRunner/settings/__init__.py | 11 ++++++++++- FasterRunner/settings/base.py | 1 - FasterRunner/urls.py | 15 +++++++-------- fastrunner/utils/middleware.py | 4 ++-- fastuser/admin.py | 4 +++- fastuser/urls.py | 4 ++-- requirements.txt | 15 ++++++++------- 9 files changed, 39 insertions(+), 23 deletions(-) diff --git a/FasterRunner/__init__.py b/FasterRunner/__init__.py index e69de29b..daae0a1f 100644 --- a/FasterRunner/__init__.py +++ b/FasterRunner/__init__.py @@ -0,0 +1,6 @@ +import pymysql + +# 替换mysqlclient +pymysql.version_info = (1, 4, 6, "final", 0) # 修改版本号为兼容版本 +pymysql.install_as_MySQLdb() + diff --git a/FasterRunner/auth.py b/FasterRunner/auth.py index b8d7215b..6f3bbb84 100644 --- a/FasterRunner/auth.py +++ b/FasterRunner/auth.py @@ -100,7 +100,7 @@ def authenticate(self, request): supplied using JWT-based authentication. Otherwise returns `None`. """ # jwt_value = request.query_params.get("token", None) - jwt_value = request.META.get('HTTP_AUTHORIZATION', None) + jwt_value = request.headers.get('authorization', None) try: payload = jwt_decode_handler(jwt_value) except jwt.ExpiredSignature: diff --git a/FasterRunner/settings/__init__.py b/FasterRunner/settings/__init__.py index 34707112..21398729 100644 --- a/FasterRunner/settings/__init__.py +++ b/FasterRunner/settings/__init__.py @@ -5,4 +5,13 @@ # @File: __init__.py.py # @Time : 2019/6/14 17:05 # @Email: lihuacai168@gmail.com -# @Software: PyCharm \ No newline at end of file +# @Software: PyCharm + +# 兼容已经移除的方法 +import django +from django.utils.encoding import smart_str +django.utils.encoding.smart_text = smart_str + +from django.utils.translation import gettext_lazy +django.utils.translation.ugettext = gettext_lazy +django.utils.translation.ugettext_lazy = gettext_lazy \ No newline at end of file diff --git a/FasterRunner/settings/base.py b/FasterRunner/settings/base.py index f811a7c2..5fefd246 100644 --- a/FasterRunner/settings/base.py +++ b/FasterRunner/settings/base.py @@ -129,7 +129,6 @@ USE_I18N = True -USE_L10N = True USE_TZ = False diff --git a/FasterRunner/urls.py b/FasterRunner/urls.py index 40610d45..44653d5d 100644 --- a/FasterRunner/urls.py +++ b/FasterRunner/urls.py @@ -14,7 +14,6 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.conf.urls import url from django.urls import path, include, re_path from rest_framework.routers import DefaultRouter @@ -47,9 +46,9 @@ urlpatterns = [ path(r"login", obtain_jwt_token), path('admin/', admin.site.urls), - url(r'^docs/', schema_view, name="docs"), - url(r'^accounts/', include('rest_framework.urls',)), - url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework_api_auth')), + # re_path(r'^docs/', schema_view, name="docs"), + path('accounts/', include('rest_framework.urls',)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework_api_auth')), path('api/user/', include('fastuser.urls')), path('api/fastrunner/', include('fastrunner.urls')), path('api/system/', include(system_router.urls)), @@ -57,11 +56,11 @@ # 执行定时任务 # TODO 需要增加触发检验,暂时关闭触发入口 # re_path(r'^run_all_auto_case/$', run_all_auto_case.run_all_auto_case, name='run_all_auto_case'), - re_path(r'^get_report_url/$', run_all_auto_case.get_report_url, name='get_report_url'), + path('get_report_url/', run_all_auto_case.get_report_url, name='get_report_url'), # swagger - url(r'^swagger(?P\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), - url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), - url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), + re_path(r'^swagger(?P\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), + path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), + path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), ] diff --git a/fastrunner/utils/middleware.py b/fastrunner/utils/middleware.py index 7279eab9..8b00fae0 100644 --- a/fastrunner/utils/middleware.py +++ b/fastrunner/utils/middleware.py @@ -31,9 +31,9 @@ def process_response(self, request, response): else: user = request.user - ip: str = request.META.get("HTTP_X_FORWARDED_FOR", request.META.get("REMOTE_ADDR")) + ip: str = request.headers.get("x-forwarded-for", request.META.get("REMOTE_ADDR")) # 前端请求头没传project,就默认为0 - project = request.META.get("HTTP_PROJECT", 0) + project = request.headers.get("project", 0) url: str = request.path # 去除测试报告页字体相关的访问 diff --git a/fastuser/admin.py b/fastuser/admin.py index c1d9b12f..d03f3cc7 100644 --- a/fastuser/admin.py +++ b/fastuser/admin.py @@ -30,7 +30,9 @@ class UserAdmin(BaseUserAdmin): ) filter_horizontal = ('groups',) + @admin.display( + description='所属分组' + ) def belong_groups(self, obj): return ", ".join([g.name for g in obj.groups.all()]) - belong_groups.short_description = '所属分组' diff --git a/fastuser/urls.py b/fastuser/urls.py index abfcf4f7..4f98b991 100644 --- a/fastuser/urls.py +++ b/fastuser/urls.py @@ -14,7 +14,7 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -from django.urls import path,re_path +from django.urls import path from fastuser import views from fastrunner.views import timer_task @@ -23,5 +23,5 @@ # path('register/', views.RegisterView.as_view()), path('login/', views.LoginView.as_view()), path('list/', views.UserView.as_view()), - re_path(r'^auto_run_testsuite_pk/$', timer_task.auto_run_testsuite_pk, name='auto_run_testsuite_pk'), + path('auto_run_testsuite_pk/', timer_task.auto_run_testsuite_pk, name='auto_run_testsuite_pk'), ] diff --git a/requirements.txt b/requirements.txt index 54a684de..9cc9f2e2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,17 +13,18 @@ contextvars==2.4 coreapi==2.3.3 coreschema==0.0.4 DingtalkChatbot==1.3.0 -Django==2.2.28 +Django==4.1 django-celery-beat -django-cors-headers==2.4.0 +django-cors-headers==4.3.1 django-jsonfield==1.4.0 django-model-utils==4.0.0 django-rest-swagger==2.2.0 -django-simpleui==2020.7 +django-simpleui==2023.12.12 django-utils-six==2.0 -djangorestframework~=3.8.2 +djangorestframework==3.14.0 djangorestframework-jwt==1.11.0 -drf-yasg==1.17.1 +drf-yasg==1.21.7 +packaging==23.2 har2case==0.3.1 # HttpRunner==1.5.15 idna==2.8 @@ -34,9 +35,8 @@ Jinja2==2.10.3 # kombu loguru==0.5.1 MarkupSafe==1.1.1 -mysqlclient==2.0.1 +#mysqlclient==2.0.1 openapi-codec==1.3.2 -packaging==20.4 PyJWT==1.7.1 pyparsing==2.4.7 python-dotenv==0.10.3 @@ -71,3 +71,4 @@ django-log-request-id~=2.0.0 gevent==22.10.2 django-filter~=2.4.0 django-auth-ldap==2.3.0 +pymysql==1.1.0