|
| 1 | +import os |
| 2 | +import urllib |
| 3 | + |
| 4 | +from google.appengine.api import users |
| 5 | +from google.appengine.ext import ndb |
| 6 | + |
| 7 | +import jinja2 |
| 8 | + |
| 9 | +import webapp2 |
| 10 | + |
| 11 | +JINJA_ENVIRONMENT = jinja2.Environment( |
| 12 | + loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), |
| 13 | + extensions=['jinja2.ext.autoescape']) |
| 14 | + |
| 15 | +DEFAULT_GUESTBOOK_NAME = 'default_guestbook' |
| 16 | + |
| 17 | + |
| 18 | +# We set a parent key on the 'Greetings' to ensure that they are all in the |
| 19 | +# same entity group. Queries across the single entity group will be consistent. |
| 20 | +# However, the write rate should be limited to ~1/second. |
| 21 | + |
| 22 | +def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME): |
| 23 | + """Constructs a Datastore key for a Guestbook entity with guestbook_name""" |
| 24 | + return ndb.Key('Guestbook', guestbook_name) |
| 25 | + |
| 26 | + |
| 27 | +class Greeting(ndb.Model): |
| 28 | + """Models an individual Guestbook entry with author, content, and date.""" |
| 29 | + author = ndb.UserProperty() |
| 30 | + content = ndb.StringProperty(indexed=False) |
| 31 | + date = ndb.DateTimeProperty(auto_now_add=True) |
| 32 | + |
| 33 | + |
| 34 | +class MainPage(webapp2.RequestHandler): |
| 35 | + |
| 36 | + def get(self): |
| 37 | + guestbook_name = self.request.get('guestbook_name', |
| 38 | + DEFAULT_GUESTBOOK_NAME) |
| 39 | + greetings_query = Greeting.query( |
| 40 | + ancestor=guestbook_key(guestbook_name)).order(-Greeting.date) |
| 41 | + greetings = greetings_query.fetch(10) |
| 42 | + |
| 43 | + if users.get_current_user(): |
| 44 | + url = users.create_logout_url(self.request.uri) |
| 45 | + url_linktext = 'Logout' |
| 46 | + else: |
| 47 | + url = users.create_login_url(self.request.uri) |
| 48 | + url_linktext = 'Login' |
| 49 | + |
| 50 | + template_values = { |
| 51 | + 'greetings': greetings, |
| 52 | + 'guestbook_name': urllib.quote_plus(guestbook_name), |
| 53 | + 'url': url, |
| 54 | + 'url_linktext': url_linktext, |
| 55 | + } |
| 56 | + |
| 57 | + template = JINJA_ENVIRONMENT.get_template('index.html') |
| 58 | + self.response.write(template.render(template_values)) |
| 59 | + |
| 60 | + |
| 61 | +class Guestbook(webapp2.RequestHandler): |
| 62 | + |
| 63 | + def post(self): |
| 64 | + # We set the same parent key on the 'Greeting' to ensure each Greeting |
| 65 | + # is in the same entity group. Queries across the single entity group |
| 66 | + # will be consistent. However, the write rate to a single entity group |
| 67 | + # should be limited to ~1/second. |
| 68 | + guestbook_name = self.request.get('guestbook_name', |
| 69 | + DEFAULT_GUESTBOOK_NAME) |
| 70 | + greeting = Greeting(parent=guestbook_key(guestbook_name)) |
| 71 | + |
| 72 | + if users.get_current_user(): |
| 73 | + greeting.author = users.get_current_user() |
| 74 | + |
| 75 | + greeting.content = self.request.get('content') |
| 76 | + greeting.put() |
| 77 | + |
| 78 | + query_params = {'guestbook_name': guestbook_name} |
| 79 | + self.redirect('/?' + urllib.urlencode(query_params)) |
| 80 | + |
| 81 | + |
| 82 | +application = webapp2.WSGIApplication([ |
| 83 | + ('/', MainPage), |
| 84 | + ('/sign', Guestbook), |
| 85 | +], debug=True) |
0 commit comments