Exemplo n.º 1
0
def ajax_search_artist_letter(request):
    """
    Searches the database for artists starting with a letter
    """
    search = post_var(request, 'search').lower()
    # filter every appropriate column
    search = search + '%'
    # yeah we have to do this because ordering by lower doesnt work across
    # tables
    songs = Song.objects.raw(
        "SELECT sng.id AS id, \
                sng.tracknumber AS tracknumber, \
                sng.title AS title \
            FROM player_song sng \
	        LEFT JOIN player_album alb \
		        ON sng.album_id = alb.id \
	        LEFT JOIN player_artist art \
		        ON sng.artist_id = art.id \
	        LEFT JOIN player_genre gr \
		        ON sng.genre_id = gr.id \
	        WHERE \
		        LOWER(art.name) LIKE %s \
	        ORDER BY \
		        LOWER(art.name), LOWER(alb.name), sng.tracknumber",
		[search, ]
    )

    ctx = {
        'songs': songs,
    }
    return render(request, 'ajax/search.html', ctx)
Exemplo n.º 2
0
def ajax_search_advanced(request):
    """
    Searches the database for a complex query
    """
    artist = '%' + post_var(request, 'artist').lower() + '%'
    title = '%' + post_var(request, 'title').lower() + '%'
    genre = '%' + post_var(request, 'genre').lower() + '%'
    album = '%' + post_var(request, 'album').lower() + '%'
    
    # yeah we have to do this because ordering by lower doesnt work across
    # tables
    songs = Song.objects.raw(
        "SELECT sng.id AS id, \
                sng.tracknumber AS tracknumber, \
                sng.title AS title \
            FROM player_song sng \
	        LEFT JOIN player_album alb \
		        ON sng.album_id = alb.id \
	        LEFT JOIN player_artist art \
		        ON sng.artist_id = art.id \
	        LEFT JOIN player_genre gr \
		        ON sng.genre_id = gr.id \
	        WHERE \
		        LOWER(art.name) LIKE %s \
		        AND \
		        LOWER(alb.name) LIKE %s \
		        AND \
		        LOWER(gr.name) LIKE %s \
		        AND \
		        LOWER(sng.title) LIKE %s \
	        ORDER BY \
		        LOWER(art.name), LOWER(alb.name), sng.tracknumber",
		[artist, album, genre, title]
    )

    ctx = {
        'songs': songs,
    }
    return render(request, 'ajax/search.html', ctx)