funkwhale/api/funkwhale_api/favorites/views.py

91 wiersze
3.4 KiB
Python
Czysty Zwykły widok Historia

2018-06-10 08:55:16 +00:00
from rest_framework import mixins, status, viewsets
2019-01-11 12:33:35 +00:00
from rest_framework.decorators import action
2018-06-10 08:55:16 +00:00
from rest_framework.response import Response
from django.db.models import Prefetch
from funkwhale_api.activity import record
2018-07-17 11:09:13 +00:00
from funkwhale_api.common import fields, permissions
2018-06-10 08:55:16 +00:00
from funkwhale_api.music.models import Track
from funkwhale_api.music import utils as music_utils
from funkwhale_api.users.oauth import permissions as oauth_permissions
from . import filters, models, serializers
2018-06-09 13:36:16 +00:00
class TrackFavoriteViewSet(
mixins.CreateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet,
):
filterset_class = filters.TrackFavoriteFilter
serializer_class = serializers.UserTrackFavoriteSerializer
queryset = models.TrackFavorite.objects.all().select_related("user")
2018-07-17 11:09:13 +00:00
permission_classes = [
oauth_permissions.ScopePermission,
2018-07-17 11:09:13 +00:00
permissions.OwnerPermission,
]
required_scope = "favorites"
anonymous_policy = "setting"
2018-07-17 11:09:13 +00:00
owner_checks = ["write"]
def get_serializer_class(self):
if self.request.method.lower() in ["head", "get", "options"]:
return serializers.UserTrackFavoriteSerializer
return serializers.UserTrackFavoriteWriteSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = self.perform_create(serializer)
serializer = self.get_serializer(instance=instance)
headers = self.get_success_headers(serializer.data)
record.send(instance)
2018-06-09 13:36:16 +00:00
return Response(
serializer.data, status=status.HTTP_201_CREATED, headers=headers
)
def get_queryset(self):
2018-07-17 11:09:13 +00:00
queryset = super().get_queryset()
queryset = queryset.filter(
2018-07-17 11:09:13 +00:00
fields.privacy_level_query(self.request.user, "user__privacy_level")
)
tracks = Track.objects.with_playable_uploads(
music_utils.get_actor_from_request(self.request)
).select_related("artist", "album__artist")
queryset = queryset.prefetch_related(Prefetch("track", queryset=tracks))
return queryset
def perform_create(self, serializer):
2018-06-09 13:36:16 +00:00
track = Track.objects.get(pk=serializer.data["track"])
favorite = models.TrackFavorite.add(track=track, user=self.request.user)
return favorite
2019-01-11 12:33:35 +00:00
@action(methods=["delete", "post"], detail=False)
def remove(self, request, *args, **kwargs):
try:
2018-06-09 13:36:16 +00:00
pk = int(request.data["track"])
favorite = request.user.track_favorites.get(track__pk=pk)
except (AttributeError, ValueError, models.TrackFavorite.DoesNotExist):
return Response({}, status=400)
favorite.delete()
return Response([], status=status.HTTP_204_NO_CONTENT)
2019-01-11 12:33:35 +00:00
@action(methods=["get"], detail=False)
def all(self, request, *args, **kwargs):
"""
Return all the favorites of the current user, with only limited data
to have a performant endpoint and avoid lots of queries just to display
favorites status in the UI
"""
if not request.user.is_authenticated:
return Response({"results": [], "count": 0}, status=200)
favorites = list(
request.user.track_favorites.values("id", "track").order_by("id")
)
payload = {"results": favorites, "count": len(favorites)}
return Response(payload, status=200)