[SOLVED?] amf3.use_proxies_default and django

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[SOLVED?] amf3.use_proxies_default and django

robbedoes
Hi Nick,

>> PyAMF 0.6 has been released, I suggest upgrading to that to see if this fixes your problem.

Installing 0.6 seems the solution!
I cannot extract the info from this but I guess you can :-)

AMF Request: <Envelope amfVersion=3>
 (u'/3', <Request target=u'null'>[<RemotingMessage  body=[5] source=None timestamp=0 destination=u'meterstandenservice' clientId=u'9971bbcd-7d54-4e28-b08a-9f9fbbf89f65' headers={'DSId': u'nil', 'DSEndpoint': u'pyamf'} timeToLive=0 messageId=u'8529BB98-0A61-C61A-5145-4F772A85E5E5' operation=u'getLocationMeters' />]</Request>)
</Envelope>

AMF Response: <Envelope amfVersion=3>
 (u'/3', <Response status=/onResult><AcknowledgeMessage  body=[] timestamp=1289823398 destination=None clientId='b2f0c3a1-fbba-40d2-a930-cd92bce55b41' headers={} timeToLive=None messageId='caef5acc-d3a5-4c1a-a4e8-9306eb89322a' correlationId=u'8529BB98-0A61-C61A-5145-4F772A85E5E5' /></Response>)
</Envelope>

This is the gateway.

from django.core.exceptions import ObjectDoesNotExist
import pyamf
from pyamf import amf3
from pyamf.remoting.gateway.django import DjangoGateway
from meterstanden.models import Location, Meter

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s')

AMF_NAMESPACE = 'valueObjects'

amf3.use_proxies_default = True

pyamf.register_class(Location, '%s.Location' % AMF_NAMESPACE)
pyamf.register_class(Meter, '%s.Meter' % AMF_NAMESPACE)

def getLocations(request):
   locationList = Location.objects.all()
   return locationList

def getLocationMeters(request,id):
     try:
         location = Location.objects.get(id=id)
         return location.meter_set.all()
     except ObjectDoesNotExist:
         pass
     return id
   
services = {
   'meterstandenservice.getLocations':getLocations,
   'meterstandenservice.getLocationMeters':getLocationMeters,
}

myGateway = DjangoGateway(services, logger=logging)

Is this ok?

Cheers, Rob

                       
_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [SOLVED?] amf3.use_proxies_default and django

Nick Joyce
On 15 Nov 2010, at 22:23, Minox Taurus wrote:

> Hi Nick,
>
>>> PyAMF 0.6 has been released, I suggest upgrading to that to see if this fixes your problem.
>
> Installing 0.6 seems the solution!

Good to know this worked! :)

> I cannot extract the info from this but I guess you can :-)
>
> AMF Request: <Envelope amfVersion=3>
> (u'/3', <Request target=u'null'>[<RemotingMessage  body=[5] source=None timestamp=0 destination=u'meterstandenservice' clientId=u'9971bbcd-7d54-4e28-b08a-9f9fbbf89f65' headers={'DSId': u'nil', 'DSEndpoint': u'pyamf'} timeToLive=0 messageId=u'8529BB98-0A61-C61A-5145-4F772A85E5E5' operation=u'getLocationMeters' />]</Request>)
> </Envelope>
>
> AMF Response: <Envelope amfVersion=3>
> (u'/3', <Response status=/onResult><AcknowledgeMessage  body=[] timestamp=1289823398 destination=None clientId='b2f0c3a1-fbba-40d2-a930-cd92bce55b41' headers={} timeToLive=None messageId='caef5acc-d3a5-4c1a-a4e8-9306eb89322a' correlationId=u'8529BB98-0A61-C61A-5145-4F772A85E5E5' /></Response>)
> </Envelope>
>
> This is the gateway.
>
> from django.core.exceptions import ObjectDoesNotExist
> import pyamf
> from pyamf import amf3
> from pyamf.remoting.gateway.django import DjangoGateway
> from meterstanden.models import Location, Meter
>
> import logging
> logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s')
>
> AMF_NAMESPACE = 'valueObjects'
>
> amf3.use_proxies_default = True

use_proxies_default = True is useful, but has an associated performance penalty. This was x2 for 0.5.1, I haven't checked for 0.6, but is likely to be a lot less if you are using the C extensions.

You can specify which attrs on a class must be converted to a proxy, e.g.:

class Location:
    class __amf__:
        proxy = ('foo', 'bar')

Where foo and bar are names of attributes on instances of that class.

>
> pyamf.register_class(Location, '%s.Location' % AMF_NAMESPACE)
> pyamf.register_class(Meter, '%s.Meter' % AMF_NAMESPACE)

This is a perfectly acceptable way to register aliases, but can get cumbersome if you have to register all classes within 1 module.

pyamf.register_package was built for this purpose. Use it here like:

from meterstanden import models

pyamf.register_package(models, AMF_NAMESPACE)

This will register every class in `models' with the prefix AMF_NAMESPACE.

>
> def getLocations(request):
>   locationList = Location.objects.all()
>   return locationList
>
> def getLocationMeters(request,id):
>     try:
>         location = Location.objects.get(id=id)
>         return location.meter_set.all()
>     except ObjectDoesNotExist:
>         pass
>     return id

Just as a matter of style - wouldn't it be a better interface if None was returned if the Location object does not exist for id?

>
> services = {
>   'meterstandenservice.getLocations':getLocations,
>   'meterstandenservice.getLocationMeters':getLocationMeters,
> }
>
> myGateway = DjangoGateway(services, logger=logging)
>
> Is this ok?

All looks good to me - happy coding.

Cheers,

Nick
_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Loading...