view core/views.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents 89b240fe9297
children e932f2ecd4a7
line wrap: on
line source
"""
Views for the core application. These are mainly shared, common views
used by multiple applications.

"""
import json

from django.contrib.auth.models import User
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_GET
from django.views.generic import TemplateView


@login_required
@require_GET
def markdown_help(request):
    """
    This view provides the Markdown help cheat sheet. It is expected
    to be called via AJAX.

    """
    return render_to_response('core/markdown_help.html')


def ajax_users(request):
    """
    If the user is authenticated, return a JSON array of strings of usernames
    whose names start with the 'q' GET parameter, limited by the 'limit' GET
    parameter. Only active usernames are returned.
    If the user is not authenticated, return an empty array.

    """
    q = request.GET.get('q', None)
    if q is None or not request.user.is_authenticated():
        return HttpResponse(json.dumps([]), content_type='application/json')

    limit = int(request.GET.get('limit', 10))
    users = User.objects.filter(is_active=True,
            username__istartswith=q).values_list('username', flat=True)[:limit]
    return HttpResponse(json.dumps(list(users)), content_type='application/json')


class FixedView(TemplateView):
    """
    For displaying our "fixed" views generated with the custom command
    make_fixed_page.

    """
    template_name = 'fixed/base.html'
    title = ''
    content_template = ''

    def get_context_data(self, **kwargs):
        context = super(FixedView, self).get_context_data(**kwargs)
        context['title'] = self.title
        context['content_template'] = self.content_template
        return context