annotate core/views.py @ 1130:f1bc1518105b

Add search to site menu.
author Brian Neal <bgneal@gmail.com>
date Wed, 14 Sep 2016 20:34:21 -0500
parents e932f2ecd4a7
children a469445d8be3
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@679 4
bgneal@127 5 """
bgneal@679 6 import json
bgneal@679 7
bgneal@149 8 from django.contrib.auth.models import User
bgneal@149 9 from django.http import HttpResponse
bgneal@1032 10 from django.shortcuts import render
bgneal@127 11 from django.contrib.auth.decorators import login_required
bgneal@127 12 from django.views.decorators.http import require_GET
bgneal@591 13 from django.views.generic import TemplateView
bgneal@186 14
bgneal@127 15
bgneal@127 16 @login_required
bgneal@127 17 @require_GET
bgneal@127 18 def markdown_help(request):
bgneal@127 19 """
bgneal@127 20 This view provides the Markdown help cheat sheet. It is expected
bgneal@127 21 to be called via AJAX.
bgneal@679 22
bgneal@127 23 """
bgneal@1032 24 return render(request, 'core/markdown_help.html')
bgneal@149 25
bgneal@149 26
bgneal@149 27 def ajax_users(request):
bgneal@149 28 """
bgneal@679 29 If the user is authenticated, return a JSON array of strings of usernames
bgneal@679 30 whose names start with the 'q' GET parameter, limited by the 'limit' GET
bgneal@186 31 parameter. Only active usernames are returned.
bgneal@186 32 If the user is not authenticated, return an empty array.
bgneal@679 33
bgneal@149 34 """
bgneal@149 35 q = request.GET.get('q', None)
bgneal@186 36 if q is None or not request.user.is_authenticated():
bgneal@186 37 return HttpResponse(json.dumps([]), content_type='application/json')
bgneal@149 38
bgneal@186 39 limit = int(request.GET.get('limit', 10))
bgneal@679 40 users = User.objects.filter(is_active=True,
bgneal@186 41 username__istartswith=q).values_list('username', flat=True)[:limit]
bgneal@186 42 return HttpResponse(json.dumps(list(users)), content_type='application/json')
bgneal@591 43
bgneal@591 44
bgneal@591 45 class FixedView(TemplateView):
bgneal@591 46 """
bgneal@591 47 For displaying our "fixed" views generated with the custom command
bgneal@591 48 make_fixed_page.
bgneal@591 49
bgneal@591 50 """
bgneal@591 51 template_name = 'fixed/base.html'
bgneal@591 52 title = ''
bgneal@591 53 content_template = ''
bgneal@591 54
bgneal@591 55 def get_context_data(self, **kwargs):
bgneal@591 56 context = super(FixedView, self).get_context_data(**kwargs)
bgneal@591 57 context['title'] = self.title
bgneal@591 58 context['content_template'] = self.content_template
bgneal@591 59 return context