Posts Tagged ‘put’

Google App Engine: Fun with the Datastore

May 12, 2008

The Datastore is the App Engine online database for use by your online application. So not only does Google host your python code but as you accumulate application data and format it as a database they will also manage that data. The last post had adding a form to collect user seed values for the online randomish number generator. Now it’s time to store those seed values in the 1boldlentil application Datastore. Start with adding this line to import the app engine database functionality:

from google.appengine.ext import db

After this a database of seed values should be declared before the main page. This database is pretty simple and at this point consists of the seed value as an integer and the date for that entry. Google has their worked example in their documentation but you have to dig to get a list of types and properties which is here.

class SeedValues(db.Model):
  theSeed = db.IntegerProperty()
  date = db.DateTimeProperty(auto_now_add=True)

Finally we’ll use the two page structure of a simple form to collect a seed value followed by a page to print the values generated with that seed value. So for version five of the application we’ll have a /5s to hold the form and /5 to show the results. The /5s is identical to the /4s from the previous post so it won’t be shown here. The results created with /5 are different though. The top part of this class declaration has theSeed declared and assigns it to the seed value from the form. This value is then put into the database with thisSeed.put().

class version5Page(webapp.RequestHandler):
  def post(self):
    userSeed = int(cgi.escape(self.request.get('theSeed')))
    thisSeed = SeedValues()
    thisSeed.theSeed = userSeed
    thisSeed.put()
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write('')
    self.response.out.write('')
    self.response.out.write('theSeed=');
    self.response.out.write(userSeed)
    self.response.out.write('<p>');
    self.response.out.write('<a href=https://boldlentil.wordpress.com ')
    self.response.out.write('target="_blank">')
    self.response.out.write('<b>Randomish Numbers</b></a><p>')
    random.seed(userSeed)
    stop = 25
    i = 1
    while i <= stop:
      self.response.out.write(random.random())
      self.response.out.write('<br>')
      i = i + 1
    self.response.out.write('<p><b>Last 10 seed values:</b> ')
    LastTenSeeds = db.GqlQuery("SELECT * FROM SeedValues ORDER BY date DESC LIMIT 10")
    for aSeed in LastTenSeeds:
      self.response.out.write(cgi.escape(str(aSeed.theSeed)))
      self.response.out.write(' ')

After the now too familiar for loop to print out the resulting randomish numbers is a Google Query Language or GQL query for the last ten seed values which are then printed to the screen. Once again pretty simple. Define the specific database class to be used by the application, create an instance and fill it and then access with queries.

The result is: here.

Note that the types and properties also has built in support for GPS coordinates, IM and ratings. No the next post will not be about letting you readers rate each other randomish number generator seed values. But it’s tempting.