Commits

Matt Bone  committed ab89d9f

starting api. ajax ride button works with this new api.

  • Participants
  • Parent commits 3e6189a

Comments (0)

Files changed (3)

File pyramid/didyouride/didyouride/__init__.py

     config.add_route('signup', '/signup')
     config.add_route('suggestion', '/suggestion')
 
+    config.add_route('api_ride', '/api/ride/{username}/{ride_name}/{date}/{round_trip}', factory=rider_root)
+
     config.scan('didyouride')
 
     return config.make_wsgi_app()

File pyramid/didyouride/didyouride/api.py

+import datetime
+from pyramid.view import view_config
+
+@view_config(route_name="api_ride", renderer="json", permission="edit")
+def ride(rider, request):
+    round_trip = True if request.matchdict['round_trip'] == "true" else False
+    ride_date = datetime.datetime.strptime(request.matchdict['date'], "%Y-%m-%d")
+
+    rider.ride(request.matchdict['ride_name'], ride_date=ride_date)
+
+    if round_trip:
+        rider.ride(request.matchdict['ride_name'], ride_date=ride_date)
+
+    return {'status':'success'}

File pyramid/didyouride/didyouride/templates/rider_detail.jinja2

 
 {% block script %}
 $(function() {
+
   $(".datepicker").datepicker({dateFormat: 'yy-mm-dd'});
+
   deform.load();
-  $('a[id^="ridebutton_"]').click(function() {
+ 
+  $('a[id^="ride_button_"]').click(function() {
     $(this).next().show('slow');
     return false;
   });
+
+  $('a[id^="ride_submit_button_"]').click(function() {
+    var parent = $(this).parent().parent();
+    
+    var round_trip = parent.find('input[name^="round_trip_"]').is(':checked')
+    var date = parent.find('input[name^="date_"]').val()
+    var username = parent.find('input[name^="username_"]').val();
+    var ride = parent.find('input[name^="ride_"]').val();
+
+    jQuery.getJSON('/api/ride/' + username + '/' + ride + '/' + date + '/' + round_trip,
+      function(data) {
+        parent.hide('slow');
+      }
+    );
+  });
+
 });
 {% endblock %}
 
 
 <div class="rides">
 {% for ride_definition in rider.ride_definitions.values() %}
-{% if loop.first %}<ul>{% endif %}
-<li>{{ride_definition.name}} ({{ride_definition.length}} miles) 
-{% if rider.allow_edit(request) %}
-  {#<a href="{{'ride'|route_url(username=rider.username, ride_name=ride_definition.name)}}" class="rideButton">#}
-  <a href="#" class="rideButton" id="ridebutton_{{loop.index}}">
-    I Rode This
-  </a>
-  <div class="ride_entry_detail" id="ride_entry_detail_{{loop.index}}" style="display: none;">
-  <p>Round Trip? <input type="checkbox"/></p>
-  <p>When? <input type="text" class="datepicker" size="10" value="{{today}}"/></p>
-  <p><a href="#" class="rideButton">Submit</a></p>
-  </div>
-{% endif %}
-</li>
-{% if loop.last %}</ul>{% endif %}
+  {% if loop.first %}<ul>{% endif %}
+  <li>{{ride_definition.name}} ({{ride_definition.length}} miles) 
+    {% if rider.allow_edit(request) %}
+      <a href="#" class="rideButton" id="ride_button_{{loop.index}}">
+        I Rode This
+      </a>
+      <div class="ride_entry_detail" id="ride_entry_detail_{{loop.index}}" style="display: none;">
+        <p>Round Trip? <input name="round_trip_{{loop.index}}" type="checkbox"/></p>
+        <p>When? <input name="date_{{loop.index}}" type="text" class="datepicker" size="10" value="{{today}}"/></p>
+        <input name="username_{{loop.index}}" type="hidden" value="{{ rider.username }}"/>
+        <input name="ride_{{loop.index}}" type="hidden" value="{{ ride_definition.name }}"/>
+        <p><a href="#" class="rideButton" id="ride_submit_button_{{loop.index}}">Submit</a></p>
+      </div>
+    {% endif %}
+  </li>
+  {% if loop.last %}</ul>{% endif %}
 {% else %}
-<p>It looks like you haven't defined any rides.  You can define a name
-and length for a route you take often by clicking on `Create a New
-Ride` below, or you can just ride a custom, one-off ride, too.</p>
+  <p>
+    It looks like you haven't defined any rides.  You can define a name
+    and length for a route you take often by clicking on `Create a New
+    Ride` below, or you can just ride a custom, one-off ride, too.
+  </p>
 {% endfor %}
 </div>
 
 <li>{{ride.length}} miles - {{ride.ride_date.strftime("%m/%d/%Y")}}</li>
 {% endfor %}
 </ul>
-{% endblock %}
+{% endblock %}