comparison gpp/messages/views.py @ 149:ab7830b067b3

Implement ticket #40. Added a simple way to search for usernames and then view their profile. Moved this ajax username search feature out of the messages app and into core.
author Brian Neal <bgneal@gmail.com>
date Mon, 14 Dec 2009 05:07:28 +0000
parents dbd703f7d63a
children 7ad1f3e77cd9
comparison
equal deleted inserted replaced
148:35a0e6345815 149:ab7830b067b3
1 """Views for the messages application""" 1 """Views for the messages application"""
2 2
3 import datetime 3 import datetime
4 from django.shortcuts import render_to_response 4 from django.shortcuts import render_to_response
5 from django.template import RequestContext 5 from django.template import RequestContext
6 from django.contrib.auth.models import User
7 from django.http import HttpResponseRedirect 6 from django.http import HttpResponseRedirect
8 from django.http import HttpResponse
9 from django.http import HttpResponseBadRequest
10 from django.http import HttpResponseForbidden
11 from django.contrib.auth.decorators import login_required 7 from django.contrib.auth.decorators import login_required
12 from django.shortcuts import get_object_or_404 8 from django.shortcuts import get_object_or_404
13 from django.core.urlresolvers import reverse 9 from django.core.urlresolvers import reverse
14 from django.http import Http404 10 from django.http import Http404
15 11
283 return render_to_response('messages/options.html', { 279 return render_to_response('messages/options.html', {
284 'form': form, 280 'form': form,
285 }, 281 },
286 context_instance = RequestContext(request)) 282 context_instance = RequestContext(request))
287 283
288
289 def ajax_users(request):
290 """
291 If the user is authenticated, return a string of usernames whose names start with
292 the 'q' GET parameter, limited by the 'limit' GET parameters. The names are separated
293 by newlines.
294 If the user is not authenticated, return an empty string.
295 This is used by the auto-complete function in the compose form.
296 """
297 q = request.GET.get('q', None)
298 if q is None:
299 return HttpResponseBadRequest()
300
301 if request.user.is_authenticated():
302 q = request.GET.get('q', ' ')
303 limit = int(request.GET.get('limit', 10))
304 users = User.objects.filter(username__istartswith=q).values_list('username', flat=True)[:limit]
305 user_list = u"\n".join(users)
306 return HttpResponse(user_list)
307 return HttpResponseForbidden()
308
309
310 # vim: ts=4 sw=4