Mercurial > public > sg101
comparison bandmap/admin.py @ 826:d7e4c08b2e8b
Bandmap WIP: added admin action to update lat/lon.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 27 Sep 2014 15:14:47 -0500 |
parents | 5892c05886a9 |
children | 5103edd3acc4 |
comparison
equal
deleted
inserted
replaced
825:d91356818cef | 826:d7e4c08b2e8b |
---|---|
1 """Admin definitions for the bandmap application. | 1 """Admin definitions for the bandmap application. |
2 | 2 |
3 """ | 3 """ |
4 import datetime | 4 import datetime |
5 import json | |
6 import urllib | |
7 import urllib2 | |
5 | 8 |
6 from django.contrib import admin | 9 from django.contrib import admin |
10 import django.contrib.messages as messages | |
7 | 11 |
8 from bandmap.models import BandEntry | 12 from bandmap.models import BandEntry |
9 import bio.badges | 13 import bio.badges |
10 | 14 |
11 | 15 |
15 date_hierarchy = 'date_submitted' | 19 date_hierarchy = 'date_submitted' |
16 list_filter = ['date_submitted', 'is_active', 'is_approved'] | 20 list_filter = ['date_submitted', 'is_active', 'is_approved'] |
17 readonly_fields = ['lat', 'lon'] | 21 readonly_fields = ['lat', 'lon'] |
18 search_fields = ['name', 'location', 'note'] | 22 search_fields = ['name', 'location', 'note'] |
19 raw_id_fields = ['user'] | 23 raw_id_fields = ['user'] |
20 actions = ['approve_bands'] | 24 actions = ['approve_bands', 'update_location'] |
21 | 25 |
22 def approve_bands(self, request, qs): | 26 def approve_bands(self, request, qs): |
23 """This admin action awards a map pin to the user who added the band. | 27 """This admin action awards a map pin to the user who added the band. |
24 The band is then published and will be available for display on the map. | 28 The band is then published and will be available for display on the map. |
25 | 29 |
34 | 38 |
35 self.message_user(request, "%d band(s) approved." % count) | 39 self.message_user(request, "%d band(s) approved." % count) |
36 | 40 |
37 approve_bands.short_description = "Approve selected band map entries" | 41 approve_bands.short_description = "Approve selected band map entries" |
38 | 42 |
43 def update_location(self, request, qs): | |
44 """Admin action to refresh the lat/lon fields after updating the | |
45 location field. Queries the Google Maps HTTP server-side API for the | |
46 data. | |
47 | |
48 """ | |
49 base_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s" | |
50 ok_cnt = 0 | |
51 for band in qs: | |
52 url = base_url % urllib.quote_plus(band.location) | |
53 try: | |
54 response = urllib2.urlopen(url) | |
55 except urllib2.URLError as ex: | |
56 self.message_user(request, 'Error processing {}: {}'.format( | |
57 band.name, ex), level=messages.ERROR) | |
58 continue | |
59 | |
60 code = response.getcode() | |
61 if code == 200: | |
62 result = json.loads(response.read()) | |
63 try: | |
64 loc = result['results'][0]['geometry']['location'] | |
65 band.lat = loc['lat'] | |
66 band.lon = loc['lng'] | |
67 except (KeyError, IndexError) as ex: | |
68 self.message_user(request, 'Unexpected response for {}'.format( | |
69 band.name), level=messages.ERROR) | |
70 else: | |
71 band.save() | |
72 ok_cnt += 1 | |
73 else: | |
74 msg = "Error response ({}) for band {}".format(code, band.name) | |
75 self.message_user(request, msg, level=messages.ERROR) | |
76 | |
77 if ok_cnt > 0: | |
78 msg = '{} band location(s) successfully updated'.format(ok_cnt) | |
79 self.message_user(request, msg) | |
80 | |
81 update_location.short_description = "Update lat/lon for selected band map entries" | |
82 | |
39 | 83 |
40 admin.site.register(BandEntry, BandEntryAdmin) | 84 admin.site.register(BandEntry, BandEntryAdmin) |