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.
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
):
42 self
.response
.out
.write('<html><body>')
43 guestbook_name
=self
.request
.get('guestbook_name')
45 greetings
= db
.GqlQuery("SELECT * "
47 "WHERE ANCESTOR IS :1 "
48 "ORDER BY date DESC LIMIT 10",
49 guestbook_key(guestbook_name
))
51 for greeting
in greetings
:
53 self
.response
.out
.write('<b>%s</b> wrote:' % greeting
.author
.nickname())
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>
65 <form>Gustbook name: <input value="%s" name="guestbook_name">
66 <input type="submit" value="switch"></form>
68 </html>""" % (urllib
.urlencode({'guestbook_name': guestbook_name
}),
69 cgi
.escape(guestbook_name
)))
72 class Guestbook(webapp
.RequestHandler
):
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')
82 self
.redirect('/?' + urllib
.urlencode({'guestbook_name': guestbook_name
}))
85 application
= webapp
.WSGIApplication([
92 wsgiref
.handlers
.CGIHandler().run(application
)
95 if __name__
== '__main__':