add leaderboard
This commit is contained in:
@@ -6,6 +6,7 @@ from ninja.pagination import paginate
|
||||
from ninja.errors import HttpError
|
||||
from .schemas import (
|
||||
BatchUsersIn,
|
||||
LeaderboardEntry,
|
||||
UserListSchema,
|
||||
UserRegistrationSchema,
|
||||
UserLoginSchema,
|
||||
@@ -119,3 +120,18 @@ def toggle_user_is_active(request, id: int):
|
||||
}
|
||||
except User.DoesNotExist:
|
||||
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):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
total_score = models.IntegerField(default=0)
|
||||
total_score = models.FloatField(default=0.0)
|
||||
|
||||
def __str__(self):
|
||||
return self.user.username
|
||||
|
||||
def update_total_score(self, score: int):
|
||||
self.total_score = self.total_score + score
|
||||
self.save()
|
||||
def recalculate_total_score(self):
|
||||
from django.db.models import Max, Sum
|
||||
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)
|
||||
|
||||
@@ -46,3 +46,9 @@ class UserLoginSchema(Schema):
|
||||
class BatchUsersIn(Schema):
|
||||
names: List[str]
|
||||
classname: str
|
||||
|
||||
|
||||
class LeaderboardEntry(Schema):
|
||||
rank: int
|
||||
username: str
|
||||
total_score: float
|
||||
|
||||
Reference in New Issue
Block a user