annotate bandmap/views.py @ 1135:3a2669a92709

Update kombu for a redis UNIX socket problem.
author Brian Neal <bgneal@gmail.com>
date Wed, 12 Oct 2016 20:29:36 -0500
parents bb7e2fc24690
children
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@1109 21 return render(request, 'bandmap/map.html', {'V3_DESIGN': True})
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@1109 43 'V3_DESIGN': True,
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@827 70 bands.append({
bgneal@827 71 'name': band.name,
bgneal@827 72 'lat': band.lat,
bgneal@827 73 'lon': band.lon,
bgneal@830 74 'is_active': band.is_active,
bgneal@843 75 'note': band.html,
bgneal@827 76 })
bgneal@827 77 results = json.dumps(bands)
bgneal@827 78
bgneal@827 79 # Store in cache
bgneal@843 80 cache.set(cache_key, results, 600)
bgneal@827 81
bgneal@827 82 return HttpResponse(results, content_type='application/json')