Commits

Miki Tebeka  committed cfeeeb9

Counter

  • Participants
  • Parent commits 60b2e43
  • Branches counter

Comments (0)

Files changed (2)

         </form>
 
         <div class="footer">
-            G'day {{ user }}
+            G'day {{ user }}, this page was viewed {{ count }} times.
         </div>
     </body>
 </html>
 '''
 from google.appengine.ext import webapp
 from google.appengine.api import users
+from google.appengine.ext import db
 
 import jinja2
 
 get_template = jinja2.Environment(
     loader=jinja2.FileSystemLoader(dirname(__file__))).get_template
 
+class Counter(db.Model):
+    '''Global counter.'''
+    key_name = 'counter-key'
+
+    count = db.IntegerProperty(default=0)
+
+def record_hit():
+    '''Record a page hit, increment counter and return count.'''
+    counter = Counter.get_or_insert(Counter.key_name)
+    counter.count += 1
+    counter.put()
+    return counter.count
+
+
 class Home(webapp.RequestHandler):
     def get(self):
         self.reply()
         env = kw.copy()
         env.update({
             'login' : self.login_html(),
-            'user' : users.get_current_user() or 'stranger'
+            'user' : users.get_current_user() or 'stranger',
+            'count' : record_hit(),
         })
         self.response.out.write(template.render(**env))