From 613de5ae63db381f0861756aae0f4f4ae0c16cfa Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Sat, 1 Jun 2024 00:26:09 +1000 Subject: [PATCH 1/3] Remove Membership.recalculate() --- leaderboards/models.py | 24 --------------------- profiles/management/commands/recalculate.py | 3 ++- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/leaderboards/models.py b/leaderboards/models.py index 95a1bea..553ce73 100644 --- a/leaderboards/models.py +++ b/leaderboards/models.py @@ -196,30 +196,6 @@ class Membership(models.Model): CommunityMembershipQuerySet )() - def recalculate(self): - """ - Recalculates the memberships pp from its score list - """ - if self.leaderboard.score_set == ScoreSet.NORMAL: - self.pp = calculate_pp_total( - score.performance_total - for score in self.scores.order_by("-performance_total").all() - ) - elif self.leaderboard.score_set == ScoreSet.NEVER_CHOKE: - self.pp = calculate_pp_total( - ( - score.nochoke_performance_total - if score.result & ScoreResult.CHOKE - else score.performance_total - ) - for score in self.scores.order_by("-performance_total").all() - ) - elif self.leaderboard.score_set == ScoreSet.ALWAYS_FULL_COMBO: - self.pp = calculate_pp_total( - score.nochoke_performance_total - for score in self.scores.order_by("-performance_total").all() - ) - def __str__(self): return f"{self.leaderboard}: {self.user.username}" diff --git a/profiles/management/commands/recalculate.py b/profiles/management/commands/recalculate.py index 29f9ed5..7f98769 100644 --- a/profiles/management/commands/recalculate.py +++ b/profiles/management/commands/recalculate.py @@ -14,6 +14,7 @@ ) from common.osu.enums import Gamemode from leaderboards.models import Membership +from leaderboards.services import update_membership from profiles.models import Beatmap, Score, UserStats from profiles.services import ( update_difficulty_calculations, @@ -410,7 +411,7 @@ def recalculate_memberships( with tqdm(desc="Memberships", total=memberships.count(), smoothing=0) as pbar: for page in paginator: for membership in page: - membership.recalculate() + update_membership(membership.leaderboard, membership.user_id) pbar.update() Membership.objects.bulk_update(page, ["pp"]) From 4f107c8c8bc9b0510aa4faa1cd929371dcd5ab22 Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Sat, 1 Jun 2024 00:26:39 +1000 Subject: [PATCH 2/3] Fix lb scores not returning requested amount --- leaderboards/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leaderboards/views.py b/leaderboards/views.py index 595ab10..fb2cbf2 100644 --- a/leaderboards/views.py +++ b/leaderboards/views.py @@ -291,7 +291,7 @@ def get(self, request, leaderboard_type, gamemode, leaderboard_id): 1800, ) else: - scores = leaderboard.get_top_scores(limit=5) + scores = leaderboard.get_top_scores(limit=limit) serialiser = LeaderboardScoreSerialiser(scores, many=True) return Response(serialiser.data) From 60d6d758fdadd7dea1644e36b9b0911b35e56271 Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Sat, 1 Jun 2024 00:32:22 +1000 Subject: [PATCH 3/3] Add unique constraint for memberships --- .../0021_membership_unique_memberships.py | 19 +++++++++++++++++++ leaderboards/models.py | 6 ++++++ 2 files changed, 25 insertions(+) create mode 100644 leaderboards/migrations/0021_membership_unique_memberships.py diff --git a/leaderboards/migrations/0021_membership_unique_memberships.py b/leaderboards/migrations/0021_membership_unique_memberships.py new file mode 100644 index 0000000..dff46aa --- /dev/null +++ b/leaderboards/migrations/0021_membership_unique_memberships.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.11 on 2024-05-31 14:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("leaderboards", "0020_membershipscore_performance_total_and_more"), + ] + + operations = [ + migrations.AddConstraint( + model_name="membership", + constraint=models.UniqueConstraint( + fields=("leaderboard_id", "user_id"), name="unique_memberships" + ), + ), + ] diff --git a/leaderboards/models.py b/leaderboards/models.py index 553ce73..17b4cf2 100644 --- a/leaderboards/models.py +++ b/leaderboards/models.py @@ -200,6 +200,12 @@ def __str__(self): return f"{self.leaderboard}: {self.user.username}" class Meta: + constraints = [ + models.UniqueConstraint( + fields=["leaderboard_id", "user_id"], name="unique_memberships" + ) + ] + indexes = [models.Index(fields=["leaderboard"]), models.Index(fields=["user"])]