add challenge author

This commit is contained in:
2026-05-06 06:28:39 -06:00
parent 57df250b55
commit 455d684bd4
4 changed files with 57 additions and 4 deletions

View File

@@ -15,7 +15,7 @@ def challenge(request):
"""
后台显示所有的列表
"""
return Challenge.objects.all()
return Challenge.objects.select_related("author").all()
@router.get("/display", response=List[ChallengeDisplay])
@@ -23,7 +23,11 @@ def get_all_public_display(request):
"""
前台显示所有公开的挑战
"""
challenges = list(Challenge.objects.filter(is_public=True).order_by("-display"))
challenges = list(
Challenge.objects.select_related("author")
.filter(is_public=True)
.order_by("-display")
)
if request.user.is_authenticated:
task_ids = [c.task_ptr_id for c in challenges]
submitted_ids = set(
@@ -41,6 +45,7 @@ def get_all_public_display(request):
score=c.score,
pass_score=c.pass_score,
is_public=c.is_public,
author_name=c.author_name,
submitted=c.task_ptr_id in submitted_ids,
)
for c in challenges
@@ -49,7 +54,9 @@ def get_all_public_display(request):
@router.get("/{display}", response=ChallengeAll)
def get(request, display: int):
return get_object_or_404(Challenge, display=display)
return get_object_or_404(
Challenge.objects.select_related("author"), display=display
)
@router.post("/")
@@ -61,10 +68,12 @@ def create_or_update(request, payload: ChallengeIn):
item.content = payload.content
item.score = payload.score
item.is_public = payload.is_public
if item.author_id is None and request.user.is_authenticated:
item.author = request.user
item.save()
return {"message": "更新成功"}
except Challenge.DoesNotExist:
Challenge.objects.create(**payload.dict())
Challenge.objects.create(**payload.dict(), author=request.user)
return {"message": "创建成功"}