def put(self, request, artist_id, format = None): music = self.get_object(artist_id) serializer = MusicSerializer(music, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, number = None, format = None): if (int(number) == 0): raise Http404 music = MusicPost.objects.order_by('artist_hotttnesss').reverse().values('artist_id', 'artist_name', 'artist_info').distinct() title = "List of artists by popularity" if (number == None): serializer = MusicSerializer(music, many=True) else: serializer = MusicSerializer(music[:int(number)], many=True) return Response({'object_list': serializer.data, 'title': title})
def post(self, request, room, format=None): """ Post new music --- parameters: - name: music_id type: string paramType: body required: true - name: url type: string paramType: body required: true - name: thumbnail type: string paramType: body required: true - name: total_duration type: integer paramType: body required: true - name: duration type: integer paramType: body required: true - name: timer_start type: integer paramType: body - name: source type: string paramType: body required: true - name: name type: string paramType: body required: true """ request.data.update({'room_id': room.id}) try: music = Music.objects.get(music_id=request.data.get('music_id'), room=room) serializer = MusicSerializer(music, data=request.data, partial=True) except Music.DoesNotExist: serializer = MusicSerializer(data=request.data) if serializer.is_valid(): music = serializer.save() room.add_music(music) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, artist_id = None, song_year=None, format = None): if (artist_id == None): raise Http404 qs = MusicPost.objects.filter(artist_id = artist_id).all() if (len(qs)==0): raise Http404 music = list(MusicPost.objects.filter(artist_id = artist_id).filter(song_year=song_year).values_list('song_hotttnesss', flat=True)) numb = len(music) if(numb > 1): std_deviation = statistics.stdev(music) else: std_deviation = 0 serializer = MusicSerializer(music[0]) artist_name = MusicPost.objects.filter(artist_id = artist_id).all() list_year = list(MusicPost.objects.filter(artist_id = artist_id).values_list('song_year', flat=True).distinct()) context = { "object": serializer.data, "artist_name": artist_name[0].artist_name, "artist_id": artist_id, "stat": list_year, "year": song_year, "mean": 'http://127.0.0.1:8000/juceer/0.1/statistics/mean/artists/'+ str(artist_id) + '/', "median": 'http://127.0.0.1:8000/juceer/0.1/statistics/median/artists/'+ str(artist_id) + '/', "std_deviation": str(std_deviation), "songs_of_the_same_artist": 'http://127.0.0.1:8000/juceer/0.1/artists/' + str(artist_id) + '/songs/', } return Response(context)
def get(self, request, artist_id=None, format=None): if (artist_id == None): raise Http404 music = list( MusicPost.objects.filter(artist_id=artist_id).values_list( 'song_hotttnesss', flat=True)) if (len(music) == 0): raise Http404 serializer = MusicSerializer(music[0]) list_year = list( MusicPost.objects.filter(artist_id=artist_id).values_list( 'song_year', flat=True).distinct()) context = { "object": serializer.data, "stat": list_year, "mean": 'http://127.0.0.1:8000/juceer/0.1/statistics/mean/artists/' + str(artist_id) + '/', "median": statistics.median(music), "std_deviation": 'http://127.0.0.1:8000/juceer/0.1/statistics/std_dev/artists/' + str(artist_id) + '/', "songs_of_the_same_artist": 'http://127.0.0.1:8000/juceer/0.1/artists/' + str(artist_id) + '/songs/', } return Response(context)
def test_music_serializer(self): m = Music( music_id='a', name='a', thumbnail='https://a.com', total_duration=114, duration=114, url='https://www.a.com', source='youtube', room=self.r, ) m.save() expected_serialization = { 'pk': m.pk, 'music_id': 'a', 'name': 'a', 'thumbnail': 'https://a.com', 'total_duration': 114, 'duration': 114, 'url': 'https://www.a.com', 'source': 'youtube', 'timer_start': 0, 'count': 0, 'last_play': None, 'one_shot': False } dict(MusicSerializer(m).data).should.eql(expected_serialization)
class RoomSerializer(serializers.ModelSerializer): """ Serializing one Room """ # Get more info about room count_left = serializers.IntegerField(source='get_count_remaining', read_only=True) time_left = serializers.IntegerField(source='get_remaining_time', read_only=True) token = serializers.CharField(max_length=64, read_only=True) current_time_past = serializers.IntegerField( source='get_current_time_past', read_only=True) current_time_past_percent = serializers.IntegerField( source='get_current_time_past_percent', read_only=True) current_time_left = serializers.IntegerField( source='get_current_remaining_time', read_only=True) playlist = serializers.ListField(source='playlist.all', child=PlaylistSerializer(), read_only=True) current_music = MusicSerializer(read_only=True) # Validators for post method password = serializers.CharField(max_length=128, write_only=True, required=True) name = serializers.CharField(max_length=64, required=True) can_adjust_volume = serializers.BooleanField(default=False) shuffle = serializers.BooleanField(default=False) class Meta: model = Room fields = ('name', 'current_music', 'shuffle', 'can_adjust_volume', 'count_left', 'time_left', 'current_time_left', 'current_time_past', 'current_time_past_percent', 'playlist', 'token', 'password', 'volume', 'listeners')
def get(self, request, artist_terms = None, format = None): music = MusicPost.objects.filter(artist_terms = artist_terms).order_by('artist_name').values("artist_id", "artist_name", "artist_info").distinct() if (len(music)==0): raise Http404 title = "List of artists of "+ artist_terms.replace("_", " ") + " genre" serializer = MusicSerializer(music, many=True) return Response({'object_list': serializer.data, 'title': title })
def get(self, request, number = None, song_year= None, format = None): if (int(number) == 0): raise Http404 if (number == None or song_year == None): raise Http404 elif(number != None and song_year != None): music = MusicPost.objects.filter(song_year = song_year).order_by('artist_hotttnesss').reverse().values('artist_id', 'artist_name', 'artist_info', 'song_id').distinct() title = "List of songs by popularity in "+song_year serializer = MusicSerializer(music, many=True) return Response({'object_list': serializer.data, 'title': title, 'song_year': song_year})
def patch(self, request, room, pk, format=None): """ Update music --- serializer: MusicSerializer """ try: music = Music.objects.get(pk=pk, room=room) except Music.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = MusicSerializer(music, data=request.data, partial=True) if serializer.is_valid(): serializer.save() message = { 'action': 'music_patched', 'music': serializer.data, } room.send_message(message) return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): music = MusicPost.objects.order_by('song_year').reverse().values( 'song_year').distinct() title = "List of years" serializer = MusicSerializer(music, many=True) return Response({ 'object_list': serializer.data, 'link': "/juceer/0.1/artists/years/", 'title': title, 'song_year': song_year })
def get(self, request, artist_id = None, song_year = None, format = None): #music = MusicPost.objects.all() if (artist_id == None or song_year == None): music = MusicPost.objects.order_by('song_hotttnesss').reverse().all() title = "List of songs" elif(artist_id != None and song_year != None): music = MusicPost.objects.filter(artist_id = artist_id, song_year = song_year).all() if(len(music)==0): raise Http404 title = "List of songs of "+ music[0].artist_name + " in " + song_year serializer = MusicSerializer(music, many=True) return Response({'object_list': serializer.data, 'title': title })
def get(self, request, artist_id, format = None): #music = MusicPost.objects.get(artist_id = artist_id) music = MusicPost.objects.filter(artist_id = artist_id).values("artist_hotttnesss", "artist_id", "artist_name", "artist_terms").all() if (len(music)==0): raise Http404 serializer = MusicSerializer(music[0]) context = { "object": serializer.data, "songs_of_the_artist": 'http://127.0.0.1:8000/juceer/0.1/artists/' + str(artist_id) + '/songs/', "statistic_of_the_artist": 'http://127.0.0.1:8000/juceer/0.1/statistics/mean/artists/' + str(artist_id) + '/', } return Response(context)
class RoomsSerializer(serializers.ModelSerializer): """ Serializing all the Rooms """ # Get more info about room count_left = serializers.IntegerField(source='get_count_remaining', read_only=True) time_left = serializers.IntegerField(source='get_remaining_time', read_only=True) current_time_left = serializers.IntegerField( source='get_current_remaining_time', read_only=True) playlist = serializers.ListField(source='get_musics_remaining', child=MusicSerializer(), read_only=True) current_music = MusicSerializer(read_only=True) class Meta: model = Room fields = ('name', 'current_music', 'shuffle', 'can_adjust_volume', 'count_left', 'time_left', 'current_time_left', 'playlist', 'volume', 'listeners')
def get(self, request, song_id, format = None): #music = MusicPost.objects.get(artist_id = artist_id) music = MusicPost.objects.filter(song_id = song_id).all() if (len(music)==0): raise Http404 serializer = MusicSerializer(music[0]) context = { "object": serializer.data, "songs_of_the_same_year": 'http://127.0.0.1:8000/juceer/0.1/years/'+ str(music[0].song_year) + '/', "songs_of_the_same_artist": 'http://127.0.0.1:8000/juceer/0.1/artists/' + str(music[0].artist_id) + '/songs/', "artists_of_the_same_ganre": 'http://127.0.0.1:8000/juceer/0.1/genres/' + str(music[0].artist_terms) + '/artists/', "google_artist": 'https://www.google.com/?q=' + music[0].artist_name + '/', } return Response(context)
def get(self, request): query = request.GET.get('q', None) user = None context = {"query": query} title = 'You searched for '+ query if query: SearchQuery.objects.create(user = user, query = query) s_list = MusicPost.objects.filter(artist_name__icontains = str(query)).all()|MusicPost.objects.filter(artist_terms__icontains = str(query)).all()|MusicPost.objects.filter(song_year__icontains = str(query)) s_list = s_list.order_by('artist_name').all() context["s_list"] = s_list else: title = "You search is empty" s_list = [] serializer = MusicSerializer(s_list, many = True) return Response({'object_list': serializer.data , "title": title})
def delete(self, request, room, pk, format=None): """ Delete a music --- """ try: music_to_delete = Music.objects.get(pk=pk, room=room) except Music.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) # We can't delete the current_music (SQL...), then skip the music before deletion if music_to_delete == room.current_music: room.play_next() message = { 'action': 'music_deleted', 'music': MusicSerializer(music_to_delete).data } music_to_delete.delete() room.send_message(message) return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, *args, **kwargs): musics = Music.objects.all() serializer = MusicSerializer(musics, many=True) return Response(serializer.data)
from datetime import datetime from django.utils import timezone from django.utils.six import BytesIO from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from music.models import Music from music.serializers import MusicSerializer musicdatetime = timezone.make_aware(datetime.now(), timezone.get_current_timezone()) # music = Music(name='dh', release_date=musicdatetime, music_category='hip-hop', played=False) # music.save() # # musicV2 = Music(name='tiger', release_date=musicdatetime, music_category='k-pop', played=False) # musicV2.save() musicV3 = Music(name='max', release_date=musicdatetime, music_category='ballad', played=False) musicV3.save() music3Serializer = MusicSerializer(musicV3) print(music3Serializer.data) renderer = JSONRenderer() rendered_data1 = renderer.render(music3Serializer)
def post(self, request, format = None): serializer = MusicSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def add_music(self, request, queryset): for music in queryset: # Hack to transform music into dict music.room.add_music(**MusicSerializer(music).data) return
def get(self, request, format = None): music = MusicPost.objects.order_by('song_year').reverse().values('song_year').distinct() title = "List of songs by years" serializer = MusicSerializer(music, many=True) return Response({'object_list': serializer.data, 'title': title})
def get(self, request, format = None): music = MusicPost.objects.order_by('artist_name').values('artist_id', 'artist_name', 'artist_info').distinct() title = "List of artists" serializer = MusicSerializer(music, many=True) return Response({'object_list': serializer.data, 'title': title})