improve handling of utf-8 request params
[gae-samples.git] / voterlator / main.py
blobee34cd37b0aa4cdb75a644e2ff09fbb2945e77aa
1 #!/usr/bin/python2.4
3 # Copyright (C) 2011 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """An very simple app using pull queues to tally votes."""
19 __author__ = 'nverne@google.com (Nicholas Verne)'
21 import os
22 import time
24 from google.appengine.api import memcache
25 from google.appengine.api import taskqueue
26 from google.appengine.ext import db
27 from google.appengine.ext import webapp
28 from google.appengine.ext.webapp import template
29 from google.appengine.ext.webapp.util import run_wsgi_app
31 LANGUAGES = ['Java', 'Go', 'C++', 'Perl', 'Python']
33 class Tally(db.Model):
34 """Simple counting model."""
35 count = db.IntegerProperty(default=0, required=True)
37 @classmethod
38 def increment_by(cls, key_name, count):
39 """Increases a Tally's count. Should be run in a transaction."""
40 tally = cls.get_by_key_name(key_name)
41 if tally is None:
42 tally = cls(key_name=key_name)
43 tally.count += count
44 tally.put()
47 class VoteHandler(webapp.RequestHandler):
48 """Handles adding of vote tasks."""
50 def get(self):
51 """Displays the voting form."""
52 self.render_template('index.html', {'tallies': self.get_tallies(),
53 'LANGUAGES': LANGUAGES})
55 def post(self):
56 """Adds tasks to votes queue if ugliest is valid."""
57 ugliest = self.request.get('ugliest')
59 if ugliest and ugliest in LANGUAGES:
60 q = taskqueue.Queue('votes')
61 q.add(taskqueue.Task(payload=ugliest, method='PULL'))
62 self.redirect('/')
64 def get_tallies(self):
65 """Fetches tallies from memcache if possible, otherwise from datastore."""
66 tallies = memcache.get('tallies')
67 if tallies is None:
68 tallies = Tally.all().fetch(len(LANGUAGES))
69 memcache.set('tallies', tallies, time=5)
70 return tallies
72 def render_template(self, name, template_args):
73 """Renders a named django template."""
74 path = os.path.join(os.path.dirname(__file__), 'templates', name)
75 self.response.out.write(template.render(path, template_args))
78 class TallyHandler(webapp.RequestHandler):
79 """Pulls tasks from the vote queue."""
81 def store_tallies(self, tallies):
82 """Updates the tallies in datastore."""
83 for key_name, count in tallies.iteritems():
84 db.run_in_transaction(Tally.increment_by, key_name, count)
86 def post(self):
87 """Leases vote tasks, accumulates tallies and stores them."""
88 q = taskqueue.Queue('votes')
89 # Keep leasing tasks in a loop. When the task fails due to
90 # deadline, it should be retried.
91 while True:
92 tasks = q.lease_tasks(300, 1000)
93 if not tasks:
94 # Let the retry parameters of the queue cause this
95 # task to be rescheduled.
96 self.error(500)
97 return
99 tallies = {}
100 # accumulate tallies in memory
101 for t in tasks:
102 tallies[t.payload] = tallies.get(t.payload, 0) + 1
104 self.store_tallies(tallies)
105 q.delete_tasks(tasks)
107 class StartHandler(webapp.RequestHandler):
108 """Starts some tally tasks."""
109 def get(self):
110 workers = int(self.request.get('workers', 2))
111 q = taskqueue.Queue('tally')
112 q.purge()
113 time.sleep(2)
115 if workers > 0:
116 q.add([taskqueue.Task(url='/tally',
117 countdown=x) for x in xrange(workers)])
118 self.redirect('/')
120 application = webapp.WSGIApplication([
121 ('/', VoteHandler),
122 ('/tally', TallyHandler),
123 ('/start', StartHandler),
124 ], debug=True)
127 def main():
128 run_wsgi_app(application)
130 if __name__ == '__main__':
131 main()