Luke Plant avatar Luke Plant committed 5b785bc

Added shortcut for entering camper/contact address details on 'add place' page

Comments (0)

Files changed (4)

cciw/bookings/tests.py

         resp = self.client.get(reverse('cciw.bookings.views.places_json'))
         json = simplejson.loads(resp.content)
         self.assertEqual(json['places'][0]['name'], self.place_details['name'])
+
+    def test_account_json(self):
+        self.login()
+        acc = BookingAccount.objects.get(email=self.email)
+        acc.address = '123 Main Street'
+        acc.save()
+
+        resp = self.client.get(reverse('cciw.bookings.views.account_json'))
+        json = simplejson.loads(resp.content)
+        self.assertEqual(json['account']['address'], '123 Main Street')

cciw/bookings/urls.py

              (r'^add-place/$', 'add_place'),
              (r'^edit-place/(?P<id>\d+)/$', 'edit_place'),
              (r'^places-json/$', 'places_json'),
+             (r'^account-json/$', 'account_json'),
              (r'^checkout/$', 'list_bookings'),
              (r'^pay/$', 'pay'),
              (r'^pay/done/$', 'pay_done'),

cciw/bookings/views.py

         return c
 
 
+# Public attributes - i.e. that the account holder is allowed to see
 BOOKING_PLACE_PUBLIC_ATTRS = [
     'id',
     'name',
     'created',
 ]
 
+# Public attributes - i.e. that the account holder is allowed to see
+ACCOUNT_PUBLIC_ATTRS = [
+    'email',
+    'name',
+    'address',
+    'post_code',
+    'phone_number',
+]
+
 @booking_account_required
 @json_response
 def places_json(request):
     return retval
 
 
+@booking_account_required
+@json_response
+def account_json(request):
+    retval = {'status': 'success'}
+    retval['account'] = dict((k, getattr(request.booking_account, k))
+                             for k in ACCOUNT_PUBLIC_ATTRS)
+    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/cciw/bookings/add_place.html

 
 {% else %}
 
-       var placeData = [];
+       var userData = [];
 
        var escape = function(t) {
            return t.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
 
        var handleExistingPlacesData = function(json) {
            var places = json['places'];
-           placeData['places'] = places;
+           userData['places'] = places;
            if (places.length > 0) {
                var cont = $('#id_use_existing_radio_container');
                $('#id_use_existing_btn').show();
            }
        };
 
+       var handleAccountData = function(json) {
+           var account = json['account'];
+           userData['account'] = account;
+           /* Easiest way to get them where we want is to move it using javascript */
+           var btn1 = $('#id_use_account_1_btn');
+           btn1.appendTo(btn1.prev('div.form').find('div.formrow'));
+           btn1.css({'float':'right'}).show();
+
+           var btn2 = $('#id_use_account_2_btn');
+           btn2.appendTo(btn2.prev('div.form').find('div.formrow'));
+           btn2.css({'float':'right'}).show();
+
+       }
+
        var useExistingDataShow = function(ev) {
            ev.preventDefault();
            var popup = $('#id_use_existing_data_popup');
            for (var i=0; i < radios.length; i++) {
                if (radios[i].checked) {
                    chosen = parseInt(radios[i].value, 10);
-                   place = placeData['places'][chosen];
+                   place = userData['places'][chosen];
                    var mainform = document.addplaceform;
                    for (var j=0; j < attrs.length; j++) {
                        var attr = attrs[j];
            useData(gp_info_attrs);
        };
 
+       var useAccountForCamperAddressClick = function(ev) {
+           ev.preventDefault();
+           $('#id_address').val(userData['account']['address']);
+           $('#id_post_code').val(userData['account']['post_code']);
+           $('#id_phone_number').val(userData['account']['phone_number']);
+       }
+
+       var useAccountForContactDetailsClick = function(ev) {
+           ev.preventDefault();
+           $('#id_contact_name').val(userData['account']['name']);
+           $('#id_contact_phone_number').val(userData['account']['phone_number']);
+       }
+
        $('#id_popup_close_btn').click(useExistingDataClose);
        $('#id_use_existing_btn').click(useExistingDataShow);
        $('#id_use_all_btn').click(useAllBtnClick);
        $('#id_use_address_btn').click(useAddressBtnClick);
        $('#id_use_gp_info_btn').click(useGPInfoBtnClick);
+       $('#id_use_account_1_btn').click(useAccountForCamperAddressClick);
+       $('#id_use_account_2_btn').click(useAccountForContactDetailsClick);
 
        // Typing enter on text boxes shouldn't activate the 'Use existing
        // data' button
            success: handleExistingPlacesData
        });
 
+       /* Load account data */
+       $.ajax({
+           type: "GET",
+           url: '{% url "cciw.bookings.views.account_json" %}',
+           dataType: "json",
+           success: handleAccountData
+       });
+
 {% endif %}
 
    });
 {% cciw_form_field form 'name' 'Name' %}
 {% cciw_form_field form 'sex' 'Sex' %}
 {% cciw_form_field form 'date_of_birth' 'Date of birth (YYYY-MM-DD)' %}
-{% cciw_form_field form 'address' 'Address' %}
+{% cciw_form_field form 'address' 'Address' %} <input style="display:none;" type="submit" id="id_use_account_1_btn" value="Use account address">
 {% cciw_form_field form 'post_code' 'Post code' %}
 {% cciw_form_field form 'phone_number' 'Phone number' %}
 {% cciw_form_field form 'email' 'Email' %}
 
 <p>Please enter a contact name and number that can be used <strong>during the week of camp</strong> in case of emergency.</p>
 
-{% cciw_form_field form 'contact_name' 'Name' %}
+{% cciw_form_field form 'contact_name' 'Name' %} <input style="display:none;" type="submit" id="id_use_account_2_btn" value="Use account name and number">
 {% cciw_form_field form 'contact_phone_number' 'Phone number' %}
 
 <h2>Dietary requirements</h2>
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.