view bandmap/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 09ed84a7394c
children bb7e2fc24690
line wrap: on
line source
"""Views for the bandmap application.

"""
import json

from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render
from django.contrib import messages as django_messages
from django.http import HttpResponse
from django.core.cache import cache
from django.conf import settings

from bandmap.forms import BandForm
from bandmap.models import BandEntry


SUCCESS = u"Successfully submitted {} for admin review. Thanks!"


def map_view(request):
    return render(request, 'bandmap/map.html')


@login_required
def add_band(request):
    """
    Provides the ability for a user to submit a new band to the map.

    """
    if request.method == 'POST':
        form = BandForm(request.POST)
        if form.is_valid():
            band = form.save(commit=False)
            band.user = request.user
            band.save()
            django_messages.success(request, SUCCESS.format(band.name))
            return redirect('bandmap-add')
    else:
        form = BandForm()

    return render(request, 'bandmap/add.html', {
        'form': form,
        })


def query(request):
    """Retrieves the band map entries and returns them as JSON.

    """
    show_param = request.GET.get('show', 'all')
    if show_param not in ['all', 'active', 'inactive']:
        show_param = 'all'

    # Do we have the results cached already?
    cache_key = 'bandmap-results-{}'.format(show_param)
    results = cache.get(cache_key)
    if results and not settings.DEBUG:
        return HttpResponse(results, content_type='application/json')

    # Generate results
    qs = BandEntry.objects.filter(is_approved=True)
    if show_param == 'active':
        qs = qs.filter(is_active=True)
    elif show_param == 'inactive':
        qs = qs.filter(is_active=False)

    bands = []
    for band in qs.iterator():
        bands.append({
            'name': band.name,
            'lat': band.lat,
            'lon': band.lon,
            'is_active': band.is_active,
            'note': band.html,
        })
    results = json.dumps(bands)

    # Store in cache
    cache.set(cache_key, results, 600)

    return HttpResponse(results, content_type='application/json')