refactor: 统一用户名的 ks 班级前缀处理
同一个剥前缀函数在 submission 和 flowchart 的管理端各有一份逐字相同的
拷贝,conf 里还有一份内联写法,合并到 utils/shortcuts.strip_class_prefix。
改名是因为原名 get_real_name 和 UserProfile.real_name 字段、以及三个
serializer 里的同名方法都容易混。
行为上修了两处:
- 剥前缀改用 removeprefix,不再按长度硬切。班级号对不上时原样返回,
旧写法会从中间截出乱码(ks999王五 配 class_name=251 会切成「王五」)
- get_class_name 的正则从 \d+ 收紧到 \d{3,4},与前端
ButtonWithSearch 的 /^ks\d{3,4}/ 对齐。旧的贪婪匹配在姓名部分是纯
数字时会吃掉整串(ks251001 返回 251001 而不是 251)。顺带 re.search
换成 re.match,外层的 startswith 判断并进正则
正常数据(ks251张三、ks2510李四)新旧结果一致,已逐例对拍。
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ from account.decorators import teacher_admin_required
|
||||
from account.models import AdminType, User
|
||||
from problem.models import Problem
|
||||
from utils.api import APIView
|
||||
from utils.shortcuts import strip_class_prefix
|
||||
|
||||
from ..models import FlowchartSubmission, FlowchartSubmissionStatus
|
||||
|
||||
@@ -64,12 +65,6 @@ for _w in CUSTOM_WORDS:
|
||||
jieba.add_word(_w, freq=9999)
|
||||
|
||||
|
||||
def get_real_name(username, class_name):
|
||||
if class_name and username.startswith("ks"):
|
||||
return username[len(f"ks{class_name}") :]
|
||||
return username
|
||||
|
||||
|
||||
class FlowchartStatisticsAPI(APIView):
|
||||
@teacher_admin_required
|
||||
def get(self, request):
|
||||
@@ -169,7 +164,7 @@ class FlowchartStatisticsAPI(APIView):
|
||||
if all_users_dict:
|
||||
for uname in set(all_users_dict.keys()) - submitted_users:
|
||||
class_name = all_users_dict[uname]
|
||||
real_name = get_real_name(uname, class_name)
|
||||
real_name = strip_class_prefix(uname, class_name)
|
||||
unaccepted.append({"username": uname, "real_name": real_name})
|
||||
|
||||
# 5. Word cloud from feedback + suggestions + criteria comments
|
||||
|
||||
Reference in New Issue
Block a user