11.12.2009

Google App Engine Oil is fantastic!

To make Google App Engine , there are two ways using python or java.


Java makes virtually another language like Ruby or PHP work. So I love RoR(Ruby On Rails), I tried to use RoR on GAE looking these site.


http://jruby-rack.appspot.com/
http://rails-primer.appspot.com/


It worked! That's great but the motion was very slow so I got so many errors(on GAE s request handler has a limited amount of time to generate and return a response to a request, typically around 30 seconds).
So I decide to switch Ruby to using Python.


GAE's python allows to use Django framework which likes RoR. I was new to Pyhton & Django. But To learn python was easy. However I didn't feel happy using Django because the differences of it and RoR(I think Django is good framework though). When I was stopping to try using GAE, I met Google App Engine Oil(GAEO).


http://code.google.com/p/google-app-engine-oil/


This is another framework on GAE using python. GAEO's feature is very similar to RoR and very compact. If you liked RoR style, you like GAEO too.


By the way, though above official site has some tutorials, there were small information about GAEO now. Yesterday I post on GAEO's google group a sample code of how to make the page using form.
http://groups.google.com/group/google-app-engine-oil/browse_thread/thread/a9989a23d3a15e32


the same step & source code are here.

(0) install GAE(python) & GAEO on your PC
Install GAE sdk:
http://code.google.com/intl/en/appengine/docs/python/gettingstarted/introduction.html
Install GAEO:
http://doc.gaeo.org/tutorials/getting-started



(1) At Terminal
gaeogen scaffold Testform index new create show




(2) Make model: edt application/mode/testform.py



from google.appengine.ext import db
from gaeo.model import BaseModel, SearchableBaseModel
class Testform(BaseModel):
    name = db.StringProperty(required=True)
    secret = db.StringProperty(required=False)
    phone = db.PhoneNumberProperty(required=True)
    post_at = db.DateTimeProperty(required=True, auto_now_add=True)




(3)Make controller(at application/controller/testform.py)

import cgi
from google.appengine.ext import db
from gaeo.controller import BaseController
from model.testform import Testform
from google.appengine.ext.db import djangoforms
class TestformController(BaseController):
    def create(self):
      data = AddForm(data=self.request.POST)
      if data.is_valid:
        entity = data.save(commit=False)
        entity.update_attributes(secret= "foo")
        self.redirect('/testform')
      else:
        self.redirect('/testform/new')
    def index(self):
        query = Testform.all()
        self.result = query.fetch(limit=1000)
    def new(self):
        self.form = AddForm()
    def show(self):
        self.r = Testform.get(self.params.get('id'))
class AddForm(djangoforms.ModelForm):
  class Meta:
    model = Testform
    exclude = ['secret']




(4)Make new.html(At application/templates/new.html) : you just add
only {{ form }}



{% extends "../base.html" %}
{% block title %}TestformController#new{% endblock %}
{% block content %}
        
New Testform {{ form }}
{% endblock %}



(5)Make show.html(At application/templates/show.html) :



{% extends "../base.html" %}
{% block title %}TestformController#show{% endblock %}
{% block content %}
{{ r.name }}:{{ r.phone }}:{{ r.secret }}
{% endblock %}




Enjoy!

No comments:

Post a Comment