bgneal@820: """Admin definitions for the bandmap application. bgneal@820: bgneal@820: """ bgneal@823: import datetime bgneal@826: import json bgneal@826: import urllib bgneal@826: import urllib2 bgneal@823: bgneal@820: from django.contrib import admin bgneal@826: import django.contrib.messages as messages bgneal@820: bgneal@820: from bandmap.models import BandEntry bgneal@823: import bio.badges bgneal@820: bgneal@820: bgneal@820: class BandEntryAdmin(admin.ModelAdmin): bgneal@830: list_display = ['name', 'date_submitted', 'location', 'is_active', bgneal@820: 'is_approved'] bgneal@820: date_hierarchy = 'date_submitted' bgneal@820: list_filter = ['date_submitted', 'is_active', 'is_approved'] bgneal@820: readonly_fields = ['lat', 'lon'] bgneal@820: search_fields = ['name', 'location', 'note'] bgneal@820: raw_id_fields = ['user'] bgneal@827: actions = ['update_location', 'approve_bands'] bgneal@823: bgneal@823: def approve_bands(self, request, qs): bgneal@823: """This admin action awards a map pin to the user who added the band. bgneal@823: The band is then published and will be available for display on the map. bgneal@823: bgneal@823: """ bgneal@823: count = qs.count() bgneal@823: now = datetime.datetime.now() bgneal@823: for band in qs: bgneal@827: if not band.is_approved: bgneal@827: if band.date_approved is None: bgneal@827: bio.badges.award_badge(bio.badges.MAP_PIN, band.user) bgneal@827: band.date_approved = now bgneal@827: band.is_approved = True bgneal@827: band.save() bgneal@823: bgneal@823: self.message_user(request, "%d band(s) approved." % count) bgneal@823: bgneal@823: approve_bands.short_description = "Approve selected band map entries" bgneal@823: bgneal@826: def update_location(self, request, qs): bgneal@826: """Admin action to refresh the lat/lon fields after updating the bgneal@826: location field. Queries the Google Maps HTTP server-side API for the bgneal@826: data. bgneal@826: bgneal@826: """ bgneal@826: base_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s" bgneal@826: ok_cnt = 0 bgneal@826: for band in qs: bgneal@826: url = base_url % urllib.quote_plus(band.location) bgneal@826: try: bgneal@826: response = urllib2.urlopen(url) bgneal@826: except urllib2.URLError as ex: bgneal@826: self.message_user(request, 'Error processing {}: {}'.format( bgneal@826: band.name, ex), level=messages.ERROR) bgneal@826: continue bgneal@826: bgneal@826: code = response.getcode() bgneal@826: if code == 200: bgneal@826: result = json.loads(response.read()) bgneal@826: try: bgneal@826: loc = result['results'][0]['geometry']['location'] bgneal@826: band.lat = loc['lat'] bgneal@826: band.lon = loc['lng'] bgneal@826: except (KeyError, IndexError) as ex: bgneal@826: self.message_user(request, 'Unexpected response for {}'.format( bgneal@826: band.name), level=messages.ERROR) bgneal@826: else: bgneal@826: band.save() bgneal@826: ok_cnt += 1 bgneal@826: else: bgneal@826: msg = "Error response ({}) for band {}".format(code, band.name) bgneal@826: self.message_user(request, msg, level=messages.ERROR) bgneal@826: bgneal@826: if ok_cnt > 0: bgneal@826: msg = '{} band location(s) successfully updated'.format(ok_cnt) bgneal@826: self.message_user(request, msg) bgneal@826: bgneal@826: update_location.short_description = "Update lat/lon for selected band map entries" bgneal@826: bgneal@820: bgneal@820: admin.site.register(BandEntry, BandEntryAdmin)