[Assorted-commits] SF.net SVN: assorted:[1424] pitch-in
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-05-19 00:09:41
|
Revision: 1424 http://assorted.svn.sourceforge.net/assorted/?rev=1424&view=rev Author: yangzhang Date: 2009-05-19 00:09:28 +0000 (Tue, 19 May 2009) Log Message: ----------- added pitch-in Added Paths: ----------- pitch-in/ pitch-in/trunk/ pitch-in/trunk/src/ pitch-in/trunk/src/helloworld/ pitch-in/trunk/src/helloworld/app.yaml pitch-in/trunk/src/helloworld/pitchin.py Added: pitch-in/trunk/src/helloworld/app.yaml =================================================================== --- pitch-in/trunk/src/helloworld/app.yaml (rev 0) +++ pitch-in/trunk/src/helloworld/app.yaml 2009-05-19 00:09:28 UTC (rev 1424) @@ -0,0 +1,8 @@ +application: pitch-in +version: 1 +runtime: python +api_version: 1 + +handlers: +- url: /.* + script: pitchin.py Added: pitch-in/trunk/src/helloworld/pitchin.py =================================================================== --- pitch-in/trunk/src/helloworld/pitchin.py (rev 0) +++ pitch-in/trunk/src/helloworld/pitchin.py 2009-05-19 00:09:28 UTC (rev 1424) @@ -0,0 +1,138 @@ +import cgi + +# TODO: add anonymization feature + +from google.appengine.api import users +from google.appengine.ext import webapp +from google.appengine.ext.webapp.util import run_wsgi_app +from google.appengine.ext import db +import cPickle, logging + +def getContribs(pool): return cPickle.loads(str(pool.contribs)) +def serContribs(contribs): return cPickle.dumps(contribs, 2) +def showContribs(pool): + contribs = getContribs(pool) + if len(contribs) == 0: + return '<p>(no participants yet)</p>' + else: + s = '<ul>' + for name, amount in contribs.iteritems(): + s += '<li>%s: $%d.00</li>' % (cgi.escape(name), amount) + s += '<li>TOTAL: $%d.00</li></ul>' % (sum(contribs.itervalues()),) + return s + +class invalid_submit(Exception): pass +def validate(pred, msg): + if not pred: raise invalid_submit(msg) + +class Pool(db.Model): + descrip = db.StringProperty() + contribs = db.BlobProperty() + +def mainPage(page, error = ''): + page.response.out.write(r""" + <html> + <body> + <p style="color: red">%(error)s</p> + <p> + To create a pool, just enter a description of what it's + for here, and you will be provided a URL that you can + distribute to the people you want to participate in the + pool. + </p> + <form action="/create" method="post"> + <div><input type="text" name="descrip"/></div> + <div><input type="submit" width="50" value="Create Pool"></div> + </form> + <p>Examples:</p> + <ul> + <li>Birthday gift for Amy (aiming for $180 Rock Band set)</li> + <li>Holiday vacation trip</li> + </ul> + </body> + </html>""" % {'error': error}) + +class MainPage(webapp.RequestHandler): + def get(self): + mainPage(self) + +class CreatePage(webapp.RequestHandler): + def post(self): + if self.request.get('descrip').strip() == '': + mainPage(self, 'Must specify a description') + else: + pool = Pool() + pool.descrip = self.request.get('descrip') + pool.contribs = cPickle.dumps({}) + pool.put() + self.response.out.write(r""" + <html> + <body> + <p> + Congrats! You've successfully created a new money pool. + Send this URL out to anyone you're inviting to + participate: + </p> + <p> + <a + href="/pool?key=%(key)s">http://pitch-in.appspot.com/pool?key=%(key)s</a> + </p> + </body> + </html>""" % {'key': pool.key()}) + +class PoolPage(webapp.RequestHandler): + def getPool(self): + return db.get(db.Key(self.request.get('key'))) + def get(self, error = ''): + pool = self.getPool() + self.response.out.write(r""" + <html> + <body> + <p style="color: red">%(error)s</p> + <p>Description of pool: %(descrip)s</p> + %(contribs)s + <p> + To add or update your own contribution to the pool, enter + the following information: + </p> + <form action="/pool" method="post"> + <input type="hidden" name="key" value="%(key)s"/> + <div> + <label name="name">Name:</label> + <input type="text" name="name"/> + </div> + <div> + <label name="amount">Amount:</label> + $<input type="text" name="amount"/>.00 + </div> + <div><input type="submit" value="Enter the pool!"/></div> + </form> + </body> + </html> + """ % {'error': error, + 'descrip': cgi.escape(pool.descrip), + 'contribs': showContribs(pool), + 'key': cgi.escape(self.request.get('key'))}) + def post(self): + try: + validate(self.request.get('name').strip() != '', + 'You must enter a name.') + try: amount = int(self.request.get('amount')) + except ValueError: raise invalid_submit('You must enter an integer dollar amount.') + except invalid_submit, ex: + self.get(ex) + else: + pool = self.getPool() + contribs = getContribs(pool) + contribs[self.request.get('name')] = amount + pool.contribs = cPickle.dumps(contribs) + pool.put() + self.redirect('/pool?key=%s' % pool.key()) + +application = webapp.WSGIApplication([('/', MainPage), + ('/create', CreatePage), + ('/pool', PoolPage)], + debug = True) + +def main(): run_wsgi_app(application) +if __name__ == "__main__": main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |