# HG changeset patch # User Brian Neal # Date 1411522831 18000 # Node ID 71db8076dc3d503d4d1326af8fcabae47c0c559a # Parent 9a0df7bd240971dc581374c6b81b1dbb61d466fd Bandmap WIP: geocoding integrated with add form. Add form works. Before submitting the form, client side JS makes a geocode request to Google and populates hidden lat/lon fields with the result. Successfully created a model instance on the server side. Still need to update admin dashboard, admin approval, and give out badges for adding bands to the map. Once that is done, then work on displaying the map with filtering. diff -r 9a0df7bd2409 -r 71db8076dc3d bandmap/static/js/bandmap.js --- a/bandmap/static/js/bandmap.js Sun Sep 21 18:20:29 2014 -0500 +++ b/bandmap/static/js/bandmap.js Tue Sep 23 20:40:31 2014 -0500 @@ -1,3 +1,31 @@ +var bandmap = null; +var geocoder = null; + +function addBandOnSubmit(event) { + var location = $('#id_location').val(); + if (!location) { + alert("Please enter a location"); + return false; + } + var button = $(this); + button.attr('disabled', 'disabled'); + var form = $('#bandmap-add-form'); + geocoder.geocode({'address': location}, function(results, status) { + button.removeAttr('disabled'); + if (status == google.maps.GeocoderStatus.OK) { + $('#id_lat').val(results[0].geometry.location.lat().toString()); + $('#id_lon').val(results[0].geometry.location.lng().toString()); + $(form).submit(); + } + else { + var msg = "Geocode unsuccessful: " + status + "\n" + + "Enter a new location"; + alert(msg); + } + }); + return false; +} + $(document).ready(function() { var map_div = $('#map-canvas'); if (map_div.length) { @@ -5,10 +33,12 @@ center: {lat: 15.0, lng: -30.0}, zoom: 2 }; - var map = new google.maps.Map(map_div[0], map_options); + bandmap = new google.maps.Map(map_div[0], map_options); } var add_form = $('#bandmap-add-form'); if (add_form.length) { + geocoder = new google.maps.Geocoder(); + $('#bandmap-add-submit').click(addBandOnSubmit); } }); diff -r 9a0df7bd2409 -r 71db8076dc3d bandmap/views.py --- a/bandmap/views.py Sun Sep 21 18:20:29 2014 -0500 +++ b/bandmap/views.py Tue Sep 23 20:40:31 2014 -0500 @@ -3,10 +3,14 @@ """ from django.contrib.auth.decorators import login_required from django.shortcuts import redirect, render +from django.contrib import messages as django_messages from bandmap.forms import BandForm +SUCCESS = "Successfully submitted {} for admin review. Thanks!" + + def map_view(request): return render(request, 'bandmap/map.html') @@ -23,7 +27,8 @@ band = form.save(commit=False) band.user = request.user band.save() - redirect('bandmap-thanks') + django_messages.success(request, SUCCESS.format(band.name)) + redirect('bandmap-add') else: form = BandForm() diff -r 9a0df7bd2409 -r 71db8076dc3d sg101/templates/bandmap/add.html --- a/sg101/templates/bandmap/add.html Sun Sep 21 18:20:29 2014 -0500 +++ b/sg101/templates/bandmap/add.html Tue Sep 23 20:40:31 2014 -0500 @@ -1,6 +1,13 @@ {% extends 'bandmap/bandmap_base.html' %} {% block bandmap_content %}

Add Band

+{% if messages %} + +{% endif %}

To add a band to our map, please fill out the form, below. The information will be reviewed by our staff and will normally be published within 24 hours. @@ -28,7 +35,7 @@   - +