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
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
from django.db.models import Prefetch
|
|
|
|
|
2018-03-01 19:38:48 +00:00
|
|
|
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
|
2018-09-06 18:35:02 +00:00
|
|
|
from funkwhale_api.music import utils as music_utils
|
2019-03-25 16:02:51 +00:00
|
|
|
from funkwhale_api.users.oauth import permissions as oauth_permissions
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-07-22 09:56:25 +00:00
|
|
|
from . import filters, models, serializers
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
class TrackFavoriteViewSet(
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
):
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.TrackFavoriteFilter
|
2017-06-23 21:00:42 +00:00
|
|
|
serializer_class = serializers.UserTrackFavoriteSerializer
|
2018-09-06 18:35:02 +00:00
|
|
|
queryset = models.TrackFavorite.objects.all().select_related("user")
|
2018-07-17 11:09:13 +00:00
|
|
|
permission_classes = [
|
2019-03-25 16:02:51 +00:00
|
|
|
oauth_permissions.ScopePermission,
|
2018-07-17 11:09:13 +00:00
|
|
|
permissions.OwnerPermission,
|
|
|
|
]
|
2019-03-25 16:02:51 +00:00
|
|
|
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
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
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)
|
2018-03-01 19:38:48 +00:00
|
|
|
record.send(instance)
|
2018-06-09 13:36:16 +00:00
|
|
|
return Response(
|
|
|
|
serializer.data, status=status.HTTP_201_CREATED, headers=headers
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2018-07-17 11:09:13 +00:00
|
|
|
queryset = super().get_queryset()
|
2018-09-06 18:35:02 +00:00
|
|
|
queryset = queryset.filter(
|
2018-07-17 11:09:13 +00:00
|
|
|
fields.privacy_level_query(self.request.user, "user__privacy_level")
|
|
|
|
)
|
2018-10-26 12:18:50 +00:00
|
|
|
tracks = Track.objects.with_playable_uploads(
|
2018-09-06 18:35:02 +00:00
|
|
|
music_utils.get_actor_from_request(self.request)
|
|
|
|
).select_related("artist", "album__artist")
|
|
|
|
queryset = queryset.prefetch_related(Prefetch("track", queryset=tracks))
|
|
|
|
return queryset
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
2018-06-09 13:36:16 +00:00
|
|
|
track = Track.objects.get(pk=serializer.data["track"])
|
2017-06-23 21:00:42 +00:00
|
|
|
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)
|
2017-06-23 21:00:42 +00:00
|
|
|
def remove(self, request, *args, **kwargs):
|
|
|
|
try:
|
2018-06-09 13:36:16 +00:00
|
|
|
pk = int(request.data["track"])
|
2017-06-23 21:00:42 +00:00
|
|
|
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-10 10:58:18 +00:00
|
|
|
|
2019-01-11 12:33:35 +00:00
|
|
|
@action(methods=["get"], detail=False)
|
2019-01-10 10:58:18 +00:00
|
|
|
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)
|