Skip to content

Commit

Permalink
Refactor out task dependencies in services
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed May 19, 2024
1 parent 95d3be0 commit a799f61
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion leaderboards/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
4 changes: 2 additions & 2 deletions leaderboards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions profiles/management/commands/updateusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(
Expand Down
7 changes: 0 additions & 7 deletions profiles/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
21 changes: 20 additions & 1 deletion profiles/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down

0 comments on commit a799f61

Please sign in to comment.