annotate bandmap/admin.py @ 938:9f9e50df5b83

Add tests for downloads signal handlers.
author Brian Neal <bgneal@gmail.com>
date Tue, 28 Apr 2015 21:22:42 -0500
parents 09ed84a7394c
children 4aadaf3bc234
rev   line source
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@843 25 readonly_fields = ['html']
bgneal@823 26
bgneal@823 27 def approve_bands(self, request, qs):
bgneal@823 28 """This admin action awards a map pin to the user who added the band.
bgneal@823 29 The band is then published and will be available for display on the map.
bgneal@823 30
bgneal@823 31 """
bgneal@823 32 count = qs.count()
bgneal@823 33 now = datetime.datetime.now()
bgneal@823 34 for band in qs:
bgneal@827 35 if not band.is_approved:
bgneal@827 36 if band.date_approved is None:
bgneal@827 37 bio.badges.award_badge(bio.badges.MAP_PIN, band.user)
bgneal@827 38 band.date_approved = now
bgneal@827 39 band.is_approved = True
bgneal@827 40 band.save()
bgneal@823 41
bgneal@823 42 self.message_user(request, "%d band(s) approved." % count)
bgneal@823 43
bgneal@823 44 approve_bands.short_description = "Approve selected band map entries"
bgneal@823 45
bgneal@826 46 def update_location(self, request, qs):
bgneal@826 47 """Admin action to refresh the lat/lon fields after updating the
bgneal@826 48 location field. Queries the Google Maps HTTP server-side API for the
bgneal@826 49 data.
bgneal@826 50
bgneal@826 51 """
bgneal@826 52 base_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s"
bgneal@826 53 ok_cnt = 0
bgneal@826 54 for band in qs:
bgneal@826 55 url = base_url % urllib.quote_plus(band.location)
bgneal@826 56 try:
bgneal@826 57 response = urllib2.urlopen(url)
bgneal@826 58 except urllib2.URLError as ex:
bgneal@826 59 self.message_user(request, 'Error processing {}: {}'.format(
bgneal@826 60 band.name, ex), level=messages.ERROR)
bgneal@826 61 continue
bgneal@826 62
bgneal@826 63 code = response.getcode()
bgneal@826 64 if code == 200:
bgneal@826 65 result = json.loads(response.read())
bgneal@826 66 try:
bgneal@826 67 loc = result['results'][0]['geometry']['location']
bgneal@826 68 band.lat = loc['lat']
bgneal@826 69 band.lon = loc['lng']
bgneal@826 70 except (KeyError, IndexError) as ex:
bgneal@826 71 self.message_user(request, 'Unexpected response for {}'.format(
bgneal@826 72 band.name), level=messages.ERROR)
bgneal@826 73 else:
bgneal@826 74 band.save()
bgneal@826 75 ok_cnt += 1
bgneal@826 76 else:
bgneal@826 77 msg = "Error response ({}) for band {}".format(code, band.name)
bgneal@826 78 self.message_user(request, msg, level=messages.ERROR)
bgneal@826 79
bgneal@826 80 if ok_cnt > 0:
bgneal@826 81 msg = '{} band location(s) successfully updated'.format(ok_cnt)
bgneal@826 82 self.message_user(request, msg)
bgneal@826 83
bgneal@826 84 update_location.short_description = "Update lat/lon for selected band map entries"
bgneal@826 85
bgneal@820 86
bgneal@820 87 admin.site.register(BandEntry, BandEntryAdmin)