annotate bandmap/views.py @ 844:6900400f971b

Add NSSR Halloween vote banner.
author Brian Neal <bgneal@gmail.com>
date Tue, 14 Oct 2014 15:14:50 -0500
parents 09ed84a7394c
children bb7e2fc24690
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.core.cache import cache
bgneal@838 11 from django.conf import settings
bgneal@820 12
bgneal@820 13 from bandmap.forms import BandForm
bgneal@827 14 from bandmap.models import BandEntry
bgneal@820 15
bgneal@820 16
bgneal@840 17 SUCCESS = u"Successfully submitted {} for admin review. Thanks!"
bgneal@821 18
bgneal@821 19
bgneal@820 20 def map_view(request):
bgneal@820 21 return render(request, 'bandmap/map.html')
bgneal@820 22
bgneal@820 23
bgneal@820 24 @login_required
bgneal@820 25 def add_band(request):
bgneal@820 26 """
bgneal@820 27 Provides the ability for a user to submit a new band to the map.
bgneal@820 28
bgneal@820 29 """
bgneal@820 30 if request.method == 'POST':
bgneal@820 31 form = BandForm(request.POST)
bgneal@820 32 if form.is_valid():
bgneal@820 33 band = form.save(commit=False)
bgneal@820 34 band.user = request.user
bgneal@820 35 band.save()
bgneal@821 36 django_messages.success(request, SUCCESS.format(band.name))
bgneal@825 37 return redirect('bandmap-add')
bgneal@820 38 else:
bgneal@820 39 form = BandForm()
bgneal@820 40
bgneal@820 41 return render(request, 'bandmap/add.html', {
bgneal@820 42 'form': form,
bgneal@820 43 })
bgneal@827 44
bgneal@827 45
bgneal@827 46 def query(request):
bgneal@827 47 """Retrieves the band map entries and returns them as JSON.
bgneal@827 48
bgneal@827 49 """
bgneal@827 50 show_param = request.GET.get('show', 'all')
bgneal@827 51 if show_param not in ['all', 'active', 'inactive']:
bgneal@827 52 show_param = 'all'
bgneal@827 53
bgneal@827 54 # Do we have the results cached already?
bgneal@827 55 cache_key = 'bandmap-results-{}'.format(show_param)
bgneal@827 56 results = cache.get(cache_key)
bgneal@838 57 if results and not settings.DEBUG:
bgneal@827 58 return HttpResponse(results, content_type='application/json')
bgneal@827 59
bgneal@827 60 # Generate results
bgneal@827 61 qs = BandEntry.objects.filter(is_approved=True)
bgneal@827 62 if show_param == 'active':
bgneal@827 63 qs = qs.filter(is_active=True)
bgneal@827 64 elif show_param == 'inactive':
bgneal@827 65 qs = qs.filter(is_active=False)
bgneal@827 66
bgneal@827 67 bands = []
bgneal@827 68 for band in qs.iterator():
bgneal@827 69 bands.append({
bgneal@827 70 'name': band.name,
bgneal@827 71 'lat': band.lat,
bgneal@827 72 'lon': band.lon,
bgneal@830 73 'is_active': band.is_active,
bgneal@843 74 'note': band.html,
bgneal@827 75 })
bgneal@827 76 results = json.dumps(bands)
bgneal@827 77
bgneal@827 78 # Store in cache
bgneal@843 79 cache.set(cache_key, results, 600)
bgneal@827 80
bgneal@827 81 return HttpResponse(results, content_type='application/json')