Some of the information here may be outdated, please check the book instead
[edit]

As you know web2py runs on almost any architecture and supports transparently many DB back-ends (SQLite, MySQL, PostgreSQL, Oracle, MSSQL, Informix, FireBird, Ingres, DB2).

Much of web2py runs on GAE too. Here we will try to clarify what runs, what does not run, and what to do about it.

The main issue with GAE is the absence of file system access. This means you cannot store session, tickets and uploaded files on the filesystem. They have to go in the databases.

GAE provides its own database based on BigTables. The Web2py ORM can access the GAE database and automatically generates GQL (Google Query Language) queries, but GQL has limitations compared with SQL and reflect in limitations in the types of ORM queries one can build.

Example

Consider the following application that DOES NOT RUN on GAE:

model db.py:

db=DAL("sqlite://db.db")

db.define_table('image',
  Field('title'),
  Field('filename','upload'))

db.image.title.requires=IS_NOT_EMPTY()

controller default.py

def index():
    form=SQLFORM(db.image)
    if form.accepts(request.vars): response.flash='record inserted'
    images=db().select(db.image.ALL,orderby=db.image.title)
    return dict(form=form,images=images)

def edit():
    image=db(db.image.id==request.args[0]).select()[0]
    form=SQLFORM(db.image,image,upload=URL(r=request,f='download'))
    if form.accepts(request.vars): redirect(URL(r=request,f='index'))
    return dict(form=form)

def download():
    return response.download(request,db)

view default/index.html

{{extend 'layout.html'}}
<h2>Post image</h2>
{{=form}}

<h2>Images</h2>
<ul>
{{for image in images:}}
   <li>{{=image.title}}
   [<a href="{{=URL(r=request,f='edit',args=[image.id])}}">edit</a>]
   <br/>
   <img width="200px" src="{{=URL(r=request,f='download',args=[image.filename])}}"/>
   </li>
{{pass}}
</ul>

view default/edit.html

{{extend 'layout.html'}}
<h2>Edit image</h2>
{{=form}}

It allows visitors to upload images with a title and to [edit] those title, images. All images are shown in the index page.

This app does not run because:

  • uses sqlite
  • uses file based sessions

Porting to GAE

In order to port it to GAE you need to change

db=DAL("sqlite://db.db")

into

try:
    db=DAL("sqlite://db.db")
except:
    db=DAL('gae')
    session.connect(request,response,db=db)

The last line tells web2py to store sessions in DB. A web2py_session table is created automatically.

That's it! Code written for GAE also works without GAE since it is valid web2py code.

On Complex Queries

These queries will work on GAE

db(db.image.id==1).select()
db(db.image.id==1).delete()
db(db.image.id==1).update(title='title')

db(db.image.title<'z').select()
db(db.image.title>'a').delete()
db(db.image.title=='test').update(title='title')

db((db.image.title<'z')&(db.image.title>'a')).select(db.image.ALL,sortby=db.image.title,limitby=(0,10))

but on GAE you WILL NOT BE ABLE to:

  • use OR (operator "|") in queries
  • use the not operators ("!=" and "~")
  • use LIKE (operator "like") and IN (operator "belongs")
  • use groupby
  • mix queries by id and queries by other fields like in (db.image.id==id)&(db.image.title=='')
  • manipulate date, time, datetime
  • select anything other than db.table.ALL
  • do JOIN; i.e. build queries involving more than one table

What else does not work

Admin also does not work, it is would re read only

© 2008-2010 by Massimo Di Pierro - All rights reserved - Powered by web2py - design derived from a theme by the earlybird
The content of this book is released under the Artistic License 2.0 - Modified content cannot be reproduced.