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@820
|
17 list_display = ['name', 'date_submitted', 'date_approved', 'is_active',
|
bgneal@820
|
18 'is_approved']
|
bgneal@820
|
19 date_hierarchy = 'date_submitted'
|
bgneal@820
|
20 list_filter = ['date_submitted', 'is_active', 'is_approved']
|
bgneal@820
|
21 readonly_fields = ['lat', 'lon']
|
bgneal@820
|
22 search_fields = ['name', 'location', 'note']
|
bgneal@820
|
23 raw_id_fields = ['user']
|
bgneal@826
|
24 actions = ['approve_bands', 'update_location']
|
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@823
|
34 bio.badges.award_badge(bio.badges.MAP_PIN, band.user)
|
bgneal@823
|
35 band.date_approved = now
|
bgneal@823
|
36 band.is_approved = True
|
bgneal@823
|
37 band.save()
|
bgneal@823
|
38
|
bgneal@823
|
39 self.message_user(request, "%d band(s) approved." % count)
|
bgneal@823
|
40
|
bgneal@823
|
41 approve_bands.short_description = "Approve selected band map entries"
|
bgneal@823
|
42
|
bgneal@826
|
43 def update_location(self, request, qs):
|
bgneal@826
|
44 """Admin action to refresh the lat/lon fields after updating the
|
bgneal@826
|
45 location field. Queries the Google Maps HTTP server-side API for the
|
bgneal@826
|
46 data.
|
bgneal@826
|
47
|
bgneal@826
|
48 """
|
bgneal@826
|
49 base_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s"
|
bgneal@826
|
50 ok_cnt = 0
|
bgneal@826
|
51 for band in qs:
|
bgneal@826
|
52 url = base_url % urllib.quote_plus(band.location)
|
bgneal@826
|
53 try:
|
bgneal@826
|
54 response = urllib2.urlopen(url)
|
bgneal@826
|
55 except urllib2.URLError as ex:
|
bgneal@826
|
56 self.message_user(request, 'Error processing {}: {}'.format(
|
bgneal@826
|
57 band.name, ex), level=messages.ERROR)
|
bgneal@826
|
58 continue
|
bgneal@826
|
59
|
bgneal@826
|
60 code = response.getcode()
|
bgneal@826
|
61 if code == 200:
|
bgneal@826
|
62 result = json.loads(response.read())
|
bgneal@826
|
63 try:
|
bgneal@826
|
64 loc = result['results'][0]['geometry']['location']
|
bgneal@826
|
65 band.lat = loc['lat']
|
bgneal@826
|
66 band.lon = loc['lng']
|
bgneal@826
|
67 except (KeyError, IndexError) as ex:
|
bgneal@826
|
68 self.message_user(request, 'Unexpected response for {}'.format(
|
bgneal@826
|
69 band.name), level=messages.ERROR)
|
bgneal@826
|
70 else:
|
bgneal@826
|
71 band.save()
|
bgneal@826
|
72 ok_cnt += 1
|
bgneal@826
|
73 else:
|
bgneal@826
|
74 msg = "Error response ({}) for band {}".format(code, band.name)
|
bgneal@826
|
75 self.message_user(request, msg, level=messages.ERROR)
|
bgneal@826
|
76
|
bgneal@826
|
77 if ok_cnt > 0:
|
bgneal@826
|
78 msg = '{} band location(s) successfully updated'.format(ok_cnt)
|
bgneal@826
|
79 self.message_user(request, msg)
|
bgneal@826
|
80
|
bgneal@826
|
81 update_location.short_description = "Update lat/lon for selected band map entries"
|
bgneal@826
|
82
|
bgneal@820
|
83
|
bgneal@820
|
84 admin.site.register(BandEntry, BandEntryAdmin)
|