fix: correct OSS route registration and get_url resolution - #6523
Open
ZH007-s wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
web.py 中同一个 URLConf 被分别挂载到管理端和聊天端:
path(admin_api_prefix, include("oss.urls"))
path(chat_api_prefix, include("oss.urls"))
但 oss.urls 的 app_name 都是 oss。不指定实例命名空间时,Django 会出现重复 URL namespace,之后通过 reverse() 或解析路由时可能指向不确定的 oss 路由。
改成:
include("oss.urls", namespace="admin_oss")
include("oss.urls", namespace="chat_oss")
让管理端和聊天端各自独立。同理,文件检索路由使用 admin_oss_retrieval 和 chat_oss_retrieval。
retrieval_urls.py 里的实际问题在这一条:
re_path(rf'^/oss/get_url/(?P[\w-]+)?$', ...)
有三个缺陷:
子路由被 path('admin/', include(...)) 挂载后,传入的剩余路径是 oss/get_url/...,没有开头 /;原正则要求 /oss,因此通常匹配不到。
捕获参数叫 url,但 GetUrlView 接收的是 application_id,即使匹配也会收到错误的关键字参数。
? 让 ID 可选,不符合获取应用 URL 必须提供 application_id 的接口语义。
修复后:
re_path(r'^oss/get_url/(?P<application_id>[\w-]+)/?$', ...)
现在以下路径都会正确进入 GetUrlView,并把 ID 作为 application_id 传入:
/admin/oss/get_url/<application_id>
/chat/oss/get_url/<application_id>
两处 rf'...' 改为 r'...' 只是清理:正则中没有插值变量,行为没有变化。