diff --git a/passerelle_maarch/soap.py b/passerelle_maarch/soap.py index a729126..796b886 100644 --- a/passerelle_maarch/soap.py +++ b/passerelle_maarch/soap.py @@ -26,6 +26,9 @@ except ImportError: from suds.transport.http import HttpAuthenticated from suds.transport import Reply from suds.client import Client +from suds.sudsobject import asdict +from suds.plugin import MessagePlugin + # FIXME: WSDL incomplet, ne reference pas le schema suivant # voir https://fedorahosted.org/suds/ticket/220 pour le fix @@ -60,12 +63,25 @@ class MaarchSudsTransport(HttpAuthenticated): return result +class MaarchFixesPlugin(MessagePlugin): + def marshalled(self, context): + gedId = context.envelope.getChild('Body').getChild('viewResource').getChild('gedId') + if gedId is not None: + gedId.set('xsi:type', 'xsd:int') + calledByWS = context.envelope.getChild('Body').getChild('viewResource').getChild('calledByWS') + if calledByWS is not None: + calledByWS.set('xsi:type', 'xsd:boolean') + + def get_client(maarch): transport = MaarchSudsTransport(maarch=maarch) - return Client(maarch.wsdl_url, transport=transport, cache=None, doctor=doctor) + return Client(maarch.wsdl_url, transport=transport, + plugins=[MaarchFixesPlugin()], + cache=None, + doctor=doctor) # see FIXME above -def client_to_dict(client): - """return description of the client, as dict""" +def client_to_jsondict(client): + """return description of the client, as dict (for json export)""" res = {} for i, sd in enumerate(client.sd): d = {} @@ -83,3 +99,21 @@ def client_to_dict(client): d['types'][sd.xlate(t[0])] = unicode(ft) res[sd.service.name] = d return res + +def recursive_asdict(d): + """Convert Suds object into serializable format.""" + out = {} + for k, v in asdict(d).iteritems(): + if hasattr(v, '__keylist__'): + out[k] = recursive_asdict(v) + elif isinstance(v, list): + out[k] = [] + for item in v: + if hasattr(item, '__keylist__'): + out[k].append(recursive_asdict(item)) + else: + out[k].append(item) + else: + out[k] = v + return out + diff --git a/passerelle_maarch/urls.py b/passerelle_maarch/urls.py index 4ef25a1..118feb6 100644 --- a/passerelle_maarch/urls.py +++ b/passerelle_maarch/urls.py @@ -25,6 +25,14 @@ public_urlpatterns = patterns('', name='maarch-view'), url(r'^(?P[\w,-]+)/ping/$', MaarchPingView.as_view(), name='maarch-ping'), + url(r'^(?P[\w,-]+)/resource/$', MaarchResourceView.as_view(), + name='maarch-resource'), + url(r'^(?P[\w,-]+)/resource/(?P\d+)/$', MaarchResourceView.as_view(), + name='maarch-resource-id'), + url(r'^(?P[\w,-]+)/contact/$', MaarchContactView.as_view(), + name='maarch-contact'), + url(r'^(?P[\w,-]+)/contact/(?P\d+)/$', MaarchContactView.as_view(), + name='maarch-contact-id'), ) management_urlpatterns = patterns('', diff --git a/passerelle_maarch/views.py b/passerelle_maarch/views.py index 73bb02c..82a1c81 100644 --- a/passerelle_maarch/views.py +++ b/passerelle_maarch/views.py @@ -24,7 +24,7 @@ from django.core.cache import cache from passerelle import utils -from .soap import get_client, client_to_dict +from .soap import get_client, client_to_jsondict, recursive_asdict from .models import MaarchManagement from .forms import MaarchManagementForm, MaarchManagementUpdateForm @@ -73,5 +73,34 @@ class MaarchPingView(MaarchDetailView): client = self.get_client() res = {'ping': 'pong'} if 'debug' in request.GET: - res['client'] = client_to_dict(client) + res['client'] = client_to_jsondict(client) return res + + +class MaarchResourceView(MaarchDetailView): + def get_data(self, request, resource_id=None, *args, **kwargs): + client = self.get_client() + if resource_id: + results = client.service.viewResource(int(resource_id), + 'res_x', 'adr_x', True) + else: + if 'where' in request.GET: + searchParams = client.factory.create('searchParams') + searchParams.whereClause = request.GET.get('where') + else: + searchParams = '' + results = client.service.Demo_searchResources(searchParams) + return recursive_asdict(results) + + +class MaarchContactView(MaarchDetailView): + def get_data(self, request, contact_id=None, *args, **kwargs): + client = self.get_client() + if contact_id: + searchParams = {'whereClause': "contact_id = '%s'" % contact_id} + elif 'where' in request.GET: + searchParams = {'whereClause': request.GET.get('where')} + else: + searchParams = '' + results = client.service.listContacts(searchParams) + return recursive_asdict(results)