annotate core/views.py @ 605:a7968613c469

Another tweak to fabfile because of Django 1.4 directory structure changes.
author Brian Neal <bgneal@gmail.com>
date Mon, 23 Jul 2012 19:53:36 -0500
parents 1982996ce365
children 89b240fe9297
rev   line source
bgneal@127 1 """
bgneal@127 2 Views for the core application. These are mainly shared, common views
bgneal@127 3 used by multiple applications.
bgneal@127 4 """
bgneal@149 5 from django.contrib.auth.models import User
bgneal@149 6 from django.http import HttpResponse
bgneal@127 7 from django.shortcuts import render_to_response
bgneal@127 8 from django.contrib.auth.decorators import login_required
bgneal@127 9 from django.views.decorators.http import require_GET
bgneal@591 10 from django.views.generic import TemplateView
bgneal@186 11 import django.utils.simplejson as json
bgneal@186 12
bgneal@127 13
bgneal@127 14 @login_required
bgneal@127 15 @require_GET
bgneal@127 16 def markdown_help(request):
bgneal@127 17 """
bgneal@127 18 This view provides the Markdown help cheat sheet. It is expected
bgneal@127 19 to be called via AJAX.
bgneal@127 20 """
bgneal@127 21 return render_to_response('core/markdown_help.html')
bgneal@149 22
bgneal@149 23
bgneal@149 24 def ajax_users(request):
bgneal@149 25 """
bgneal@186 26 If the user is authenticated, return a JSON array of strings of usernames
bgneal@186 27 whose names start with the 'q' GET parameter, limited by the 'limit' GET
bgneal@186 28 parameter. Only active usernames are returned.
bgneal@186 29 If the user is not authenticated, return an empty array.
bgneal@149 30 """
bgneal@149 31 q = request.GET.get('q', None)
bgneal@186 32 if q is None or not request.user.is_authenticated():
bgneal@186 33 return HttpResponse(json.dumps([]), content_type='application/json')
bgneal@149 34
bgneal@186 35 limit = int(request.GET.get('limit', 10))
bgneal@186 36 users = User.objects.filter(is_active=True,
bgneal@186 37 username__istartswith=q).values_list('username', flat=True)[:limit]
bgneal@186 38 return HttpResponse(json.dumps(list(users)), content_type='application/json')
bgneal@591 39
bgneal@591 40
bgneal@591 41 class FixedView(TemplateView):
bgneal@591 42 """
bgneal@591 43 For displaying our "fixed" views generated with the custom command
bgneal@591 44 make_fixed_page.
bgneal@591 45
bgneal@591 46 """
bgneal@591 47 template_name = 'fixed/base.html'
bgneal@591 48 title = ''
bgneal@591 49 content_template = ''
bgneal@591 50
bgneal@591 51 def get_context_data(self, **kwargs):
bgneal@591 52 context = super(FixedView, self).get_context_data(**kwargs)
bgneal@591 53 context['title'] = self.title
bgneal@591 54 context['content_template'] = self.content_template
bgneal@591 55 return context