Personbook is a simple Google App Engine application. The end-user enters the names of people and the system stores person records in a database. The current list of people is displayed. You can run the sample on any computer that has App Engine installed. If you are using a USF Computer Science account, here are the instructions: In-Class Assignment I 1. There are four files necessary for the sample. All are attached, except for the index.html (Goggle Sites doesn't allow HTML attachments). So download the three attachments then copy/past the code for index.html below. Put all the files in a directory named 'personbook'. 2. From the directory above personbook, run: dev_appserver personbook 3. Open a browser to http://localhost:8080 4. Once you get the application to run as is, try to add a field 'age' to each Person object. You'll need to
You can check out the entire Google Datastore API at: http://code.google.com/appengine/docs/datastore/ The Sample Codeapp.yaml file -- This is a config file that sets up the application and specifies the controller code.application: personbook version: 1 runtime: python api_version: 1 handlers: - url: /.* script: personbook_controller.py ******************************************* personbook is the name of the application and must match the name of the application you provide when you upload the app. to Google (appcfg). Note that this name need not be the same as the directory where your local files live. The script line just specifies which python program file will handle web requests. In this case, all files (.*) are handled by personbook_controller.py index.html -- this is the view<html> <body> <h2>People</h2> {% for person in people %} <b>{{person.first}} {{person.last}}</b><br/> {% endfor %} <h2>Please add a new person</h2> <form action="/on_add" method="post"> <div>first:<textarea name="first" rows="1" cols="60"></textarea></div> <div>last: <textarea name="last" rows="1" cols="60"></textarea></div> <div><input type="submit" value="Add Person"></div> </form> </body> </html> ******************************************* index.html is the HTML for the main page of your web app. In this example, its the only page. The scripting is Google AppEngine's Django-like scripting. Everything in {% ... %} is scripting code, and template variables are enclosed in {{ ... }}. 'people' is the only template variable sent from the controller. The scripting code defines a variable 'person' which is the loop placeholder for each item in teh 'people' list. If the user entered 'Joe' and 'Williams' and then clicked "Add Person", the controller would be sent the request: on_add?first=Joe&last=Williams person.py -- this is the model, the code that handles the persistent datafrom google.appengine.ext import db class Person(db.Model): first=db.StringProperty() last=db.StringProperty() ****************************************** class Person inherits from db.Model, which makes it a class representing a database table. It has two persistent fields, first and last personbook_controller.py -- the controller code that accesses the database and invokes pages (views)import cgi **************************************************************************************************import os from google.appengine.ext.webapp import template from google.appengine.ext import db from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app # person module has our model-- our Person database object. from person import Person # main page appears on load class MainPage(webapp.RequestHandler): def get(self): # get all the persons in the database people=db.GqlQuery("SELECT * FROM Person") # set up the template_values with the list of people returned. template_values= {'people':people} # render the page using the template engine path = os.path.join(os.path.dirname(__file__),'index.html') self.response.out.write(template.render(path,template_values)) # this class handles the input (on_add event). We just grab the first and last from input form and create/put a # new Person record. class PersonApp(webapp.RequestHandler): def post(self): person=Person() person.first=self.request.get('first') person.last=self.request.get('last') person.put() # causes database store self.redirect('/') # just show the homepage again. # create this global variable that represents the application and specifies which class # should handle each page in the site application = webapp.WSGIApplication( # MainPage handles the home load [('/', MainPage), # when user clicks on add button, we call on_add action # check out index.html to see where on_add gets submitted ('/on_add', PersonApp)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() The application variable created sets the action-Class mappings that tell Python which class should 'handle' each request. In this case, MainPage handles the home page request (personbook.appspot.com/) and PersonApp handles the on_add request (personbook.appspot.com?first=Joe&last=Willams) MainPage's get handler just gets the list of people from the database and puts them into the template value with the key 'people'. Remember, 'people' is the list that the HTML file expects. PersonApp's post handler gets the parameters from the form (first and last) and sets those fields in a new persistent Person object. It then calls 'put' to write the object to the database. Finally, it redirects so that the main page is rendered again (this causes MainPage's get controller to be called). In-Class Assignment IIModify your PersonBook application in the following way:
|