|
|
@ -74,7 +74,7 @@ class PodcastListSerializer(serializers.ModelSerializer): |
|
|
def get_in_user_playlist(self, obj): |
|
|
def get_in_user_playlist(self, obj): |
|
|
""" |
|
|
""" |
|
|
Check if the podcast is in the user's personal playlist. |
|
|
Check if the podcast is in the user's personal playlist. |
|
|
Returns True if the podcast is in the user's playlist and active, False otherwise. |
|
|
|
|
|
|
|
|
Uses request-level set cache to prevent N+1 queries when serializing lists. |
|
|
""" |
|
|
""" |
|
|
request = self.context.get('request') |
|
|
request = self.context.get('request') |
|
|
user = request.user if request and request.user.is_authenticated else None |
|
|
user = request.user if request and request.user.is_authenticated else None |
|
|
@ -82,7 +82,15 @@ class PodcastListSerializer(serializers.ModelSerializer): |
|
|
if not user: |
|
|
if not user: |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
return UserPlaylist.is_in_user_playlist(user, obj) |
|
|
|
|
|
|
|
|
cache_attr = f'_user_playlist_podcast_ids_{user.id}' |
|
|
|
|
|
if not hasattr(request, cache_attr): |
|
|
|
|
|
setattr( |
|
|
|
|
|
request, |
|
|
|
|
|
cache_attr, |
|
|
|
|
|
set(UserPlaylist.objects.filter(user=user, status=True).values_list('podcast_id', flat=True)) |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
return obj.id in getattr(request, cache_attr) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PodcastDetailSerializer(serializers.ModelSerializer): |
|
|
class PodcastDetailSerializer(serializers.ModelSerializer): |
|
|
@ -215,15 +223,23 @@ class PodcastDetailSerializer(serializers.ModelSerializer): |
|
|
def get_in_user_playlist(self, obj): |
|
|
def get_in_user_playlist(self, obj): |
|
|
""" |
|
|
""" |
|
|
Check if the podcast is in the user's personal playlist. |
|
|
Check if the podcast is in the user's personal playlist. |
|
|
Returns True if the podcast is in the user's playlist and active, False otherwise. |
|
|
|
|
|
|
|
|
Uses request-level set cache to prevent N+1 queries when serializing lists. |
|
|
""" |
|
|
""" |
|
|
request = self.context.get('request') |
|
|
request = self.context.get('request') |
|
|
user = request.user if request and request.user.is_authenticated else None |
|
|
user = request.user if request and request.user.is_authenticated else None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not user: |
|
|
if not user: |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
return UserPlaylist.is_in_user_playlist(user, obj) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cache_attr = f'_user_playlist_podcast_ids_{user.id}' |
|
|
|
|
|
if not hasattr(request, cache_attr): |
|
|
|
|
|
setattr( |
|
|
|
|
|
request, |
|
|
|
|
|
cache_attr, |
|
|
|
|
|
set(UserPlaylist.objects.filter(user=user, status=True).values_list('podcast_id', flat=True)) |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
return obj.id in getattr(request, cache_attr) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PinnedPodcastCollectionSerializer(serializers.ModelSerializer): |
|
|
class PinnedPodcastCollectionSerializer(serializers.ModelSerializer): |
|
|
|