Advertisement
Guest User

gaedatastore

a guest
May 2nd, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import cgi
  2. import datetime
  3. import urllib
  4. import wsgiref.handlers
  5.  
  6. from google.appengine.ext import db
  7. from google.appengine.api import users
  8. from google.appengine.ext import webapp
  9. from google.appengine.ext.webapp.util import run_wsgi_app
  10.  
  11. class Greeting(db.Model):
  12.     """Models an individual Guestbook entry with an author, content and date."""
  13.     print 'Greeting(db.Model)'
  14.     author = db.UserProperty()
  15.     content = db.StringProperty(multiline=True)
  16.     date = db.DateTimeProperty(auto_now_add=True)
  17.  
  18. def guestbook_key(guestbook_name=None):
  19.     """Constructs a Datastore key for a Guestbook entity with guestbook_name."""
  20.     print 'guestbook_key function'
  21.     return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')
  22.  
  23. class MainPage(webapp.RequestHandler):
  24.     def get(self):
  25.         print 'MainPage  class'
  26.         self.response.out.write('<html><body>')
  27.         guestbook_name=self.request.get('guestbook_name')
  28.        
  29.         # Ancestor Queries, as shown here, are strongly consistent with the High
  30.         # Replication Datastore. Queries that span entity groups are eventually
  31.         # consistent. If we omitted the ancestor from this query there would be a
  32.         # slight chance that Greeting that had just been written would not show up
  33.         # in a query.
  34.         greetings = db.GqlQuery("SELECT * "
  35.                                 "FROM Greeting  "
  36.                                 "WHERE ANCESTOR IS :1 "
  37.                                 "ORDER BY date DESC LIMIT 10",
  38.                                 guestbook_key(guestbook_name))
  39.  
  40.         for greeting in greetings:
  41.             if greeting.author:
  42.                 self.response.out.write(
  43.                     '<b>%s</b> wrote:' % greeting.author.nickname())
  44.             else:
  45.                 self.response.write('An anonymous person wrote:')
  46.             self.response.out.write('<blockquote>%s</blockquote>' % cgi.escape(greeting.content))
  47.            
  48.             self.response.out.write("""
  49.                 <form action="/sign?%s" method="post">
  50.                     <div><textarea name="content" rows="3" cols="60"></textarea></div>
  51.                     <div><input type="submit" value="Sign Guestbook"></div>
  52.                 </form>
  53.                 <hr>
  54.                 <form>Guestbook name: <input value="%s" name="guestbook_name">
  55.                 <input type="submit" value="switch"></form>
  56.             </body>
  57.         </html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}),
  58.                             cgi.escape(guestbook_name)))
  59.  
  60. class Guestbook(webapp.RequestHandler):
  61.     def post(self):
  62.         print 'Guestbook'
  63.         # We set the same parent key on the 'Greeting' to ensure each greeting is in
  64.         # the same entity group. Queries across the single entity group will be
  65.         # consistent. However, the write rate to a single entity group should
  66.         # be limited to ~1/second.
  67.         guestbook_name = self.request.get('guestbook_name')
  68.         greeting = Greeting(parent=guestbook_key(guestbook_name))
  69.        
  70.         if users.get_current_user():
  71.             greeting.author = users.get_current_user()
  72.        
  73.         greeting.content = self.request.get('content')
  74.         greeting.put()
  75.         self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))
  76.  
  77. application = webapp.WSGIApplication([('/', MainPage), ('/sign', Guestbook)], debug=True)
  78.  
  79. def main():
  80.     print 'main function()'
  81.     run_wsgi_app(application)
  82.  
  83. if __name__ == '__main__':
  84.     print 'Start'
  85.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement