annotate bandmap/views.py @ 840:7735f8a6bd1f

Bandmap fix: Unicode band names were causing a crash.
author Brian Neal <bgneal@gmail.com>
date Sat, 04 Oct 2014 17:15:22 -0500
parents f31168465953
children 09ed84a7394c
rev   line source
bgneal@820 1 """Views for the bandmap application.
bgneal@820 2
bgneal@820 3 """
bgneal@827 4 import json
bgneal@827 5
bgneal@820 6 from django.contrib.auth.decorators import login_required
bgneal@820 7 from django.shortcuts import redirect, render
bgneal@821 8 from django.contrib import messages as django_messages
bgneal@827 9 from django.http import HttpResponse
bgneal@827 10 from django.template.loader import render_to_string
bgneal@827 11 from django.core.cache import cache
bgneal@838 12 from django.conf import settings
bgneal@820 13
bgneal@820 14 from bandmap.forms import BandForm
bgneal@827 15 from bandmap.models import BandEntry
bgneal@820 16
bgneal@820 17
bgneal@840 18 SUCCESS = u"Successfully submitted {} for admin review. Thanks!"
bgneal@821 19
bgneal@821 20
bgneal@820 21 def map_view(request):
bgneal@820 22 return render(request, 'bandmap/map.html')
bgneal@820 23
bgneal@820 24
bgneal@820 25 @login_required
bgneal@820 26 def add_band(request):
bgneal@820 27 """
bgneal@820 28 Provides the ability for a user to submit a new band to the map.
bgneal@820 29
bgneal@820 30 """
bgneal@820 31 if request.method == 'POST':
bgneal@820 32 form = BandForm(request.POST)
bgneal@820 33 if form.is_valid():
bgneal@820 34 band = form.save(commit=False)
bgneal@820 35 band.user = request.user
bgneal@820 36 band.save()
bgneal@821 37 django_messages.success(request, SUCCESS.format(band.name))
bgneal@825 38 return redirect('bandmap-add')
bgneal@820 39 else:
bgneal@820 40 form = BandForm()
bgneal@820 41
bgneal@820 42 return render(request, 'bandmap/add.html', {
bgneal@820 43 'form': form,
bgneal@820 44 })
bgneal@827 45
bgneal@827 46
bgneal@827 47 def query(request):
bgneal@827 48 """Retrieves the band map entries and returns them as JSON.
bgneal@827 49
bgneal@827 50 """
bgneal@827 51 show_param = request.GET.get('show', 'all')
bgneal@827 52 if show_param not in ['all', 'active', 'inactive']:
bgneal@827 53 show_param = 'all'
bgneal@827 54
bgneal@827 55 # Do we have the results cached already?
bgneal@827 56 cache_key = 'bandmap-results-{}'.format(show_param)
bgneal@827 57 results = cache.get(cache_key)
bgneal@838 58 if results and not settings.DEBUG:
bgneal@827 59 return HttpResponse(results, content_type='application/json')
bgneal@827 60
bgneal@827 61 # Generate results
bgneal@827 62 qs = BandEntry.objects.filter(is_approved=True)
bgneal@827 63 if show_param == 'active':
bgneal@827 64 qs = qs.filter(is_active=True)
bgneal@827 65 elif show_param == 'inactive':
bgneal@827 66 qs = qs.filter(is_active=False)
bgneal@827 67
bgneal@827 68 bands = []
bgneal@827 69 for band in qs.iterator():
bgneal@831 70 note = render_to_string('bandmap/balloon.html', {'band': band})
bgneal@831 71 note = note.strip().replace('\n', '')
bgneal@827 72 bands.append({
bgneal@827 73 'name': band.name,
bgneal@827 74 'lat': band.lat,
bgneal@827 75 'lon': band.lon,
bgneal@830 76 'is_active': band.is_active,
bgneal@831 77 'note': note,
bgneal@827 78 })
bgneal@827 79 results = json.dumps(bands)
bgneal@827 80
bgneal@827 81 # Store in cache
bgneal@827 82 cache.set(cache_key, results, 300)
bgneal@827 83
bgneal@827 84 return HttpResponse(results, content_type='application/json')