pyAMF - Communication with Django from Flex

Up until this point I have been using Django AMF for my remote services. It was working fairly well on my windows machine, but when I tried to deploy my application to my Ubuntu slice, it started kicking up weird errors. I'd spoken with one of the pyAMF developers in a Reddit thread, and they seemed like a motivated group, so I decided to give pyAMF a go.

*sigh*

Now I have new errors to deal with. Not a problem though, signed up to their mailing list and Thijs and Nick were extremely helpful in hunting down the bugs and squishing them dead. pyAMF is still in alpha, but they are moving over to beta soon. It is working well now, so give it a try if you need communication between Flash/Flex/AIR and Python.

The Gateway

They have a couple of examples on the pyAMF site, but here are a couple things that I learned. In my amfgateway.py, which is the equivelant of a Django views.py, I had to register my models so that pyAMF would send and recieve typed custom objects. It ended up looking something like this:

amfgateway.py

import pyamf
from pyamf.remoting.djangogateway import DjangoGateway
from django.contrib.auth.models import Userpyamf.register_class( User, 'django.contrib.auth.models.User' )
 
def get_users(request):
    return User.objects.all()
 
sessionGateway = DjangoGateway({'ve.get_users'  : get_users})  # could include other functions as well

I am using the Django auth structure to authenticate users in my application, so that is the bare bones of what you need to get that going. Here's what the urls.py looks like:

urls.py

from django.conf.urls.defaults import *urlpatterns = patterns(' ',
     (r'^ve/gateway/', 've.clients.amfgateway.sessionGateway'),
)

As you can see, it is extremely simple. It is setup in the same fashion you would use to link up a Django view.

In Flex, I've set up a RemoteServiceDelegate which is extending the Seasar RemotingService [the site is in japanese]. This is actually a holdover from Django AMF, but it works well, so I am going to continue to use it. Here's the code:

RemoteServiceDelegate.as

package com.visualempathy.session.model.delegate
{
 import mx.controls.Alert;
 import mx.rpc.events.FaultEvent;	import org.seasar.flex2.rpc.remoting.RemoteService;
 
  public class RemoteServiceDelegate extends RemoteService
  {
 	public static const gatewayURL:String = "http://myserver.com/ve/gateway/"
 	public function RemoteServiceDelagate()
        {
 		super();
 		useAMF0 = false;
 		gatewayUrl = gatewayURL;
 		destination = 've';
                addEventListener(FaultEvent.FAULT, handleDataFault);
 	}
 
 	private function handleDataFault( fe:FaultEvent ):void
 	{
 		Alert.show( fe.fault.faultDetail );
 	}
 }
}

Using this setup, the destination in the remoting service is used like this:

 sessiongateway = DjangoGateway({'ve.get_users' : ve.get_users
've.get_orders': ve.get_orders})

where 've', the destination in the RemotingService class precedes the method name in the DjangoGateway argument. This can be added to as much as needed as demonstrated above.

1 Response to “pyAMF - Communication with Django from Flex”


  1. 1 Jeff D.

    Thanks for your post, I’m having to decide about a django amf implementation and was very happy to find your post.

    I was planning on PyAMF only because Django AMF was done by a group in Japan and I thought language might be an issue if I get into trouble.

Leave a Reply