Beispiel #1
0
def get(request):
    if request.is_ajax() or True:
        (page, perpage, term, user_id) = _get_defaults(request.GET)
        offset = (int(page) - 1) * perpage
        if offset < 0:
            offset = 0

        try:
            user_id = int(user_id)
        except ValueError:
            user = request.user
        else:
            try:
                user = User.objects.get(pk=user_id)
            except User.DoesNotExist:
                user = request.user

        friendships1 = Friends.objects.filter(Q(user=user)).filter(accepted=True).values_list("friend")
        friendships2 = Friends.objects.filter(Q(friend=user)).filter(accepted=True).values_list("user")

        friends = User.objects.filter(pk__in=(x[0] for x in itertools.chain(friendships1, friendships2)))
        friends = util.get_matching_users(friends, term)

        count = len(friends)
        friends = friends[offset : offset + perpage]
        return render_to_response(
            "friends/display.html",
            {"friends": friends, "has_next": count > offset + perpage, "has_prev": offset > 0, "js": ("friends.js",)},
            context_instance=RequestContext(request),
        )
Beispiel #2
0
def users(request):
  """
  Ajax Request to search for user after first, last name and username
  """
  if 'term' not in request.GET:
    raise Http404
  
  if request.is_ajax() or True:
    users_list = []

    prof.start('profile-get-users-ajax')
    users = util.get_matching_users(User.objects, request.GET['term'])
    for user in users:
      if user.first_name != '' or user.last_name != '':
        name = user.first_name + ' ' + user.last_name + ' (' + user.username + ')'
      else:
        name = user.username
      users_list.append({'id': user.id,
                        'label': name,
                        'value': name})
    prof.stop('profile-get-users-ajax')
    return HttpResponse(json.dumps(users_list), mimetype='application/javascript')