Commits

Matt Bone committed 99fd60f

custom rides

  • Participants
  • Parent commits 71e890e

Comments (0)

Files changed (7)

pyramid/didyouride/didyouride/__init__.py

 
     config.add_route('index', '/',) 
     config.add_route('riders', '/riders',)
-    config.add_route('rider_detail', '/rider/{username}')
+    config.add_route('rider_detail', '/rider/{username}', factory=rider_root)
     config.add_route('new_ride_definition', '/rider/{username}/new-ride', factory=rider_root)
+    config.add_route('new_custom_ride', '/rider/{username}/custom-ride', factory=rider_root)    
     config.add_route('ride', '/rider/{username}/ride/{ride_name}', factory=rider_root)
     config.add_route('login', '/login')
     config.add_route('logout', '/logout')

pyramid/didyouride/didyouride/static/didyouride.css

 
 .deformFormFieldset ul {
     text-decoration: none;
-    font-size: 1.2em;
+    font-size: 14px;
     list-style: none;
 }
 
+.deformFormFieldset label {
+    padding-top: 10px;
+    padding-bottom: 5px;
+    display: block;
+    clear: both;
+}
+
+.deformFormFieldset button {
+    margin-top: 10px;
+}
+
+.deform fieldset {
+    border: 1px;
+}
+
 .rides ul {
     font-size: 1.2em;
     padding-left: 25px;

pyramid/didyouride/didyouride/templates/base.jinja2

     <link rel="shortcut icon" href="http://wherever-your-favicon-is.org/favicon.png"  />
     <link rel="icon" href="http://wherever-your-favicon-is.org/favicon.png" />
 
+    <script type="text/javascript">
+  var _gaq = _gaq || [];
+  _gaq.push(['_setAccount', 'UA-24348255-1']);
+  _gaq.push(['_trackPageview']);
+
+  (function() {
+    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+  })();
+</script>
+
   </head>
 
   <body>

pyramid/didyouride/didyouride/templates/custom_ride.jinja2

+{% extends "base.jinja2" %}
+
+{% block content %}
+<h2>{{rider.username}} - custom ride</h2>
+
+{{form.render()|safe}}
+
+{% endblock %}
+
+{# TODO refactor this copy/paste #}
+{% block sidebar %}
+<h3>stats</h3>
+<p>So far you've ridden: {{ rider.total_length() }} miles.</p>
+
+<ul>
+{% for ride in rider.rides %}
+<li>{{ride.length}} miles - {{ride.ride_date.strftime("%m/%d/%Y")}}</li>
+{% endfor %}
+</ul>
+{% endblock %}

pyramid/didyouride/didyouride/templates/new_ride.jinja2

 {% extends "base.jinja2" %}
 
 {% block content %}
-<h2>{{rider.username}}</h2>
+<h2>{{rider.username}} - new ride</h2>
 
 {{form.render()|safe}}
 
+{% endblock %}
+
+{# TODO refactor this copy/paste #}
+{% block sidebar %}
+<h3>stats</h3>
+<p>So far you've ridden: {{ rider.total_length() }} miles.</p>
+
+<ul>
+{% for ride in rider.rides %}
+<li>{{ride.length}} miles - {{ride.ride_date.strftime("%m/%d/%Y")}}</li>
+{% endfor %}
+</ul>
 {% endblock %}

pyramid/didyouride/didyouride/templates/rider_detail.jinja2

 <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">I Rode This</a>{% endif %}</li>
 {% if loop.last %}</ul>{% endif %}
 {% else %}
-<p>It looks like you haven't defined any rides.</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>
 
 {% if rider.allow_edit(request) %}
 <p><a href="{{'new_ride_definition'|route_url(username=rider.username)}}" class="rideButton">Create a New Ride</a></p>
+
+<h3>custom ride</h3>
+{{custom_ride_form.render()|safe}}
 {% endif %}
 
 {% endblock %}

pyramid/didyouride/didyouride/views.py

 from pyramid.httpexceptions import HTTPFound
 from pyramid.security import forget, remember
 from pyramid.url import route_url
-import deform.widget
 
 import bcrypt
 import datetime
 ## Forms and such
 import colander
 from deform import Form, ValidationFailure
+import deform.widget
 
 @colander.deferred
 def unique_user_validator(node, kw):
 
 class CustomRideSchema(colander.MappingSchema):
     length = colander.SchemaNode(colander.Float())
+    date = colander.SchemaNode(
+        colander.Date(),
+        #validator=Range(
+        #    min=datetime.date(2010, 5, 5),
+        #    min_err=_('${val} is earlier than earliest date ${min}')
+        #    )
+        )    
 
 
 class RideDefinitionSchema(colander.MappingSchema):
 
 
 @view_config(route_name="rider_detail", renderer="rider_detail.jinja2")
-def rider_detail(root, request):
-    rider = root.riders[request.matchdict['username']]
+def rider_detail(rider, request):
+    custom_ride_form = Form(CustomRideSchema(),
+                            action=route_url('new_custom_ride', request, username=rider.username),
+                            buttons=('submit',)
+                            )
 
-    return {'rider':rider}
+    return {'rider': rider,
+            'custom_ride_form': custom_ride_form}
 
 
 @view_config(route_name="new_ride_definition",  permission='edit', renderer="new_ride.jinja2")
             'recent_rides': root.recent_rides}
 
 
-# @view_config(route_name="new_ride_definition",  permission='edit', renderer="new_ride.jinja2")
-# def new_custom_ride(rider, request):
+@view_config(route_name="new_custom_ride",  permission='edit', renderer="custom_ride.jinja2")
+def new_custom_ride(rider, request):
 
-#     form = Form(CustomRideSchema(), buttons=('submit',))
+    form = Form(CustomRideSchema(), buttons=('submit',))
 
-#     if 'submit' in request.POST:
-#         controls = request.POST.items()
+    if 'submit' in request.POST:
+        controls = request.POST.items()
 
-#         try:
-#             appstruct = form.validate(controls)
+        try:
+            appstruct = form.validate(controls)
 
-#             rider.custom_ride(appstruct['length'], ride_date=datetime.datetime.now())
+            rider.custom_ride(appstruct['length'], ride_date=appstruct['date'])
             
-#             return HTTPFound(route_url('rider_detail', request, username=rider.username))
+            return HTTPFound(route_url('rider_detail', request, username=rider.username))
     
-#         except ValidationFailure as e:
-#             return {'rider': rider,
-#                     'form': e}
+        except ValidationFailure as e:
+            return {'rider': rider,
+                    'form': e}
         
-#     return HTTPFound(route_url('rider_detail', request, username=rider.username))
+        return HTTPFound(route_url('rider_detail', request, username=rider.username))
+    
+    else:
+        return {'rider': rider,
+                'form': form}
 
 
-    
 @view_config(route_name="ride", permission='edit')
 def ride(rider, request):
     ride_name = request.matchdict['ride_name']