コード例 #1
0
ファイル: list.py プロジェクト: skratz17/rate-my-music-server
    def list(self, request):
        """GET all lists"""
        lists = List.objects.all()

        song_id = request.query_params.get('songId', None)
        user_id = request.query_params.get('userId', None)
        favoritedBy = request.query_params.get('favoritedBy', None)
        page = request.query_params.get('page', None)
        pageSize = request.query_params.get('pageSize', 10)

        if song_id is not None:
            lists = lists.filter(songs__song_id=song_id).distinct()

        if user_id is not None:
            lists = lists.filter(creator_id=user_id)

        if favoritedBy is not None:
            lists = lists.filter(favorites__rater_id=favoritedBy)

        count = len(lists)

        if page is not None:
            lists = paginate(lists, page, pageSize)

        serializer = SimpleListSerializer(lists, many=True)
        return Response({"data": serializer.data, "count": count})
コード例 #2
0
    def list(self, request):
        """GET all ratings"""
        ratings = Rating.objects.all()

        user_id = request.query_params.get('userId', None)
        song_id = request.query_params.get('songId', None)
        page = request.query_params.get('page', None)
        pageSize = request.query_params.get('pageSize', 10)

        if user_id is not None:
            ratings = ratings.filter(rater_id=user_id)
        
        if song_id is not None:
            ratings = ratings.filter(song_id=song_id)

        ratings = self._sort_by_query_string_param(ratings)

        count = len(ratings)

        if page is not None:
            ratings = paginate(ratings, page, pageSize)

        serializer = RatingSerializer(ratings, many=True)
        return Response({
            "data": serializer.data,
            "count": count
        })
コード例 #3
0
ファイル: song.py プロジェクト: skratz17/rate-my-music-server
    def list(self, request):
        """GET all songs with optional query string parameters:
            startYear - start year of songs to return results for
            endYear - end year of songs to return results for
            genres - comma-separated list of genre ids to return results for
            artist - id of artist to return results for
            q - search term to search song or artist names
            orderBy - field (one of: name, artist, year) to sort results by
            direction - direction to sort results in (one of: asc, desc)
        """
        songs = Song.objects.all()

        startYear = request.query_params.get('startYear', None)
        endYear = request.query_params.get('endYear', None)
        genres = request.query_params.get('genres', None)
        artist = request.query_params.get('artist', None)
        q = request.query_params.get('q', None)
        page = request.query_params.get('page', None)
        pageSize = request.query_params.get('pageSize', 10)

        if startYear is not None:
            try:
                startYear = int(startYear)
            except ValueError:
                startYear = 0
            songs = songs.filter(year__gte=startYear)
        
        if endYear is not None:
            try:
                endYear = int(endYear)
            except ValueError:
                endYear = 3000 
            songs = songs.filter(year__lte=endYear)
        
        if genres is not None:
            genres = genres.split(',')
            for genre in genres:
                songs = songs.filter(genres__genre_id=genre).distinct()

        if artist is not None:
            songs = songs.filter(artist_id=artist)

        if q is not None:
            songs = songs.filter(Q(artist__name__icontains=q) | Q(name__icontains=q))

        songs = self._sort_by_query_string_param(songs)

        count = len(songs)

        if page is not None:
            songs = paginate(songs, page, pageSize)

        serializer = SongSerializer(songs, many=True)
        return Response({
            "data": serializer.data,
            "count": count
        })
コード例 #4
0
    def list(self, request):
        genres = Genre.objects.all()

        q = request.query_params.get('q', None)
        page = request.query_params.get('page', None)
        pageSize = request.query_params.get('pageSize', 10)

        if q is not None:
            genres = self._filter_by_search_term(genres, q)

        count = len(genres)

        if page is not None:
            genres = paginate(genres, page, pageSize)

        serializer = GenreSerializer(genres, many=True)
        return Response({"data": serializer.data, "count": count})
コード例 #5
0
    def list(self, request):
        """GET all artists"""
        artists = Artist.objects.all()

        q = request.query_params.get('q', None)
        page = request.query_params.get('page', None)
        pageSize = request.query_params.get('pageSize', 10)

        if q is not None:
            artists = self._filter_by_search_term(artists, q)

        count = len(artists)

        if page is not None:
            artists = paginate(artists, page, pageSize)

        serializer = ArtistSerializer(artists, many=True)
        return Response({"data": serializer.data, "count": count})