Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix membership missing unique #57

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions leaderboards/migrations/0021_membership_unique_memberships.py
Original file line number Diff line number Diff line change
@@ -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"
),
),
]
30 changes: 6 additions & 24 deletions leaderboards/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,34 +196,16 @@ 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}"

class Meta:
constraints = [
models.UniqueConstraint(
fields=["leaderboard_id", "user_id"], name="unique_memberships"
)
]

indexes = [models.Index(fields=["leaderboard"]), models.Index(fields=["user"])]


Expand Down
2 changes: 1 addition & 1 deletion leaderboards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion profiles/management/commands/recalculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"])

Expand Down