comparison core/views.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/core/views.py@be3fff614b93
children 1982996ce365
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Views for the core application. These are mainly shared, common views
3 used by multiple applications.
4 """
5 from django.contrib.auth.models import User
6 from django.http import HttpResponse
7 from django.shortcuts import render_to_response
8 from django.template import RequestContext
9 from django.contrib.auth.decorators import login_required
10 from django.views.decorators.http import require_GET
11 import django.utils.simplejson as json
12
13
14 @login_required
15 @require_GET
16 def markdown_help(request):
17 """
18 This view provides the Markdown help cheat sheet. It is expected
19 to be called via AJAX.
20 """
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 JSON array of strings of usernames
27 whose names start with the 'q' GET parameter, limited by the 'limit' GET
28 parameter. Only active usernames are returned.
29 If the user is not authenticated, return an empty array.
30 """
31 q = request.GET.get('q', None)
32 if q is None or not request.user.is_authenticated():
33 return HttpResponse(json.dumps([]), content_type='application/json')
34
35 limit = int(request.GET.get('limit', 10))
36 users = User.objects.filter(is_active=True,
37 username__istartswith=q).values_list('username', flat=True)[:limit]
38 return HttpResponse(json.dumps(list(users)), content_type='application/json')