From a799f61e09744d2c32002f9e5eca6ccdef84d9f1 Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Sun, 19 May 2024 17:12:24 +1000 Subject: [PATCH] Refactor out task dependencies in services --- leaderboards/test_views.py | 2 +- leaderboards/views.py | 4 ++-- profiles/management/commands/updateusers.py | 4 ++-- profiles/services.py | 7 ------- profiles/tasks.py | 21 ++++++++++++++++++++- profiles/views.py | 8 ++++---- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/leaderboards/test_views.py b/leaderboards/test_views.py index 12d9a22..cb1e0ed 100644 --- a/leaderboards/test_views.py +++ b/leaderboards/test_views.py @@ -245,7 +245,7 @@ def test_post(self, arf, view, leaderboard, osu_user): url = reverse("leaderboard-invite-list", kwargs=kwargs) request = arf.post( url, - data={"user_ids": [osu_user.id], "message": "test message"}, + data={"user_ids": [5701575], "message": "test message"}, format="json", ) diff --git a/leaderboards/views.py b/leaderboards/views.py index 738b05c..595ab10 100644 --- a/leaderboards/views.py +++ b/leaderboards/views.py @@ -26,7 +26,7 @@ from profiles.enums import AllowedBeatmapStatus, ScoreSet from profiles.models import Score, ScoreFilter from profiles.serialisers import BeatmapScoreSerialiser, UserScoreSerialiser -from profiles.services import refresh_user_from_api +from profiles.tasks import update_user class LeaderboardList(APIView): @@ -459,7 +459,7 @@ def post(self, request, leaderboard_type, gamemode, leaderboard_id): invite = leaderboard.invites.get(user_id=invitee_id) except Invite.DoesNotExist: # update profile to ensure they are in the database - refresh_user_from_api(user_id=invitee_id, gamemode=gamemode) + update_user(user_id=invitee_id, gamemode=gamemode) invite = Invite( user_id=invitee_id, leaderboard_id=leaderboard_id, message=message diff --git a/profiles/management/commands/updateusers.py b/profiles/management/commands/updateusers.py index 37b4968..263270a 100644 --- a/profiles/management/commands/updateusers.py +++ b/profiles/management/commands/updateusers.py @@ -3,7 +3,7 @@ from django.conf import settings from django.core.management.base import BaseCommand -from profiles.services import refresh_user_from_api +from profiles.tasks import update_user class Command(BaseCommand): @@ -25,7 +25,7 @@ def handle(self, *args, **options): user_ids += fp.readlines() for user_id in user_ids: - user_stats = refresh_user_from_api(user_id=user_id, gamemode=gamemode) + user_stats = update_user(user_id=user_id, gamemode=gamemode) self.stdout.write( self.style.SUCCESS( diff --git a/profiles/services.py b/profiles/services.py index 94261c4..f793835 100644 --- a/profiles/services.py +++ b/profiles/services.py @@ -223,13 +223,6 @@ def refresh_user_from_api( for score in created_scores: update_performance_calculation(score, difficulty_calculator) - # Update memberships - transaction.on_commit( - lambda: update_memberships.delay( - user_id=user_stats.user_id, gamemode=user_stats.gamemode - ) - ) - return user_stats diff --git a/profiles/tasks.py b/profiles/tasks.py index 4a1adda..9f33027 100644 --- a/profiles/tasks.py +++ b/profiles/tasks.py @@ -2,6 +2,7 @@ from common.osu.enums import Gamemode from leaderboards.models import Leaderboard +from leaderboards.tasks import update_memberships from profiles.services import refresh_user_from_api @@ -36,4 +37,22 @@ def update_user(user_id: int, gamemode: int = Gamemode.STANDARD): """ Runs an update for a given user """ - refresh_user_from_api(user_id=user_id, gamemode=Gamemode(gamemode)) + user_stats = refresh_user_from_api(user_id=user_id, gamemode=Gamemode(gamemode)) + if user_stats is not None: + update_memberships.delay( + user_id=user_stats.user_id, gamemode=user_stats.gamemode + ) + return user_stats + + +@shared_task +def update_user_by_username(username: str, gamemode: int = Gamemode.STANDARD): + """ + Runs an update for a given user + """ + user_stats = refresh_user_from_api(username=username, gamemode=Gamemode(gamemode)) + if user_stats is not None: + update_memberships.delay( + user_id=user_stats.user_id, gamemode=user_stats.gamemode + ) + return user_stats diff --git a/profiles/views.py b/profiles/views.py index a61c640..72d2a0d 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -16,8 +16,8 @@ UserScoreSerialiser, UserStatsSerialiser, ) -from profiles.services import fetch_scores, fetch_user, refresh_user_from_api -from profiles.tasks import update_user +from profiles.services import fetch_scores, fetch_user +from profiles.tasks import update_user, update_user_by_username class UserStatsDetail(APIView): @@ -37,13 +37,13 @@ def get(self, request, user_string, gamemode): if user_id_type == "id": user_stats = fetch_user(user_id=int(user_string), gamemode=gamemode) if user_stats is None: - user_stats = refresh_user_from_api( + user_stats = update_user( user_id=int(user_string), gamemode=gamemode ) elif user_id_type == "username": user_stats = fetch_user(username=user_string, gamemode=gamemode) if user_stats is None: - user_stats = refresh_user_from_api( + user_stats = update_user_by_username( username=user_string, gamemode=gamemode ) else: