Fernando Correia’s Weblog

July 19, 2008

Flex client updating objects in Google App Engine

Filed under: Software Development — Fernando Correia @ 10:31 pm
Tags: , ,

I am continuing to learn how to integrate Flex to Google App Engine. Now my test project is able to update objects, in addition to inserting and retrieving them.

On the server side, this is done by the update service operation:

def update(self, project):
    logging.debug('udpate %s' % (project))
    existing_project = Project.get(project._key)
    existing_project.name = project.name
    return Project.get(project._key)

There is also a new save operation that decides between insert and update depending on the state of the object. If the object was read before, it is updated. Otherwise, it is inserted:

def save(self, project):
    logging.debug('save %s' % (project))
    if hasattr(project, '_key') and project._key != None:
        return self.update(project)
        return self.insert(project)

The App Engine model is also saving the date and time each object was last modified:

class Project(db.Model):
    # ...
    modified_at = db.DateTimeProperty(auto_now=True)

On the client side, the save operation is called after an object is prepared. If the object was being edited, its key was saved and so the service will know it must be updated. If it is a new object, the key will be null so the service will insert a new object.

public function submitProject():void {
    var newProject:Object = new Object();
    newProject.code = project_code.text;
    newProject.name = project_name.text;
    newProject._key = project._key;
    gateway.call("ProjectService.save", new Responder(null, onFault), newProject);

The _key property is a very important piece of information in this context. So I patched the PyAMF library to include it within every object it sends to the client. There is a ticket for that on the PyAMF project.

def writeObjectAMF(self, obj, args, kwargs, remove):
    Writes an object that has already been prepared by writeObjectAMF0 or writeObjectAMF3.
        obj._key = str(obj.key())
        obj._key = None
    self.writeObject(obj, *args, **kwargs)
    del obj._key
    if remove:
        self.context.class_aliases[obj.__class__] = None

All the source code for this project is hosted in github. Please be aware that I am sharing what I find out as I learn. I am sure there are better ways of using this tools. I hope to learn them. Comments are welcome.


1 Comment »

  1. PyAMF and Google App Engine…

    After Google’s announcement on Monday we received a lot of questions about running PyAMF on the Google App Engine so we decided to start working on a tutorial. This is a work in progress and might require some updates to the library. We’re…

    Trackback by PyAMF blog — July 20, 2008 @ 5:49 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: