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