Mercurial > public > sg101
comparison bandmap/views.py @ 827:5103edd3acc4
Bandmap: initial version complete.
Javascript queries server for band data.
View created to handle query.
Test created for view.
Link added to map in sidebar.
Template tweaks.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 27 Sep 2014 19:41:41 -0500 |
parents | d91356818cef |
children | b9973588af28 |
comparison
equal
deleted
inserted
replaced
826:d7e4c08b2e8b | 827:5103edd3acc4 |
---|---|
1 """Views for the bandmap application. | 1 """Views for the bandmap application. |
2 | 2 |
3 """ | 3 """ |
4 import json | |
5 | |
4 from django.contrib.auth.decorators import login_required | 6 from django.contrib.auth.decorators import login_required |
5 from django.shortcuts import redirect, render | 7 from django.shortcuts import redirect, render |
6 from django.contrib import messages as django_messages | 8 from django.contrib import messages as django_messages |
9 from django.http import HttpResponse | |
10 from django.template.loader import render_to_string | |
11 from django.core.cache import cache | |
7 | 12 |
8 from bandmap.forms import BandForm | 13 from bandmap.forms import BandForm |
14 from bandmap.models import BandEntry | |
9 | 15 |
10 | 16 |
11 SUCCESS = "Successfully submitted {} for admin review. Thanks!" | 17 SUCCESS = "Successfully submitted {} for admin review. Thanks!" |
12 | 18 |
13 | 19 |
33 form = BandForm() | 39 form = BandForm() |
34 | 40 |
35 return render(request, 'bandmap/add.html', { | 41 return render(request, 'bandmap/add.html', { |
36 'form': form, | 42 'form': form, |
37 }) | 43 }) |
44 | |
45 | |
46 def query(request): | |
47 """Retrieves the band map entries and returns them as JSON. | |
48 | |
49 """ | |
50 show_param = request.GET.get('show', 'all') | |
51 if show_param not in ['all', 'active', 'inactive']: | |
52 show_param = 'all' | |
53 | |
54 # Do we have the results cached already? | |
55 cache_key = 'bandmap-results-{}'.format(show_param) | |
56 results = cache.get(cache_key) | |
57 if results: | |
58 return HttpResponse(results, content_type='application/json') | |
59 | |
60 # Generate results | |
61 qs = BandEntry.objects.filter(is_approved=True) | |
62 if show_param == 'active': | |
63 qs = qs.filter(is_active=True) | |
64 elif show_param == 'inactive': | |
65 qs = qs.filter(is_active=False) | |
66 | |
67 bands = [] | |
68 for band in qs.iterator(): | |
69 bands.append({ | |
70 'name': band.name, | |
71 'url': band.url, | |
72 'lat': band.lat, | |
73 'lon': band.lon, | |
74 'note': render_to_string('bandmap/balloon.html', {'band': band}), | |
75 }) | |
76 results = json.dumps(bands) | |
77 | |
78 # Store in cache | |
79 cache.set(cache_key, results, 300) | |
80 | |
81 return HttpResponse(results, content_type='application/json') |