update
This commit is contained in:
@@ -7,7 +7,6 @@ from ninja.errors import HttpError
|
|||||||
from .schemas import (
|
from .schemas import (
|
||||||
BatchUsersIn,
|
BatchUsersIn,
|
||||||
ClassStudentEntry,
|
ClassStudentEntry,
|
||||||
LeaderboardEntry,
|
|
||||||
UserListSchema,
|
UserListSchema,
|
||||||
UserRegistrationSchema,
|
UserRegistrationSchema,
|
||||||
UserLoginSchema,
|
UserLoginSchema,
|
||||||
@@ -123,21 +122,6 @@ def toggle_user_is_active(request, id: int):
|
|||||||
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)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/classes", response=List[str])
|
@router.get("/classes", response=List[str])
|
||||||
def list_classes(request):
|
def list_classes(request):
|
||||||
"""返回所有不重复的非空班级名列表,按字典序升序"""
|
"""返回所有不重复的非空班级名列表,按字典序升序"""
|
||||||
|
|||||||
16
account/migrations/0004_remove_profile.py
Normal file
16
account/migrations/0004_remove_profile.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# Generated by Django 6.0.1 on 2026-03-18 10:38
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('account', '0003_user_classname'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.DeleteModel(
|
||||||
|
name='Profile',
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
|
|
||||||
from django.db.models.signals import post_save
|
|
||||||
from django.dispatch import receiver
|
|
||||||
|
|
||||||
|
|
||||||
class RoleChoices(models.TextChoices):
|
class RoleChoices(models.TextChoices):
|
||||||
SUPER = "super", "超级管理员"
|
SUPER = "super", "超级管理员"
|
||||||
@@ -51,33 +48,3 @@ class User(AbstractUser):
|
|||||||
verbose_name_plural = verbose_name
|
verbose_name_plural = verbose_name
|
||||||
|
|
||||||
|
|
||||||
class Profile(models.Model):
|
|
||||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
|
||||||
total_score = models.FloatField(default=0.0)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return self.user.username
|
|
||||||
|
|
||||||
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)
|
|
||||||
def create_user_profile(sender, instance, created, **kwargs):
|
|
||||||
if created:
|
|
||||||
Profile.objects.create(user=instance)
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=User)
|
|
||||||
def save_user_profile(sender, instance, **kwargs):
|
|
||||||
instance.profile.save()
|
|
||||||
|
|||||||
@@ -48,12 +48,6 @@ class BatchUsersIn(Schema):
|
|||||||
classname: str
|
classname: str
|
||||||
|
|
||||||
|
|
||||||
class LeaderboardEntry(Schema):
|
|
||||||
rank: int
|
|
||||||
username: str
|
|
||||||
total_score: float
|
|
||||||
|
|
||||||
|
|
||||||
class ClassStudentEntry(Schema):
|
class ClassStudentEntry(Schema):
|
||||||
name: str
|
name: str
|
||||||
username: str
|
username: str
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from django.db.models import Count, OuterRef, Q, Subquery, IntegerField
|
|||||||
|
|
||||||
from .schemas import (
|
from .schemas import (
|
||||||
FlagIn,
|
FlagIn,
|
||||||
MyScoreOut,
|
|
||||||
SubmissionFilter,
|
SubmissionFilter,
|
||||||
SubmissionIn,
|
SubmissionIn,
|
||||||
SubmissionOut,
|
SubmissionOut,
|
||||||
@@ -123,27 +122,6 @@ 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("/by-user-task", response=List[SubmissionOut])
|
@router.get("/by-user-task", response=List[SubmissionOut])
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from django.utils import timezone
|
|||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver # 导入receiver
|
from django.dispatch import receiver # 导入receiver
|
||||||
|
|
||||||
from account.models import Profile, RoleChoices, User
|
from account.models import RoleChoices, User
|
||||||
from task.models import Task
|
from task.models import Task
|
||||||
from prompt.models import Conversation
|
from prompt.models import Conversation
|
||||||
|
|
||||||
@@ -93,8 +93,6 @@ class Submission(TimeStampedModel):
|
|||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
# if self.score > 0:
|
|
||||||
# self.user.profile.update_total_score(self.score)
|
|
||||||
|
|
||||||
|
|
||||||
class Rating(models.Model):
|
class Rating(models.Model):
|
||||||
@@ -141,4 +139,3 @@ 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()
|
|
||||||
|
|||||||
@@ -121,9 +121,3 @@ 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
|
|
||||||
|
|||||||
Reference in New Issue
Block a user