diff gpp/core/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 2d299909e074
children be3fff614b93
line wrap: on
line diff
--- a/gpp/core/views.py	Sun Dec 13 21:57:34 2009 +0000
+++ b/gpp/core/views.py	Mon Dec 14 05:07:28 2009 +0000
@@ -2,6 +2,10 @@
 Views for the core application. These are mainly shared, common views
 used by multiple applications.
 """
+from django.contrib.auth.models import User
+from django.http import HttpResponse
+from django.http import HttpResponseBadRequest
+from django.http import HttpResponseForbidden
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.contrib.auth.decorators import login_required
@@ -15,3 +19,24 @@
     to be called via AJAX.
     """
     return render_to_response('core/markdown_help.html')
+
+
+def ajax_users(request):
+    """
+    If the user is authenticated, return a string of usernames whose names start with
+    the 'q' GET parameter, limited by the 'limit' GET parameters. The names are separated
+    by newlines. Only active usernames are returned.
+    If the user is not authenticated, return an empty string.
+    """
+    q = request.GET.get('q', None)
+    if q is None:
+        return HttpResponseBadRequest()
+
+    if request.user.is_authenticated():
+        q = request.GET.get('q', ' ')
+        limit = int(request.GET.get('limit', 10))
+        users = User.objects.filter(is_active=True, 
+                username__istartswith=q).values_list('username', flat=True)[:limit]
+        user_list = u"\n".join(users)
+        return HttpResponse(user_list)
+    return HttpResponseForbidden()