diff --git a/accounts/views.py b/accounts/views.py index 6e3e34f..618dbc7 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -11,7 +11,7 @@ from django.views.generic import CreateView from accounts.forms import CustomUserCreationForm from accounts.models import User -from twitch.models import NotificationSubscription +from twitch.models import Game, NotificationSubscription if TYPE_CHECKING: from django.forms import BaseModelForm @@ -86,14 +86,50 @@ def profile_view(request: HttpRequest) -> HttpResponse: HttpResponse: Rendered profile template. """ subscriptions = NotificationSubscription.objects.filter(user=request.user) # type: ignore[misc] - game_subscriptions = subscriptions.filter(game_id__isnull=False) + + # Direct game subscriptions + direct_game_subscriptions = subscriptions.filter(game_id__isnull=False) + + # Organization subscriptions org_subscriptions = subscriptions.filter(organization_id__isnull=False) + + # Get games from organization subscriptions (inherited games) + inherited_games = Game.objects.filter(owner__in=org_subscriptions.values("organization")) + + # Create mapping of inherited game IDs to their organization names + inherited_game_org_map = {} + for game in inherited_games: + if game.owner: + inherited_game_org_map[game.id] = game.owner.name + + # Combine direct and inherited game IDs + direct_game_ids = direct_game_subscriptions.values_list("game_id", flat=True) + inherited_game_ids = inherited_games.values_list("id", flat=True) + all_game_ids = set(direct_game_ids) | set(inherited_game_ids) + + # Get all games (direct + inherited) + game_subscriptions_combined = Game.objects.filter(id__in=all_game_ids) + + # Get direct game IDs for template indication + direct_game_ids = list(direct_game_subscriptions.values_list("game_id", flat=True)) + + # Get all games (direct + inherited) with inheritance info + games_with_inheritance = [] + for game in game_subscriptions_combined: + is_inherited = game.id not in direct_game_ids + inherited_from = inherited_game_org_map.get(game.id) if is_inherited else None + games_with_inheritance.append({ + "game": game, + "is_inherited": is_inherited, + "inherited_from": inherited_from, + }) + return render( request, "accounts/profile.html", { "user": request.user, - "game_subscriptions": game_subscriptions, + "games_with_inheritance": games_with_inheritance, "org_subscriptions": org_subscriptions, }, ) diff --git a/templates/accounts/profile.html b/templates/accounts/profile.html index e17263e..342b8e6 100644 --- a/templates/accounts/profile.html +++ b/templates/accounts/profile.html @@ -29,9 +29,12 @@