update to reflect API changes
[gae-samples.git] / guestbook_highrep / guestbook.py
blob0c4f7f819666b777a4e20a22325122825263d30d
1 #!/usr/bin/env python
3 # Copyright 2007 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 import cgi
18 import datetime
19 import urllib
20 import wsgiref.handlers
22 from google.appengine.ext import db
23 from google.appengine.api import users
24 from google.appengine.ext import webapp
26 # We set a parent key on the 'Greetings' to ensure that they are all in the same
27 # entity group. Queries across the single entity group will be consistent.
28 # However, the write rate should be limited to ~1/second.
30 def guestbook_key(guestbook_name=None):
31 return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')
34 class Greeting(db.Model):
35 author = db.UserProperty()
36 content = db.StringProperty(multiline=True)
37 date = db.DateTimeProperty(auto_now_add=True)
40 class MainPage(webapp.RequestHandler):
41 def get(self):
42 self.response.out.write('<html><body>')
43 guestbook_name=self.request.get('guestbook_name')
45 greetings = db.GqlQuery("SELECT * "
46 "FROM Greeting "
47 "WHERE ANCESTOR IS :1 "
48 "ORDER BY date DESC LIMIT 10",
49 guestbook_key(guestbook_name))
51 for greeting in greetings:
52 if greeting.author:
53 self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
54 else:
55 self.response.out.write('An anonymous person wrote:')
56 self.response.out.write('<blockquote>%s</blockquote>' %
57 cgi.escape(greeting.content))
59 self.response.out.write("""
60 <form action="/sign?%s" method="post">
61 <div><textarea name="content" rows="3" cols="60"></textarea></div>
62 <div><input type="submit" value="Sign Guestbook"></div>
63 </form>
64 <hr>
65 <form>Gustbook name: <input value="%s" name="guestbook_name">
66 <input type="submit" value="switch"></form>
67 </body>
68 </html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}),
69 cgi.escape(guestbook_name)))
72 class Guestbook(webapp.RequestHandler):
73 def post(self):
74 guestbook_name=self.request.get('guestbook_name')
75 greeting = Greeting(parent=guestbook_key(guestbook_name))
77 if users.get_current_user():
78 greeting.author = users.get_current_user()
80 greeting.content = self.request.get('content')
81 greeting.put()
82 self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))
85 application = webapp.WSGIApplication([
86 ('/', MainPage),
87 ('/sign', Guestbook)
88 ], debug=True)
91 def main():
92 wsgiref.handlers.CGIHandler().run(application)
95 if __name__ == '__main__':
96 main()