|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START all] |
| 16 | +import cgi |
| 17 | +import urllib |
| 18 | + |
| 19 | +import webapp2 |
| 20 | + |
| 21 | +from google.appengine.ext import ndb |
| 22 | + |
| 23 | + |
| 24 | +# [START greeting] |
| 25 | +class Greeting(ndb.Model): |
| 26 | + """Models an individual Guestbook entry with content and date.""" |
| 27 | + content = ndb.StringProperty() |
| 28 | + date = ndb.DateTimeProperty(auto_now_add=True) |
| 29 | +# [END greeting] |
| 30 | + |
| 31 | +# [START query] |
| 32 | + @classmethod |
| 33 | + def query_book(cls, ancestor_key): |
| 34 | + return cls.query(ancestor=ancestor_key).order(-cls.date) |
| 35 | + |
| 36 | + |
| 37 | +class MainPage(webapp2.RequestHandler): |
| 38 | + def get(self): |
| 39 | + self.response.out.write('<html><body>') |
| 40 | + guestbook_name = self.request.get('guestbook_name') |
| 41 | + ancestor_key = ndb.Key("Book", guestbook_name or "*notitle*") |
| 42 | + greetings = Greeting.query_book(ancestor_key).fetch(20) |
| 43 | + |
| 44 | + for greeting in greetings: |
| 45 | + self.response.out.write('<blockquote>%s</blockquote>' % |
| 46 | + cgi.escape(greeting.content)) |
| 47 | +# [END query] |
| 48 | + |
| 49 | + self.response.out.write(""" |
| 50 | + <form action="/sign?%s" method="post"> |
| 51 | + <div><textarea name="content" rows="3" cols="60"></textarea></div> |
| 52 | + <div><input type="submit" value="Sign Guestbook"></div> |
| 53 | + </form> |
| 54 | + <hr> |
| 55 | + <form>Guestbook name: <input value="%s" name="guestbook_name"> |
| 56 | + <input type="submit" value="switch"></form> |
| 57 | + </body> |
| 58 | + </html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}), |
| 59 | + cgi.escape(guestbook_name))) |
| 60 | + |
| 61 | + |
| 62 | +# [START submit] |
| 63 | +class SubmitForm(webapp2.RequestHandler): |
| 64 | + def post(self): |
| 65 | + # We set the parent key on each 'Greeting' to ensure each guestbook's |
| 66 | + # greetings are in the same entity group. |
| 67 | + guestbook_name = self.request.get('guestbook_name') |
| 68 | + greeting = Greeting(parent=ndb.Key("Book", guestbook_name or "*notitle*"), |
| 69 | + content=self.request.get('content')) |
| 70 | + greeting.put() |
| 71 | +# [END submit] |
| 72 | + self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name})) |
| 73 | + |
| 74 | + |
| 75 | +app = webapp2.WSGIApplication([ |
| 76 | + ('/', MainPage), |
| 77 | + ('/sign', SubmitForm) |
| 78 | +]) |
| 79 | +# [END all] |
0 commit comments