Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
LLM-Filter
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2026_NGIT
LLM-Filter
Commits
0d0589b6
Commit
0d0589b6
authored
Dec 13, 2025
by
uuo00_n
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(students): 修复默认用户关联学生实体缺失导致/students/me 404的问题
确保默认用户有对应的学生实体,避免接口返回404错误 同时优化/students/me接口的错误处理逻辑
parent
7133e3a8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
6 deletions
+31
-6
students.py
app/api/v1/students.py
+12
-6
init_db.py
init_db.py
+19
-0
No files found.
app/api/v1/students.py
View file @
0d0589b6
...
...
@@ -2,7 +2,8 @@ from fastapi import APIRouter, Depends, HTTPException, Path, Body
from
pydantic
import
BaseModel
from
typing
import
Optional
,
Dict
,
Any
from
app.api.deps
import
require_edition_for_mode
,
require_role
from
app.services.student_binding
import
bind_user_to_student
,
unbind_user_from_student
,
get_student_by_user
from
app.services.student_binding
import
bind_user_to_student
,
unbind_user_from_student
from
app.services.dashboard
import
_get_student_by_user
router
=
APIRouter
(
dependencies
=
[
Depends
(
require_edition_for_mode
())])
...
...
@@ -61,8 +62,13 @@ async def unbind(student_id: str = Path(..., description="学生ID"), current_us
},
)
async
def
me
(
current_user
:
dict
=
Depends
(
require_role
(
1
)))
->
StudentOut
:
s
=
await
get_student_by_user
(
str
(
current_user
[
"_id"
]))
try
:
s
=
await
_get_student_by_user
(
current_user
[
"_id"
])
if
not
s
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"当前用户未绑定学生"
)
s
[
"_id"
]
=
str
(
s
[
"_id"
])
# 简化返回
s
[
"_id"
]
=
str
(
s
[
"_id"
])
return
s
except
HTTPException
as
e
:
if
e
.
status_code
==
404
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"当前用户未绑定学生"
)
raise
e
init_db.py
View file @
0d0589b6
...
...
@@ -594,6 +594,25 @@ async def seed_identity_data(db, mode: str):
if
user_doc
:
p
=
find_person
(
"P-STU-USER"
)
bindings
.
append
({
"account_id"
:
user_doc
[
"_id"
],
"person_id"
:
p
[
"_id"
],
"type"
:
"student"
,
"primary"
:
True
})
# 修复:为默认用户创建关联的学生实体,防止 /students/me 404
# 注意:P-STU-USER 是人物,这里需要一个对应的 student 实体指向它
existing_stu
=
await
db
.
students
.
find_one
({
"person_id"
:
p
[
"_id"
]})
if
not
existing_stu
:
await
db
.
students
.
insert_one
({
"student_id"
:
"STU-USER-001"
,
"name"
:
"默认学生USER"
,
"gender"
:
"男"
,
"grade"
:
"22级"
,
"major"
:
"软件技术"
,
"class_id"
:
"SW22-1"
,
# 默认分到 1 班
"status"
:
"在读"
,
"person_id"
:
p
[
"_id"
],
"created_at"
:
datetime
.
now
(),
"updated_at"
:
datetime
.
now
(),
})
print
(
"已为默认用户 user 创建关联学生实体"
)
if
manager_doc
:
p
=
find_person
(
"P-TEA-001"
)
bindings
.
append
({
"account_id"
:
manager_doc
[
"_id"
],
"person_id"
:
p
[
"_id"
],
"type"
:
"teacher"
,
"primary"
:
True
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment