Beispiel #1
0
def user_search_autocomplete(request, qname='query', qs=User.objects.all()):
    if not request.GET.get(qname, False):
        return return_json()

    q = request.GET[qname]

    response = {
        'query': request.GET[qname].encode('utf-8'),
        'suggestions': [],
        'data': [],
    }

    qs = qs.filter(
        Q(username__icontains=q) |
        Q(first_name__icontains=q) |
        Q(last_name__icontains=q)
    ).select_related('playlistprofile').distinct()

    for user in qs[:15]:
        response['suggestions'].append(unicode(user.playlistprofile))
        response['data'].append({
            'url': user.playlistprofile.get_absolute_url(),
            'html': '<img src="%s"> %s' % (
                user.playlistprofile.avatar_url,
                unicode(user.playlistprofile),
            )
        })

    return return_json(response)
Beispiel #2
0
def friends_search_autocomplete(request, qname='query'):
    if not request.user.is_authenticated():
        return http.HttpResponseForbidden()

    if not request.GET.get(qname, False):
        return return_json()

    q = request.GET[qname]
    qs = request.user.playlistprofile.friends()
    qs = qs.filter(Q(first_name__icontains=q) | Q(last_name__icontains=q))
    qs = qs.distinct('pk')
    print qs

    response = {
        'query': request.GET[qname].encode('utf-8'),
        'suggestions': [],
        'data': [],
    }

    for user in qs:
        response['suggestions'].append(unicode(user.playlistprofile))
        response['data'].append({
            'url': user.playlistprofile.get_absolute_url(),
            'html': '<img src="%s"> %s' % (
                user.playlistprofile.avatar_url,
                unicode(user.playlistprofile),
            ),
            'pk': user.pk,
        })

    return return_json(response)
Beispiel #3
0
def playlist_search_autocomplete(request, qname='query', qs=Playlist.objects.all()):
    if not request.GET.get(qname, False):
        return return_json()

    q = request.GET[qname]

    response = {
        'query': request.GET[qname].encode('utf-8'),
        'suggestions': [],
        'data': [],
    }
    
    playlist_search_url = urlresolvers.reverse('playlist_search')

    cursor = connection.cursor()
    sql = "select distinct name, tracks_count from playlist_playlist where name like %s and tracks_count >= 8 order by tracks_count desc limit 15"
    cursor.execute(sql, ['%%%s%%' % q])
    for row in cursor.fetchall():
        if row[0] in response['suggestions']:
            continue
        response['suggestions'].append(row[0])
        response['data'].append({
            'url': '%s?term=%s' % (playlist_search_url, row[0]),
            'html': '%s' % (
                row[0],
            )
        })

    if len(response['data']) < 15:
        cursor = connection.cursor()
        sql = "select distinct name, tracks_count from playlist_playlist where tags like %s and name not like %s and tracks_count >= 8 order by tracks_count desc limit " + unicode(15-len(response['data']))
        cursor.execute(sql, ['%%%s%%' % q,'%%%s%%' % q])
        for row in cursor.fetchall():
            if row[0] in response['suggestions']:
                continue
            response['suggestions'].append(row[0])
            response['data'].append({
                'url': '%s?term=%s' % (playlist_search_url, row[0]),
                'html': '%s' % (
                    row[0],
                )
            })

    return return_json(response)