How can I list app engine datastore parent values with python? -
not sure if on title should have said parent values or ancestors, here trying do.
i learning use python in google app engine. while reviewing content of this page in guestbook tutorial, wondering if enhance listing possible guestbooks created.
to give bit of context, how sample code works. application renders page allows create guestbook entry, switching/creating new guestbooks current or future entries. thought simple add ability list stored guestbooks dynamically generate list of links see each one.
i thought simple, trying , can't figure out. how query datastore give me list of "guestbooks" available can build links dynamically? if guestbook called guestbook_2 url looks "?guestbook_name=guestbook_2".
here code of application (i added string "insert list here" want addd list of links mentioned):
import cgi import urllib google.appengine.api import users google.appengine.ext import ndb import webapp2 main_page_footer_template = """\ <form action="/sign?%s" method="post"> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><input type="submit" value="sign guestbook"></div> </form> <hr> <form>guestbook name (you can create own guestbook here): <input value="%s" name="guestbook_name"> <input type="submit" value="switch"> </form> <a href="%s">%s</a> <br><br> list of guestbooks: %s </body> </html> """ default_guestbook_name = 'default_guestbook' # set parent key on 'greetings' ensure in same # entity group. queries across single entity group consistent. # however, write rate should limited ~1/second. def guestbook_key(guestbook_name=default_guestbook_name): """constructs datastore key guestbook entity guestbook_name.""" return ndb.key('guestbook', guestbook_name) class greeting(ndb.model): """models individual guestbook entry author, content, , date.""" author = ndb.userproperty() content = ndb.stringproperty(indexed=false) date = ndb.datetimeproperty(auto_now_add=true) class mainpage(webapp2.requesthandler): def get(self): self.response.write('<html><body>') guestbook_name = self.request.get('guestbook_name', default_guestbook_name) # ancestor queries, shown here, consistent high # replication datastore. queries span entity groups # consistent. if omitted ancestor query there # slight chance greeting had been written not # show in query. greetings_query = greeting.query( ancestor=guestbook_key(guestbook_name)).order(-greeting.date) greetings = greetings_query.fetch(10) greeting in greetings: if greeting.author: self.response.write( '<b>%s</b> wrote:' % greeting.author.nickname()) else: self.response.write('an anonymous person wrote:') self.response.write('<blockquote>%s</blockquote>' % cgi.escape(greeting.content)) if users.get_current_user(): url = users.create_logout_url(self.request.uri) url_linktext = 'logout' else: url = users.create_login_url(self.request.uri) url_linktext = 'login' # write submission form , footer of page sign_query_params = urllib.urlencode({'guestbook_name': guestbook_name}) self.response.write(main_page_footer_template % (sign_query_params, cgi.escape(guestbook_name), url, url_linktext, "insert list here" )) class guestbook(webapp2.requesthandler): def post(self): # set same parent key on 'greeting' ensure each greeting # in same entity group. queries across single entity group # consistent. however, write rate single entity group # should limited ~1/second. guestbook_name = self.request.get('guestbook_name', default_guestbook_name) greeting = greeting(parent=guestbook_key(guestbook_name)) if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() query_params = {'guestbook_name': guestbook_name} self.redirect('/?' + urllib.urlencode(query_params)) application = webapp2.wsgiapplication([ ('/', mainpage), ('/sign', guestbook), ], debug=true)
just store guestbook name somewhere, or easiest thing can fetch greeting's key parent, like
guestbooks = [greeting.key().parent().name() greeting in greeting.all(keys_only=true)]
Comments
Post a Comment