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.contrib.auth.decorators import login_required bgneal@127: from django.views.decorators.http import require_GET bgneal@591: from django.views.generic import TemplateView 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') bgneal@591: bgneal@591: bgneal@591: class FixedView(TemplateView): bgneal@591: """ bgneal@591: For displaying our "fixed" views generated with the custom command bgneal@591: make_fixed_page. bgneal@591: bgneal@591: """ bgneal@591: template_name = 'fixed/base.html' bgneal@591: title = '' bgneal@591: content_template = '' bgneal@591: bgneal@591: def get_context_data(self, **kwargs): bgneal@591: context = super(FixedView, self).get_context_data(**kwargs) bgneal@591: context['title'] = self.title bgneal@591: context['content_template'] = self.content_template bgneal@591: return context