Google App Engine Oil (GAEO)

Introduction

GAEO uses MVC pattern to create a web application. It can be simplified as the following figure:

MVC pattern
Fig. The MVC pattern.

While an user goes to an URL, she sends a request to the web application. GAEO helps the application to determine which controller handles it. The controller may interact with one or more models to process the request. After processing the request, it outputs a view to the user. If the user requested a page, the response may be a HTML text.

This tutorial will show you how to build a simple Message-Wall application by using GAEO. Of course, you can deploy the application to Google App Engine.

Create a project

After installed the GAEO, be sure that you can execute the gaeo.py or gaeo script in your shell. Then, use the following command to create a GAEO-based application.

# gaeo.py msgwall
The "msgwall" project has been created.

If you saw the message like above, you'd successfully created the msgwall application.

Define the model

Open the msgwall/application/models.py file and type the following code:

#!/usr/bin/env python

from google.appengine.ext import db
from gaeo.model.datastore import DatastoreModel

class Message(DatastoreModel):
    author = db.StringProperty()
    content = db.TextProperty()
    created = db.DateTimeProperty(auto_now_add=True)

There are 3 fields in each message record: author's name, message content, and the left time. We defined 3 properties in the Message model that mapped to the fields.

Views

Open msgwall/application/templates/welcome/index.html and put the codes in:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Message Wall</title>
</head>
<body>
  <h1>Messages</h1>
  <ul>
  {% for msg in messages %}
    <li>{{ msg.author }}: {{ msg.content }}</li>
  {% endfor %}
  </ul>
  <hr>
  <form method="post" action="/message/post">
    <fieldset>
      <legend>Leave Your Message</legend>
      <ul>
        <li><label>Name: </label><input type="text" name="author"></li>
        <li><label>Message: </label><textarea rows="3" cols="50" name="message"></textarea></li>
        <li><input type="submit" value="Post"></li>
      </ul>
    </fieldset>
  </form>    
<body>
</html>

Controller

Add the following code to msgwall/application/controllers/welcome.py.

#!/usr/bin/env python

from gaeo.controller import Controller
import models

class Welcome(Controller):
    def index(self):
        self.messages = models.Message.all()

Create a file: msgwall/application/controllers/message.py with the code:

#!/usr/bin/env python

from gaeo.controller import Controller
import models

class Message(Controller):
    def post(self):
        try:
            models.Message(**self.params).put()
            self.redirect('/')
        except Exception, e:
            self.render('There\'s an error while posting message.')

Run & Deploy

Run the application as a normal AppEngine application.

# msgwall> dev_appserver.py .

To deploy to AppEngine, use appcfg.py (in Google App Engine SDK) script:

# msgwall> appcfg.py update .

© 2010 GAEO. All Rights Reserved.