changeset 821:71db8076dc3d

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.
author Brian Neal <bgneal@gmail.com>
date Tue, 23 Sep 2014 20:40:31 -0500
parents 9a0df7bd2409
children 2c4f28b1c12a
files bandmap/static/js/bandmap.js bandmap/views.py sg101/templates/bandmap/add.html
diffstat 3 files changed, 45 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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);
    }
 });
--- 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()
 
--- 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 %}
 <h3>Add Band</h3>
+{% if messages %}
+<ul class="user-messages">
+{% for message in messages %}
+   <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
+{% endfor %}
+</ul>
+{% endif %}
 <p>
 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 @@
 <tr>
    <td>&nbsp;</td>
    <td>
-      <input type="submit" name="submit_button" value="Submit" />
+      <input id="bandmap-add-submit" type="submit" name="submit_button" value="Submit" />
    </td>
 </tr>
 </table>