def post(self, request): data = request.data serializer = SongSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def post(self, request, format="json"): if request.user.accountType != 'A': return Response({"Response": "Not Allowed"}, status=status.HTTP_403_FORBIDDEN) if request.FILES['song']: song = request.FILES['song'] fs = FileSystemStorage() filename = fs.save('./static/' + song.name, song) song_url = 'http://' + str( get_current_site(request)) + fs.url(filename) data = { "title": request.data["title"], "url": song_url, "genre": request.data["genre"], "release_date": request.data["release_date"] } context = {'request': request} song = SongSerializer(data=data, context=context) if song.is_valid(): song.save() return Response(song.data, status=status.HTTP_200_OK) return Response(song.errors, status=status.HTTP_400_BAD_REQUEST) return Response({"eror": "No song provided"}, status=status.HTTP_400_BAD_REQUEST)
def create_song(request): # obj = Artist.objects.get(artist_id=artist_id) # print(obj) # composer = Song.objects.get(artist_of_song=obj) if request.method == 'POST': data = JSONParser().parse(request) serializer = SongSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors)
def test_song_using_put(self): url = reverse( 'common-actions-audio-file', kwargs={'audiofiletype': SONG, 'audiofileid': self.song.pk} ) response = self.client.put(url, data={'name': 'Changed', 'duration': self.song.duration}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, SongSerializer(instance=Song.objects.get()).data)
def get(self, request): category = request.data.get("category", "") length = request.data.get("length", 10) name = request.data.get("name", uuid.uuid4().hex[:6].upper()) curated_playlist = recommendations(name, length, True, category, request) songs = SongSerializer(curated_playlist, many=True) return Response(songs.data, status=status.HTTP_202_ACCEPTED)
def get(self, request): song_pk = request.GET.get("song_pk", "") songs = Song.objects.all() if song_pk: songs = Song.objects.get(pk=pk) songSerializer = SongSerializer(songs, many=True) return Response(songSerializer.data, status=status.HTTP_200_OK)
def get(self, request, artist_id): obj = Artist.objects.get(artist_id=artist_id) print(obj) obj2 = Song.objects.filter(artist_of_song=obj) # obj2 = Song(artist_of_song=obj) serializer = ArtistSerializer(obj) serializer2 = SongSerializer(obj2, many=True) #serializer2 = SongSerializer(obj2) return Response(serializer2.data)
def test_create_song(self): song_name = 'Rolex' song_duration = 249 now = timezone.now() response = self.client.post(path=self.url, data={ "audiofiletype": SONG, "audiofilemetadata": { "name": song_name, "duration": song_duration } }) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, SongSerializer(instance=Song.objects.get()).data) self.assertGreater(parse_datetime(response.data.get('uploaded_time')), now) self.assertEqual(Song.objects.count(), 1)
def detail(request, id): obj = Song.objects.get(id=id) if request.method == 'GET': serializer = SongSerializer(obj) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = SongSerializer(obj, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return JsonResponse(serializer.errors) elif request.method == 'DELETE': obj.delete() return Response(status=204)
def test_song_update_using_patch(self): url = reverse('common-actions-audio-file', kwargs={'audiofiletype': SONG, 'audiofileid': self.song.id}) patch_response = self.client.patch(url, data={'name': 'Changed'}) self.assertEqual(patch_response.data, SongSerializer(Song.objects.get()).data) self.assertEqual(patch_response.status_code, status.HTTP_200_OK)
def test_list_songs(self): url = reverse('list-audio-files', kwargs={'audiofiletype': SONG}) response = self.client.get(url) self.assertEqual(response.data, SongSerializer(Song.objects.all(), many=True).data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_song_by_id(self): url = reverse('common-actions-audio-file', kwargs={'audiofiletype': SONG, 'audiofileid': self.song.pk}) response = self.client.get(url) self.assertEqual(response.data, SongSerializer(instance=self.song).data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def get(self, request): obj = Song.objects.all() serializer = SongSerializer(obj, many=True) return Response(serializer.data)