Wednesday, November 10, 2010

My first Google Application: Guestbook Tutorial with enhancement


Besides the standard instructions from the tutorial I have enhanced my guestbook to display timezone information and timestamps conversion according to the client's timezone per message. I also added a stylesheet file for the look and feel.

I had to fix some jQuery date formatter bugs, but no big deal. I will blog about the jQuery bugs later. You can view all the code online except the Python code.

guestbook.py:
   17 import cgi
   18 import datetime
   19 import wsgiref.handlers
   20 
   21 from google.appengine.ext import db
   22 from google.appengine.api import users
   23 from google.appengine.ext import webapp
   24 from google.appengine.ext.webapp import template
   25 
   26 class Greeting(db.Model):
   27   author = db.UserProperty()
   28   content = db.StringProperty(multiline=True)
   29   date = db.DateTimeProperty(auto_now_add=True)
   30 
   31 class MainPage(webapp.RequestHandler):
   32   def get(self):
   33     greetings = db.GqlQuery('SELECT * from Greeting '
   34                 'Order By date DESC '
   35                 'LIMIT 20 ')
   36     values = {'greetings': greetings}
   37     self.response.out.write(template.render('main.html', values))
   38 
   39 class Guestbook(webapp.RequestHandler):
   40   def post(self):
   41     #if author is empty use anonymous
   42     if self.request.get('author'):
   43       userauthor = self.request.get('author')
   44     else:
   45       userauthor = 'Anonymous'
   46     #set greet object
   47     greet=Greeting(content=self.request.get('content'), author=users.User(userauthor))
   48     greet.put()
   49     self.redirect('/')
   50 
   51 application = webapp.WSGIApplication([
   52     ('/', MainPage),
   53     ('/sign', Guestbook),
   54 ], debug=True)
   55 
   56 def main():
   57   wsgiref.handlers.CGIHandler().run(application)
   58 
   59 if __name__ == '__main__':
   60   main()

No comments:

Post a Comment