|
1 #!/usr/bin/env python |
|
2 # |
|
3 # Copyright 2007 Google Inc. |
|
4 # |
|
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 |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
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. |
|
16 # |
|
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 |
|
25 class Greeting(db.Model): |
|
26 author = db.UserProperty() |
|
27 content = db.StringProperty(multiline=True) |
|
28 date = db.DateTimeProperty(auto_now_add=True) |
|
29 |
|
30 |
|
31 class MainPage(webapp.RequestHandler): |
|
32 def get(self): |
|
33 self.response.out.write('<html><body>') |
|
34 |
|
35 greetings = db.GqlQuery("SELECT * " |
|
36 "FROM Greeting " |
|
37 "ORDER BY date DESC LIMIT 10") |
|
38 |
|
39 for greeting in greetings: |
|
40 if greeting.author: |
|
41 self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname()) |
|
42 else: |
|
43 self.response.out.write('An anonymous person wrote:') |
|
44 self.response.out.write('<blockquote>%s</blockquote>' % |
|
45 cgi.escape(greeting.content)) |
|
46 |
|
47 self.response.out.write(""" |
|
48 <form action="/sign" method="post"> |
|
49 <div><textarea name="content" rows="3" cols="60"></textarea></div> |
|
50 <div><input type="submit" value="Sign Guestbook"></div> |
|
51 </form> |
|
52 </body> |
|
53 </html>""") |
|
54 |
|
55 |
|
56 class Guestbook(webapp.RequestHandler): |
|
57 def post(self): |
|
58 greeting = Greeting() |
|
59 |
|
60 if users.get_current_user(): |
|
61 greeting.author = users.get_current_user() |
|
62 |
|
63 greeting.content = self.request.get('content') |
|
64 greeting.put() |
|
65 self.redirect('/') |
|
66 |
|
67 |
|
68 application = webapp.WSGIApplication([ |
|
69 ('/', MainPage), |
|
70 ('/sign', Guestbook) |
|
71 ], debug=True) |
|
72 |
|
73 |
|
74 def main(): |
|
75 wsgiref.handlers.CGIHandler().run(application) |
|
76 |
|
77 |
|
78 if __name__ == '__main__': |
|
79 main() |