bgneal@127: """
bgneal@127: Views for the core application. These are mainly shared, common views
bgneal@127: used by multiple applications.
bgneal@127: """
bgneal@149: from django.contrib.auth.models import User
bgneal@149: from django.http import HttpResponse
bgneal@127: from django.shortcuts import render_to_response
bgneal@127: from django.template import RequestContext
bgneal@127: from django.contrib.auth.decorators import login_required
bgneal@127: from django.views.decorators.http import require_GET
bgneal@186: import django.utils.simplejson as json
bgneal@186: 
bgneal@127: 
bgneal@127: @login_required
bgneal@127: @require_GET
bgneal@127: def markdown_help(request):
bgneal@127:     """
bgneal@127:     This view provides the Markdown help cheat sheet. It is expected
bgneal@127:     to be called via AJAX.
bgneal@127:     """
bgneal@127:     return render_to_response('core/markdown_help.html')
bgneal@149: 
bgneal@149: 
bgneal@149: def ajax_users(request):
bgneal@149:     """
bgneal@186:     If the user is authenticated, return a JSON array of strings of usernames 
bgneal@186:     whose names start with the 'q' GET parameter, limited by the 'limit' GET 
bgneal@186:     parameter. Only active usernames are returned.
bgneal@186:     If the user is not authenticated, return an empty array.
bgneal@149:     """
bgneal@149:     q = request.GET.get('q', None)
bgneal@186:     if q is None or not request.user.is_authenticated():
bgneal@186:         return HttpResponse(json.dumps([]), content_type='application/json')
bgneal@149: 
bgneal@186:     limit = int(request.GET.get('limit', 10))
bgneal@186:     users = User.objects.filter(is_active=True, 
bgneal@186:             username__istartswith=q).values_list('username', flat=True)[:limit]
bgneal@186:     return HttpResponse(json.dumps(list(users)), content_type='application/json')