[Assorted-commits] SF.net SVN: assorted:[1523] pitch-in/trunk
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-11-27 08:04:49
|
Revision: 1523 http://assorted.svn.sourceforge.net/assorted/?rev=1523&view=rev Author: yangzhang Date: 2009-11-27 08:04:35 +0000 (Fri, 27 Nov 2009) Log Message: ----------- - using table layout for listing contributions - highlight contributions that are being counted in the aggregates - properly strip input - only count non-negative values - remove redundant escapes; django 1.0 introduced autoescape - updated todos - show min Modified Paths: -------------- pitch-in/trunk/README pitch-in/trunk/src/pitchin/pitchin/settings.py pitch-in/trunk/src/pitchin/pitchin/templates/pool.html pitch-in/trunk/src/pitchin/pitchin/views.py pitch-in/trunk/src/pitchin/static/default.css Added Paths: ----------- pitch-in/trunk/src/pitchin/pitchin/templatetags/ pitch-in/trunk/src/pitchin/pitchin/templatetags/__init__.py pitch-in/trunk/src/pitchin/pitchin/templatetags/filters.py Modified: pitch-in/trunk/README =================================================================== --- pitch-in/trunk/README 2009-11-27 06:35:23 UTC (rev 1522) +++ pitch-in/trunk/README 2009-11-27 08:04:35 UTC (rev 1523) @@ -31,10 +31,6 @@ - Other types of pools besides integers (decimals, unique strings, strings) - Comprehensive logging - Archiving/garbage collection -- Upgrade to Django 1.0. Nice new features in Django 1.0 include: - - new cycle syntax: cycle '0' '1' - - autoescape - - for key, value in collection Related ------- Modified: pitch-in/trunk/src/pitchin/pitchin/settings.py =================================================================== --- pitch-in/trunk/src/pitchin/pitchin/settings.py 2009-11-27 06:35:23 UTC (rev 1522) +++ pitch-in/trunk/src/pitchin/pitchin/settings.py 2009-11-27 08:04:35 UTC (rev 1523) @@ -81,6 +81,7 @@ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', + 'pitchin', ) APPEND_SLASH = False Modified: pitch-in/trunk/src/pitchin/pitchin/templates/pool.html =================================================================== --- pitch-in/trunk/src/pitchin/pitchin/templates/pool.html 2009-11-27 06:35:23 UTC (rev 1522) +++ pitch-in/trunk/src/pitchin/pitchin/templates/pool.html 2009-11-27 08:04:35 UTC (rev 1523) @@ -1,21 +1,28 @@ {% extends 'base.html' %} +{% load filters %} {% block body %} - <p style="font-size: larger"><span style="font-weight: bold">Pool:</span> {{descrip|escape}}</p> + <p style="font-size: larger"><span style="font-weight: bold">Pool:</span> {{descrip}}</p> {% if not contribs %} <p style="font-style: italic;">(no participants yet)</p> {% else %} - {% for contrib in contribs %} - <div class="row{% cycle 0,1 %}">{{contrib.0|escape}}: {{contrib.1|escape}}</div> - {% endfor %} - {% for contrib in aggcontribs %} - <div class="aggrow">{{contrib.0|escape}}: {{contrib.1|escape}}</div> - {% endfor %} + <table class="contribs"> + <tr class="aggrow"><td>Name</td><td>Contribution</td></tr> + {% for name, val in contribs %} + {% comment %}<div class="row{% cycle 0,1 %}">{{name}}: {{val|boldnum}}</div>{% endcomment %} + <tr class="row{% cycle 0,1 %}"><td>{{name}}</td><td>{{val|boldnum}}</td></tr> + {% endfor %} + {% for name, val in aggcontribs %} + {% comment %}<div class="aggrow">{{name}}: {{val|boldnum}}</div>{% endcomment %} + <tr class="aggrow"><td>{{name}}</td><td>{{val|boldnum}}</td></tr> + {% endfor %} + </table> {% endif %} <p> To add or update your own contribution to the pool, enter the following information. </p> <form action="/pool" method="post"> + {% if form.errors %} <div class="errors"> {% comment %} <ul> @@ -26,12 +33,13 @@ {% endcomment %} {{form.errors}} </div> + {% endif %} {% comment %} {% for field in form %} {{field}} {% endfor %} {% endcomment %} - <div class="hideerrors">{{form}}</div> + <div class="hideerrors">{{form.as_p}}</div> <!-- <div> <input type="hidden" name="key" value="{{key|urlencode}}"/> @@ -39,13 +47,13 @@ <div> <label> Name: - <input type="text" name="name" value="{{name|escape}}"/> + <input type="text" name="name" value="{{name}}"/> </label> </div> <div> <label> Contribution: - <input type="text" name="contrib" value="{{contrib|escape}}"/> + <input type="text" name="contrib" value="{{contrib}}"/> </label> </div> --> Added: pitch-in/trunk/src/pitchin/pitchin/templatetags/filters.py =================================================================== --- pitch-in/trunk/src/pitchin/pitchin/templatetags/filters.py (rev 0) +++ pitch-in/trunk/src/pitchin/pitchin/templatetags/filters.py 2009-11-27 08:04:35 UTC (rev 1523) @@ -0,0 +1,12 @@ +from django import template +from django.template.defaultfilters import stringfilter +from django.utils.html import escape +from django.utils.safestring import mark_safe +from pitchin.common import isvalidnum + +register = template.Library() + +...@re...lter(name = 'boldnum') +@stringfilter +def boldnum(x): + return mark_safe('<strong>%s</strong>' % x) if isvalidnum(x) else escape(x) Modified: pitch-in/trunk/src/pitchin/pitchin/views.py =================================================================== --- pitch-in/trunk/src/pitchin/pitchin/views.py 2009-11-27 06:35:23 UTC (rev 1522) +++ pitch-in/trunk/src/pitchin/pitchin/views.py 2009-11-27 08:04:35 UTC (rev 1523) @@ -1,8 +1,9 @@ +from django import forms +from django.forms.util import ErrorList from django.http import HttpResponseRedirect from django.shortcuts import render_to_response -from django import forms -from django.forms.util import ErrorList from google.appengine.ext import db +from pitchin.common import isvalidnum from pitchin.models import Pool import cPickle, cgi @@ -14,14 +15,21 @@ return ( lambda *args, **kwargs: db.run_in_transaction_custom_retries(3, f, *args, **kwargs) ) -def isfloat(x): - try: float(x) - except ValueError: return False - else: return True - def mean(xs): return sum(xs) / len(xs) # +# General Django utilities. +# + +class StrippedCharField(forms.CharField): + """ + Newforms CharField that strips trailing and leading spaces + """ + def clean(self, value): + if value is not None: value = value.strip() + return super(StrippedCharField, self).clean(value) + +# # Application code. # @@ -34,12 +42,13 @@ def getContribs(pool): return cPickle.loads(str(pool.contribs)) def serContribs(contribs): return cPickle.dumps(contribs, 2) def aggContribs(contribs): - nums = map(float, filter(isfloat, contribs.itervalues())) - if len(nums) == 0 or len(nums) < len(contribs) / 2: + nums = [float(x) for x in contribs.itervalues() if isvalidnum(x)] + if len(nums) < 2: return [] else: return [ ( 'Total', '%.2f' % sum(nums) ), ( 'Average', '%.2f' % mean(nums) ), + ( 'Minimum', '%.2f' % min(nums) ), ( 'If everyone paid the minimum', '%.2f' % (len(contribs) * min(nums)) ) ] @@ -56,13 +65,13 @@ def as_divs(self): return u'' class ContactForm(forms.Form): - key = forms.CharField(required = True, + key = StrippedCharField(required = True, max_length = 100, widget = forms.HiddenInput) - name = forms.CharField(required = True, max_length = 100, + name = StrippedCharField(required = True, max_length = 100, label = 'Name', widget = forms.TextInput(attrs = {})) - contrib = forms.CharField(required = True, max_length = 100, label = 'Contribution') + contrib = StrippedCharField(required = True, max_length = 100, label = 'Contribution') def __init__(self, *args, **kwargs): #kwargs['error_class'] = CustomErrorList forms.Form.__init__(self, *args, **kwargs) @@ -112,7 +121,7 @@ try: form = ContactForm(request.REQUEST) if not form.is_valid(): raise invalid_submit(form.errors) - cleaned = form.clean_data + cleaned = form.cleaned_data except invalid_submit, ex: return view(request, form) else: Modified: pitch-in/trunk/src/pitchin/static/default.css =================================================================== --- pitch-in/trunk/src/pitchin/static/default.css 2009-11-27 06:35:23 UTC (rev 1522) +++ pitch-in/trunk/src/pitchin/static/default.css 2009-11-27 08:04:35 UTC (rev 1523) @@ -33,7 +33,7 @@ .text-input { width: 75%; } .quote { font-family: serif; font-size: 50pt; float: left; } .line { width: 75%; font-size: larger; font-style: italic; margin: 0 auto; margin-bottom: 50px; } -.rows { width: 75%; margin: 0 auto; } +.contribs { width: 50%; margin-left: 25%; margin-right: 25%; } .row0, .row1, .aggrow { padding-top: 3px; padding-bottom: 3px; } .row0 { background-color: #e3e3f8; } .row1 { background-color: #eeeeff; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |