From e1ce8e19380715ffeb3152624b75b1cc19960e2d Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Sat, 21 Dec 2024 16:28:09 +1100 Subject: [PATCH] Update score_filter json mods to be acronym arrays --- common/osu/utils.py | 5 ++++ conftest.py | 2 +- leaderboards/views.py | 13 ++++------- .../management/commands/backfilljsonmods.py | 10 ++++---- ...efilter_disqualified_mods_json_and_more.py | 23 +++++++++++++++++++ profiles/models.py | 4 ++-- profiles/views.py | 16 ++++--------- users/test_views.py | 8 +++---- users/views.py | 16 ++++++------- 9 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 profiles/migrations/0027_alter_scorefilter_disqualified_mods_json_and_more.py diff --git a/common/osu/utils.py b/common/osu/utils.py index b6eb5d7..6c43e0a 100644 --- a/common/osu/utils.py +++ b/common/osu/utils.py @@ -271,6 +271,11 @@ def get_json_mods(mods: int, add_classic: bool) -> dict: return mods_dict +def get_mod_acronyms(mods: int) -> list[str]: + json_mods = get_json_mods(mods, add_classic=False) + return list(json_mods.keys()) + + def get_bitwise_mods(acronyms: list[str]) -> int: bitwise_mods = 0 for acronym in acronyms: diff --git a/conftest.py b/conftest.py index 50c2a84..e77b4ec 100644 --- a/conftest.py +++ b/conftest.py @@ -144,7 +144,7 @@ def score(user_stats: UserStats, beatmap: Beatmap): @pytest.fixture def score_filter(): return ScoreFilter.objects.create( - required_mods=Mods.HIDDEN, required_mods_json={NewMods.HIDDEN: {}} + required_mods=Mods.HIDDEN, required_mods_json=[NewMods.HIDDEN] ) diff --git a/leaderboards/views.py b/leaderboards/views.py index 3ac096a..739d7f9 100644 --- a/leaderboards/views.py +++ b/leaderboards/views.py @@ -1,6 +1,5 @@ from collections import OrderedDict -from django.core.cache import cache from django.core.exceptions import ValidationError from django.core.validators import URLValidator from rest_framework import permissions, status @@ -10,7 +9,7 @@ from common.osu.difficultycalculator import get_default_difficulty_calculator_class from common.osu.enums import Gamemode, Mods -from common.osu.utils import get_json_mods +from common.osu.utils import get_mod_acronyms from common.utils import parse_int_or_none from leaderboards.enums import LeaderboardAccessType from leaderboards.models import Invite, Leaderboard, Membership @@ -155,14 +154,12 @@ def post(self, request, leaderboard_type, gamemode): lowest_cs=score_filter_data.get("lowest_cs"), highest_cs=score_filter_data.get("highest_cs"), required_mods=score_filter_data.get("required_mods", Mods.NONE), - required_mods_json=get_json_mods( - score_filter_data.get("required_mods", Mods.NONE), - add_classic=False, + required_mods_json=get_mod_acronyms( + score_filter_data.get("required_mods", Mods.NONE) ), disqualified_mods=score_filter_data.get("disqualified_mods", Mods.NONE), - disqualified_mods_json=get_json_mods( - score_filter_data.get("disqualified_mods", Mods.NONE), - add_classic=False, + disqualified_mods_json=get_mod_acronyms( + score_filter_data.get("disqualified_mods", Mods.NONE) ), lowest_accuracy=score_filter_data.get("lowest_accuracy"), highest_accuracy=score_filter_data.get("highest_accuracy"), diff --git a/profiles/management/commands/backfilljsonmods.py b/profiles/management/commands/backfilljsonmods.py index 46a1ce5..9854cc4 100644 --- a/profiles/management/commands/backfilljsonmods.py +++ b/profiles/management/commands/backfilljsonmods.py @@ -1,7 +1,7 @@ from django.core.management.base import BaseCommand from tqdm import tqdm -from common.osu.utils import get_json_mods +from common.osu.utils import get_mod_acronyms from profiles.models import ScoreFilter @@ -11,10 +11,10 @@ class Command(BaseCommand): def handle(self, *args, **options): score_filters = ScoreFilter.objects.all() for score_filter in tqdm(score_filters): - score_filter.required_mods_json = get_json_mods( - score_filter.required_mods, False + score_filter.required_mods_json = get_mod_acronyms( + score_filter.required_mods ) - score_filter.disqualified_mods_json = get_json_mods( - score_filter.disqualified_mods, False + score_filter.disqualified_mods_json = get_mod_acronyms( + score_filter.disqualified_mods ) score_filter.save() diff --git a/profiles/migrations/0027_alter_scorefilter_disqualified_mods_json_and_more.py b/profiles/migrations/0027_alter_scorefilter_disqualified_mods_json_and_more.py new file mode 100644 index 0000000..e28e59c --- /dev/null +++ b/profiles/migrations/0027_alter_scorefilter_disqualified_mods_json_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.4 on 2024-12-21 05:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("profiles", "0026_scorefilter_disqualified_mods_json_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="scorefilter", + name="disqualified_mods_json", + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name="scorefilter", + name="required_mods_json", + field=models.JSONField(blank=True, default=list), + ), + ] diff --git a/profiles/models.py b/profiles/models.py index b306e28..e51bae5 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -626,9 +626,9 @@ class ScoreFilter(models.Model): lowest_cs = models.FloatField(null=True, blank=True) highest_cs = models.FloatField(null=True, blank=True) required_mods = models.IntegerField(default=Mods.NONE) - required_mods_json = models.JSONField(default=dict, blank=True) + required_mods_json = models.JSONField(default=list, blank=True) disqualified_mods = models.IntegerField(default=Mods.NONE) - disqualified_mods_json = models.JSONField(default=dict, blank=True) + disqualified_mods_json = models.JSONField(default=list, blank=True) lowest_accuracy = models.FloatField(null=True, blank=True) highest_accuracy = models.FloatField(null=True, blank=True) lowest_length = models.FloatField(null=True, blank=True) diff --git a/profiles/views.py b/profiles/views.py index abf6c29..f1e4b2e 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -6,7 +6,7 @@ from rest_framework.views import APIView from common.osu.enums import Gamemode, Mods -from common.osu.utils import get_json_mods +from common.osu.utils import get_json_mods, get_mod_acronyms from common.utils import parse_float_or_none, parse_int_or_none from leaderboards.models import Membership from leaderboards.serialisers import UserMembershipSerialiser @@ -121,21 +121,13 @@ def get(self, request, user_id, gamemode): highest_cs=parse_float_or_none(request.query_params.get("highest_cs")), required_mods=required_mods, required_mods_json=( - get_json_mods( - required_mods, - add_classic=False, - ) - if required_mods is not None - else {} + get_mod_acronyms(required_mods) if required_mods is not None else [] ), disqualified_mods=disqualified_mods, disqualified_mods_json=( - get_json_mods( - disqualified_mods, - add_classic=False, - ) + get_mod_acronyms(disqualified_mods) if disqualified_mods is not None - else {} + else [] ), lowest_accuracy=parse_float_or_none( request.query_params.get("lowest_accuracy") diff --git a/users/test_views.py b/users/test_views.py index ec68ee4..d68999d 100644 --- a/users/test_views.py +++ b/users/test_views.py @@ -86,9 +86,9 @@ def test_post(self, arf, view, user): assert response.status_code == HTTPStatus.OK assert response.data["name"] == "test sudden death filter" assert response.data["score_filter"]["required_mods"] == Mods.SUDDEN_DEATH - assert response.data["score_filter"]["required_mods_json"] == { - NewMods.SUDDEN_DEATH: {} - } + assert response.data["score_filter"]["required_mods_json"] == [ + NewMods.SUDDEN_DEATH + ] @pytest.mark.django_db @@ -127,7 +127,7 @@ def test_put(self, arf, view, user, score_filter_preset): assert response.status_code == HTTPStatus.OK assert response.data["name"] == "new name" assert response.data["score_filter"]["required_mods"] == Mods.NONE - assert response.data["score_filter"]["required_mods_json"] == {} + assert response.data["score_filter"]["required_mods_json"] == [] assert response.data["score_filter"]["highest_ar"] == 8 def test_delete(self, arf, view, user, score_filter_preset): diff --git a/users/views.py b/users/views.py index bff73db..db1d007 100644 --- a/users/views.py +++ b/users/views.py @@ -5,7 +5,7 @@ from rest_framework.views import APIView from common.osu.enums import Mods -from common.osu.utils import get_json_mods +from common.osu.utils import get_json_mods, get_mod_acronyms from leaderboards.models import Invite from leaderboards.serialisers import UserInviteSerialiser from osuauth.serialisers import UserSerialiser @@ -92,14 +92,12 @@ def post(self, request): lowest_cs=score_filter_data.get("lowest_cs"), highest_cs=score_filter_data.get("highest_cs"), required_mods=score_filter_data.get("required_mods", Mods.NONE), - required_mods_json=get_json_mods( - score_filter_data.get("required_mods", Mods.NONE), - add_classic=False, + required_mods_json=get_mod_acronyms( + score_filter_data.get("required_mods", Mods.NONE) ), disqualified_mods=score_filter_data.get("disqualified_mods", Mods.NONE), - disqualified_mods_json=get_json_mods( - score_filter_data.get("disqualified_mods", Mods.NONE), - add_classic=False, + disqualified_mods_json=get_mod_acronyms( + score_filter_data.get("disqualified_mods", Mods.NONE) ), lowest_accuracy=score_filter_data.get("lowest_accuracy"), highest_accuracy=score_filter_data.get("highest_accuracy"), @@ -164,13 +162,13 @@ def put(self, request, score_filter_preset_id): score_filter.required_mods = score_filter_data.get("required_mods", Mods.NONE) score_filter.required_mods_json = get_json_mods( score_filter_data.get("required_mods", Mods.NONE), add_classic=False - ) + ).keys() score_filter.disqualified_mods = score_filter_data.get( "disqualified_mods", Mods.NONE ) score_filter.disqualified_mods_json = get_json_mods( score_filter_data.get("disqualified_mods", Mods.NONE), add_classic=False - ) + ).keys() score_filter.lowest_accuracy = score_filter_data.get("lowest_accuracy") score_filter.highest_accuracy = score_filter_data.get("highest_accuracy") score_filter.lowest_length = score_filter_data.get("lowest_length")