comparison core/views.py @ 679:89b240fe9297

For Django 1.5.2: import json; django.utils.simplejson is deprecated.
author Brian Neal <bgneal@gmail.com>
date Thu, 15 Aug 2013 20:14:33 -0500
parents 1982996ce365
children e932f2ecd4a7
comparison
equal deleted inserted replaced
678:38a198ea8c61 679:89b240fe9297
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 """
6 import json
7
5 from django.contrib.auth.models import User 8 from django.contrib.auth.models import User
6 from django.http import HttpResponse 9 from django.http import HttpResponse
7 from django.shortcuts import render_to_response 10 from django.shortcuts import render_to_response
8 from django.contrib.auth.decorators import login_required 11 from django.contrib.auth.decorators import login_required
9 from django.views.decorators.http import require_GET 12 from django.views.decorators.http import require_GET
10 from django.views.generic import TemplateView 13 from django.views.generic import TemplateView
11 import django.utils.simplejson as json
12 14
13 15
14 @login_required 16 @login_required
15 @require_GET 17 @require_GET
16 def markdown_help(request): 18 def markdown_help(request):
17 """ 19 """
18 This view provides the Markdown help cheat sheet. It is expected 20 This view provides the Markdown help cheat sheet. It is expected
19 to be called via AJAX. 21 to be called via AJAX.
22
20 """ 23 """
21 return render_to_response('core/markdown_help.html') 24 return render_to_response('core/markdown_help.html')
22 25
23 26
24 def ajax_users(request): 27 def ajax_users(request):
25 """ 28 """
26 If the user is authenticated, return a JSON array of strings of usernames 29 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 30 whose names start with the 'q' GET parameter, limited by the 'limit' GET
28 parameter. Only active usernames are returned. 31 parameter. Only active usernames are returned.
29 If the user is not authenticated, return an empty array. 32 If the user is not authenticated, return an empty array.
33
30 """ 34 """
31 q = request.GET.get('q', None) 35 q = request.GET.get('q', None)
32 if q is None or not request.user.is_authenticated(): 36 if q is None or not request.user.is_authenticated():
33 return HttpResponse(json.dumps([]), content_type='application/json') 37 return HttpResponse(json.dumps([]), content_type='application/json')
34 38
35 limit = int(request.GET.get('limit', 10)) 39 limit = int(request.GET.get('limit', 10))
36 users = User.objects.filter(is_active=True, 40 users = User.objects.filter(is_active=True,
37 username__istartswith=q).values_list('username', flat=True)[:limit] 41 username__istartswith=q).values_list('username', flat=True)[:limit]
38 return HttpResponse(json.dumps(list(users)), content_type='application/json') 42 return HttpResponse(json.dumps(list(users)), content_type='application/json')
39 43
40 44
41 class FixedView(TemplateView): 45 class FixedView(TemplateView):