Skip to content

Commit

Permalink
add link to google in geocode admin for easy check; refs #30
Browse files Browse the repository at this point in the history
  • Loading branch information
copelco committed Dec 21, 2011
1 parent 64190d7 commit 7a5708b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions openrural/error_log/admin.py
@@ -1,7 +1,8 @@
from django.db import models
from django.contrib import admin

from openrural.error_log.models import Geocode, Batch
from openrural.error_log.forms import GeocodeForm
from openrural.error_log.forms import GeocodeForm, GoogleMapsLink


class BatchAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -31,7 +32,9 @@ class GeocodeAdmin(admin.ModelAdmin):
readonly_fields = ('success', 'name', 'batch', 'news_item', 'scraper',
'zipcode')
form = GeocodeForm

formfield_overrides = {
models.CharField: {'widget': GoogleMapsLink},
}
def save_model(self, request, obj, form, change):
obj.news_item.location = form.cleaned_data['result']['point']
obj.news_item.save()
Expand Down
18 changes: 18 additions & 0 deletions openrural/error_log/forms.py
@@ -1,10 +1,26 @@
import urllib

from django import forms
from django.utils.safestring import mark_safe

from ebpub import geocoder

from openrural.error_log.models import Geocode


__all__ = ('GoogleMapsLink', 'GeocodeForm')


class GoogleMapsLink(forms.TextInput):

def render(self, name, value, attrs=None):
html = super(GoogleMapsLink, self).render(name, value, attrs=None)
location = urllib.urlencode({'q': value})
link = "http://maps.google.com/maps?{0}".format(location)
html += "&nbsp;<a href='{0}'>Google Map</a>".format(link)
return mark_safe(html)


class GeocodeForm(forms.ModelForm):

class Meta(object):
Expand All @@ -15,6 +31,8 @@ def clean_location(self):
smart_geocoder = geocoder.SmartGeocoder()
try:
self.cleaned_data['result'] = smart_geocoder.geocode(location)
except geocoder.InvalidBlockButValidStreet, e:
raise forms.ValidationError('InvalidBlockButValidStreet')
except geocoder.GeocodingException, e:
raise forms.ValidationError(unicode(e))
return location

0 comments on commit 7a5708b

Please sign in to comment.