comparison gpp/core/views.py @ 186:be3fff614b93

Implement #66; use jQuery UI autocomplete widget to replace obsolete jquery-autocomplete plugin. I implemented a very simple caching system.
author Brian Neal <bgneal@gmail.com>
date Tue, 30 Mar 2010 01:30:32 +0000
parents ab7830b067b3
children
comparison
equal deleted inserted replaced
185:afb65fa947f1 186:be3fff614b93
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 5 from django.contrib.auth.models import User
6 from django.http import HttpResponse 6 from django.http import HttpResponse
7 from django.http import HttpResponseBadRequest
8 from django.http import HttpResponseForbidden
9 from django.shortcuts import render_to_response 7 from django.shortcuts import render_to_response
10 from django.template import RequestContext 8 from django.template import RequestContext
11 from django.contrib.auth.decorators import login_required 9 from django.contrib.auth.decorators import login_required
12 from django.views.decorators.http import require_GET 10 from django.views.decorators.http import require_GET
11 import django.utils.simplejson as json
12
13 13
14 @login_required 14 @login_required
15 @require_GET 15 @require_GET
16 def markdown_help(request): 16 def markdown_help(request):
17 """ 17 """
21 return render_to_response('core/markdown_help.html') 21 return render_to_response('core/markdown_help.html')
22 22
23 23
24 def ajax_users(request): 24 def ajax_users(request):
25 """ 25 """
26 If the user is authenticated, return a string of usernames whose names start with 26 If the user is authenticated, return a JSON array of strings of usernames
27 the 'q' GET parameter, limited by the 'limit' GET parameters. The names are separated 27 whose names start with the 'q' GET parameter, limited by the 'limit' GET
28 by newlines. Only active usernames are returned. 28 parameter. Only active usernames are returned.
29 If the user is not authenticated, return an empty string. 29 If the user is not authenticated, return an empty array.
30 """ 30 """
31 q = request.GET.get('q', None) 31 q = request.GET.get('q', None)
32 if q is None: 32 if q is None or not request.user.is_authenticated():
33 return HttpResponseBadRequest() 33 return HttpResponse(json.dumps([]), content_type='application/json')
34 34
35 if request.user.is_authenticated(): 35 limit = int(request.GET.get('limit', 10))
36 q = request.GET.get('q', ' ') 36 users = User.objects.filter(is_active=True,
37 limit = int(request.GET.get('limit', 10)) 37 username__istartswith=q).values_list('username', flat=True)[:limit]
38 users = User.objects.filter(is_active=True, 38 return HttpResponse(json.dumps(list(users)), content_type='application/json')
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()