class AlbumByIdSerializer(serializers.ModelSerializer): artist = ArtistSerializer() tracks = TrackSerializer(many=True) class Meta: model = Album fields = ['id', 'title', 'price', 'created_at', 'artist', 'tracks']
def list(self, request): queryset = list(Track.objects.all()) data = [] for track in queryset: retrieved = GenericProvider(track).retrieve() data.append(retrieved) serializer = TrackSerializer(data, many=True) return Response(serializer.data)
def broadcast_track_changed(self, track): self.publish('player', TrackSerializer(track).data) if (track and track.service == Track.SERVICES.deezer and track.votes.count() > 0): try: self.scrobble(track.artist, track.title) except (pylast.WSError, IndexError): # there is a bug in current version that raises IndexError # at some cases pass self.broadcast_list_changed()
def create(self, request, *args, **kwargs): service_id = self.kwargs['service_id'] service = self.kwargs['service'] try: self.track = Track.fetch_and_save_track(service, service_id, self.establishment) Vote.objects.create(track=self.track, token=self.get_token()) except RequestException: return Response(status=status.HTTP_503_SERVICE_UNAVAILABLE) except ValueError as e: return Response(status=status.HTTP_400_BAD_REQUEST, data=e.message) serializer = TrackSerializer(self.track) self.broadcast_list_changed() return Response(serializer.data, status=status.HTTP_201_CREATED)
class PlayListSerializer(serializers.ModelSerializer): track = TrackSerializer(read_only=True) class Meta: model = PlayList fields = ('id', 'track', 'order')
def retrieve(self, request, pk=None): track = get_object_or_404(Track, id=pk) retrieved = GenericProvider(track).retrieve serializer = TrackSerializer(retrieved) return Response(serializer.data)
def create(self, request): serializer = TrackSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)