Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
148:35a0e6345815 | 149:ab7830b067b3 |
---|---|
1 """ | 1 """ |
2 Views for the core application. These are mainly shared, common views | 2 Views for the core application. These are mainly shared, common views |
3 used by multiple applications. | 3 used by multiple applications. |
4 """ | 4 """ |
5 from django.contrib.auth.models import User | |
6 from django.http import HttpResponse | |
7 from django.http import HttpResponseBadRequest | |
8 from django.http import HttpResponseForbidden | |
5 from django.shortcuts import render_to_response | 9 from django.shortcuts import render_to_response |
6 from django.template import RequestContext | 10 from django.template import RequestContext |
7 from django.contrib.auth.decorators import login_required | 11 from django.contrib.auth.decorators import login_required |
8 from django.views.decorators.http import require_GET | 12 from django.views.decorators.http import require_GET |
9 | 13 |
13 """ | 17 """ |
14 This view provides the Markdown help cheat sheet. It is expected | 18 This view provides the Markdown help cheat sheet. It is expected |
15 to be called via AJAX. | 19 to be called via AJAX. |
16 """ | 20 """ |
17 return render_to_response('core/markdown_help.html') | 21 return render_to_response('core/markdown_help.html') |
22 | |
23 | |
24 def ajax_users(request): | |
25 """ | |
26 If the user is authenticated, return a string of usernames whose names start with | |
27 the 'q' GET parameter, limited by the 'limit' GET parameters. The names are separated | |
28 by newlines. Only active usernames are returned. | |
29 If the user is not authenticated, return an empty string. | |
30 """ | |
31 q = request.GET.get('q', None) | |
32 if q is None: | |
33 return HttpResponseBadRequest() | |
34 | |
35 if request.user.is_authenticated(): | |
36 q = request.GET.get('q', ' ') | |
37 limit = int(request.GET.get('limit', 10)) | |
38 users = User.objects.filter(is_active=True, | |
39 username__istartswith=q).values_list('username', flat=True)[:limit] | |
40 user_list = u"\n".join(users) | |
41 return HttpResponse(user_list) | |
42 return HttpResponseForbidden() |