views:

662

answers:

3

Hi, I'm trying to make a little "Hello World" webservice with Django following a few tutorials, but I'm hitting the same barrier over and over. I've defined a view.py and soaplib_handler.py:

view.py:

from soaplib_handler import DjangoSoapApp, soapmethod, soap_types

class HelloWorldService(DjangoSoapApp):

    __tns__ = 'http://saers.dk/soap/'

    @soapmethod(_returns=soap_types.Array(soap_types.String))
    def hello(self):
      return "Hello World"

soaplib_handler.py:

from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers import primitive as soap_types

from django.http import HttpResponse


class DjangoSoapApp(SimpleWSGISoapApp):

    def __call__(self, request):
        django_response = HttpResponse()
        def start_response(status, headers):
            status, reason = status.split(' ', 1)
            django_response.status_code = int(status)
            for header, value in headers:
                django_response[header] = value
        response = super(SimpleWSGISoapApp, self).__call__(request.META, start_response)
        django_response.content = "\n".join(response)

        return django_response

And it seems the "response = super...." line is giving me trouble. When I load up /hello_world/services.wsdl mapped in url.py I get:

AttributeError at /hello_world/service.wsdl 'module' object has no attribute 'tostring'

For the full error message, see here: http://saers.dk:8000/hello_world/service.wsdl

Do you have any suggestion as to why I get this error? And where is ElementTree defined?

Cheers

Nik

A: 

Hi, not sure if this will solve your problem, but the decorator on your function hello says that it is suppose to return a String Array, but you are actually returning a String

Try _returns=soap_types.String instead

Ray

Yes you are right. By calling http://saers.dk:8000/hello_world/service.wsdl he is not calling hello() method, so there is different problem.
zdmytriv
A: 

Copy/paste from my service:

# SoapLib Django workaround: http://www.djangosnippets.org/snippets/979/
class DumbStringIO(StringIO):
    """ Helper class for BaseWebService """
    def read(self, n): 
        return self.getvalue()

class DjangoSoapApp(SimpleWSGISoapApp):
    def __call__(self, request):
        """ Makes Django request suitable for SOAPlib SimpleWSGISoapApp class """

        http_response = HttpResponse()

        def start_response(status, headers):
            status, reason = status.split(' ', 1)
            http_response.status_code = int(status)

            for header, value in headers:
                http_response[header] = value

        environ = request.META.copy()
        body = ''.join(['%s=%s' % v for v in request.POST.items()])
        environ['CONTENT_LENGTH'] = len(body)
        environ['wsgi.input'] = DumbStringIO(body)
        environ['wsgi.multithread'] = False

        soap_app_response = super(BaseSOAPWebService, self).__call__(environ, start_response)

        http_response.content = "\n".join(soap_app_response)

        return http_response

Django snippet has a bug. Read last two comments from that url.

zdmytriv
+1  A: 

@zdmytriv The line

soap_app_response = super(BaseSOAPWebService, self).__call__(environ, start_response)

should look like

soap_app_response = super(DjangoSoapApp, self).__call__(environ, start_response)

then your example works.

schoash