Luke Plant avatar Luke Plant committed 0645203

Enhanced booking admin to show place availability as soon as camp is selected

Comments (0)

Files changed (3)

cciw/bookings/urls.py

              (r'^account-json/$', 'account_json'),
              (r'^all-account-json/$', 'all_account_json'),
              (r'^booking-problems-json/$', 'booking_problems_json'),
+             (r'^place-availability-json/$', 'place_availability_json'),
              (r'^checkout/$', 'list_bookings'),
              (r'^pay/$', 'pay'),
              (r'^pay/done/$', 'pay_done'),

cciw/bookings/views.py

     return retval
 
 
+@json_response
+def place_availability_json(request):
+    retval = {'status': 'success'}
+    camp_id = int(request.GET['camp_id'])
+    camp = Camp.objects.get(id=camp_id)
+    places = camp.get_places_left()
+    retval['result'] = dict(total=places[0],
+                            male=places[1],
+                            female=places[2])
+    return retval
+
+
 def make_state_token(bookings):
     # Hash some key data about booking, without which the booking isn't valid.
     bookings.sort(key=lambda b: b.id)

templates/admin/bookings/booking/change_form.html

             }});
     }
 
+
+    $('div.field-camp').append('<div id="place-availability">');
+
+    var getPlaceAvailability = function() {
+        var campId = $('#id_camp').val();
+        if (campId == undefined || campId == "") {
+            $('#place-availability').html('');
+        }
+        $.ajax({
+            type: "GET",
+            url: '{% url "cciw.bookings.views.place_availability_json" %}?camp_id=' + campId,
+            dataType: "json",
+            success: function(json) {
+                if (json.status == 'success') {
+                    var html = ('Places available: total=' + json.result.total.toString() +
+                                ', male=' + json.result.male.toString() +
+                                ', female=' + json.result.female.toString())
+                    $('#place-availability').html(html);
+                }
+            }
+        })
+    };
+
     getBookingProblems();
     $('input,select,textarea').change(getBookingProblems);
 
+    getPlaceAvailability();
+    $('#id_camp').change(getPlaceAvailability);
 
 });
 </script>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.