how to read an lxml.objectify.ObjectifiedElement object with django-piston?

49 views
Skip to first unread message

Miernik

unread,
Mar 31, 2011, 6:14:15 PM3/31/11
to django-piston
I am trying to make a django-piston API which reads another API and
presents it in a different format (reads an XML-only API and presents
it in JSON). But I am having trouble, because the way I am doing it I
just an empty string.

Here is my handler:

-------------------------------------------------------------
class MyHandler(BaseHandler):
methods_allowed = ('GET')

def read(self, request, my_argument):

my_node = get_node(my_argument)
return my_node
-------------------------------------------------------------

The get_node function is just a function which gets the object from
the other API, and that is an lxml object:

-------------------------------------------------------------
In [27]: type(my_node)
Out[27]: <type 'lxml.objectify.ObjectifiedElement'>
-------------------------------------------------------------

In my urls.py I have:
-------------------------------------------------------------
from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import MyHandler

class CsrfExemptResource( Resource ):
def __init__( self, handler, authentication = None ):
super( CsrfExemptResource, self ).__init__( handler,
authentication )
self.csrf_exempt = getattr( self.handler, 'csrf_exempt',
True )

my_resource = CsrfExemptResource( MyHandler )

urlpatterns = patterns( '',
url( r'^api/(?P<my_argument>.*)/$', my_resource ),
)
-------------------------------------------------------------

And when I visit that URL (with the right argument, which given
directly to get_node gives a correct object), I just get an empty
string.

How to make it work?

Malcolm Box

unread,
Apr 1, 2011, 8:30:10 AM4/1/11
to django...@googlegroups.com, django-piston
Piston is nearly but not completely magic.

It has to know how to turn what you return from the read method into JSON. It has logic for models, querysets etc but if it doesn't have specific handling for the type it falls back on django's smart_Unicode. My guess is that the smart_Unicode for an lxml node is an empty string.

You can fix this either by converting the node to a relevant string or structure that piston does know how to render in the read() method and returning that, or by tweaking emitters.py in the construct() method to handle lxml nodes.

Hth

Malcolm

Sent from my iPhone, please excuse any typos

> --
> You received this message because you are subscribed to the Google Groups "django-piston" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-pisto...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-piston?hl=en.
>

Reply all
Reply to author
Forward
0 new messages