add leaderboard
This commit is contained in:
@@ -6,6 +6,7 @@ from ninja.pagination import paginate
|
|||||||
from ninja.errors import HttpError
|
from ninja.errors import HttpError
|
||||||
from .schemas import (
|
from .schemas import (
|
||||||
BatchUsersIn,
|
BatchUsersIn,
|
||||||
|
LeaderboardEntry,
|
||||||
UserListSchema,
|
UserListSchema,
|
||||||
UserRegistrationSchema,
|
UserRegistrationSchema,
|
||||||
UserLoginSchema,
|
UserLoginSchema,
|
||||||
@@ -119,3 +120,18 @@ def toggle_user_is_active(request, id: int):
|
|||||||
}
|
}
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
raise HttpError(404, "查无此人")
|
raise HttpError(404, "查无此人")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/leaderboard", response=List[LeaderboardEntry])
|
||||||
|
def leaderboard(request):
|
||||||
|
from .models import Profile
|
||||||
|
profiles = (
|
||||||
|
Profile.objects
|
||||||
|
.select_related("user")
|
||||||
|
.filter(total_score__gt=0)
|
||||||
|
.order_by("-total_score")
|
||||||
|
)
|
||||||
|
return [
|
||||||
|
LeaderboardEntry(rank=i + 1, username=p.user.username, total_score=p.total_score)
|
||||||
|
for i, p in enumerate(profiles)
|
||||||
|
]
|
||||||
|
|||||||
18
account/migrations/0002_alter_profile_total_score.py
Normal file
18
account/migrations/0002_alter_profile_total_score.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 6.0.1 on 2026-03-09 11:55
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('account', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='profile',
|
||||||
|
name='total_score',
|
||||||
|
field=models.FloatField(default=0.0),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -47,14 +47,23 @@ class User(AbstractUser):
|
|||||||
|
|
||||||
class Profile(models.Model):
|
class Profile(models.Model):
|
||||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||||
total_score = models.IntegerField(default=0)
|
total_score = models.FloatField(default=0.0)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.user.username
|
return self.user.username
|
||||||
|
|
||||||
def update_total_score(self, score: int):
|
def recalculate_total_score(self):
|
||||||
self.total_score = self.total_score + score
|
from django.db.models import Max, Sum
|
||||||
self.save()
|
from submission.models import Submission
|
||||||
|
total = (
|
||||||
|
Submission.objects
|
||||||
|
.filter(user=self.user, task__task_type="challenge", score__gt=0)
|
||||||
|
.values("task_id")
|
||||||
|
.annotate(best=Max("score"))
|
||||||
|
.aggregate(total=Sum("best"))["total"]
|
||||||
|
) or 0.0
|
||||||
|
self.total_score = total
|
||||||
|
self.save(update_fields=["total_score"])
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=User)
|
@receiver(post_save, sender=User)
|
||||||
|
|||||||
@@ -46,3 +46,9 @@ class UserLoginSchema(Schema):
|
|||||||
class BatchUsersIn(Schema):
|
class BatchUsersIn(Schema):
|
||||||
names: List[str]
|
names: List[str]
|
||||||
classname: str
|
classname: str
|
||||||
|
|
||||||
|
|
||||||
|
class LeaderboardEntry(Schema):
|
||||||
|
rank: int
|
||||||
|
username: str
|
||||||
|
total_score: float
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ from django.db.models import OuterRef, Subquery, IntegerField
|
|||||||
|
|
||||||
from .schemas import (
|
from .schemas import (
|
||||||
FlagIn,
|
FlagIn,
|
||||||
|
MyScoreOut,
|
||||||
SubmissionFilter,
|
SubmissionFilter,
|
||||||
SubmissionIn,
|
SubmissionIn,
|
||||||
SubmissionOut,
|
SubmissionOut,
|
||||||
@@ -81,6 +82,28 @@ def list_submissions(request, filters: SubmissionFilter = Query(...)):
|
|||||||
return submissions
|
return submissions
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/my-scores", response=List[MyScoreOut])
|
||||||
|
@login_required
|
||||||
|
def my_scores(request):
|
||||||
|
seen = {}
|
||||||
|
for s in Submission.objects.filter(
|
||||||
|
user=request.user, task__task_type="challenge"
|
||||||
|
).order_by("-score").select_related("task"):
|
||||||
|
if s.task_id not in seen:
|
||||||
|
seen[s.task_id] = s
|
||||||
|
return [
|
||||||
|
MyScoreOut(
|
||||||
|
task_id=s.task_id,
|
||||||
|
task_display=s.task.display,
|
||||||
|
task_title=s.task.title,
|
||||||
|
score=s.score,
|
||||||
|
created=s.created.isoformat(),
|
||||||
|
)
|
||||||
|
for s in seen.values()
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{submission_id}", response=SubmissionOut)
|
@router.get("/{submission_id}", response=SubmissionOut)
|
||||||
@login_required
|
@login_required
|
||||||
def get_submission(request, submission_id: UUID):
|
def get_submission(request, submission_id: UUID):
|
||||||
|
|||||||
@@ -136,3 +136,4 @@ def update_submission_score_on_save(sender, instance, **kwargs):
|
|||||||
当Rating保存时,更新对应的Submission的平均分
|
当Rating保存时,更新对应的Submission的平均分
|
||||||
"""
|
"""
|
||||||
instance.submission.update_score()
|
instance.submission.update_score()
|
||||||
|
instance.submission.user.profile.recalculate_total_score()
|
||||||
|
|||||||
@@ -105,3 +105,11 @@ class SubmissionFilter(Schema):
|
|||||||
|
|
||||||
class FlagIn(Schema):
|
class FlagIn(Schema):
|
||||||
flag: Optional[Literal["red", "blue", "green", "yellow"]] = None
|
flag: Optional[Literal["red", "blue", "green", "yellow"]] = None
|
||||||
|
|
||||||
|
|
||||||
|
class MyScoreOut(Schema):
|
||||||
|
task_id: int
|
||||||
|
task_display: int
|
||||||
|
task_title: str
|
||||||
|
score: float
|
||||||
|
created: str
|
||||||
|
|||||||
18
task/migrations/0006_challenge_pass_score.py
Normal file
18
task/migrations/0006_challenge_pass_score.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 6.0.1 on 2026-03-09 11:47
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('task', '0005_alter_task_options_alter_task_display_and_more'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='challenge',
|
||||||
|
name='pass_score',
|
||||||
|
field=models.FloatField(blank=True, null=True, verbose_name='通过分数线'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -40,6 +40,7 @@ class Tutorial(Task):
|
|||||||
|
|
||||||
class Challenge(Task):
|
class Challenge(Task):
|
||||||
score = models.IntegerField(default=0)
|
score = models.IntegerField(default=0)
|
||||||
|
pass_score = models.FloatField(null=True, blank=True, verbose_name="通过分数线")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|||||||
Reference in New Issue
Block a user