misc: move duplicated soap utility functions to main soap.py (#27140)
This commit is contained in:
parent
6c4abb1975
commit
412c2a1bdf
|
@ -34,6 +34,7 @@ from django.core.cache import cache
|
|||
|
||||
from passerelle.base.models import BaseResource
|
||||
from passerelle.utils.api import endpoint, APIError
|
||||
from passerelle.soap import sudsobject_to_dict
|
||||
|
||||
from .formdata import FormData, CREATION_SCHEMA, list_schema_fields
|
||||
|
||||
|
@ -56,22 +57,6 @@ def fill_sudsobject_with_dict(sudsobject, fields, prefix=None):
|
|||
# sudsobject.foo.bar <- fields['foo_bar']
|
||||
setattr(sudsobject, key, fields[attr])
|
||||
|
||||
def sudsobject_to_dict(sudsobject):
|
||||
out = {}
|
||||
for key, value in suds.sudsobject.asdict(sudsobject).iteritems():
|
||||
if hasattr(value, '__keylist__'):
|
||||
out[key] = sudsobject_to_dict(value)
|
||||
elif isinstance(value, list):
|
||||
out[key] = []
|
||||
for item in value:
|
||||
if hasattr(item, '__keylist__'):
|
||||
out[key].append(sudsobject_to_dict(item))
|
||||
else:
|
||||
out[key].append(item)
|
||||
else:
|
||||
out[key] = value
|
||||
return out
|
||||
|
||||
|
||||
class Greco(BaseResource):
|
||||
application = models.CharField(_('Application identifier'), max_length=200)
|
||||
|
|
|
@ -28,7 +28,7 @@ from django.conf import settings
|
|||
from suds.transport.http import HttpAuthenticated
|
||||
from suds.transport import Reply
|
||||
from suds.client import Client
|
||||
from suds.sudsobject import asdict
|
||||
|
||||
|
||||
class Transport(HttpAuthenticated):
|
||||
def __init__(self, model, **kwargs):
|
||||
|
@ -63,41 +63,3 @@ class Transport(HttpAuthenticated):
|
|||
def get_client(model):
|
||||
transport = Transport(model)
|
||||
return Client(model.wsdl_url, transport=transport, cache=None)
|
||||
|
||||
def client_to_jsondict(client):
|
||||
"""return description of the client, as dict (for json export)"""
|
||||
res = {}
|
||||
for i, sd in enumerate(client.sd):
|
||||
d = {}
|
||||
d['tns'] = sd.wsdl.tns[1]
|
||||
d['prefixes'] = dict(p for p in sd.prefixes)
|
||||
d['ports'] = {}
|
||||
for p in sd.ports:
|
||||
d['ports'][p[0].name] = {}
|
||||
for m in p[1]:
|
||||
d['ports'][p[0].name][m[0]] = dict(
|
||||
(mp[0], sd.xlate(mp[1])) for mp in m[1])
|
||||
d['types'] = {}
|
||||
for t in sd.types:
|
||||
ft = client.factory.create(sd.xlate(t[0]))
|
||||
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
|
||||
|
||||
|
|
|
@ -25,8 +25,9 @@ from django.utils.decorators import method_decorator
|
|||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from passerelle import utils
|
||||
from passerelle.soap import sudsobject_to_dict, client_to_jsondict
|
||||
|
||||
from .soap import get_client, client_to_jsondict, recursive_asdict
|
||||
from .soap import get_client
|
||||
from .models import Maarch
|
||||
|
||||
logger = logging.getLogger('passerelle.contrib.maarch')
|
||||
|
@ -223,7 +224,7 @@ class ResourceView(DetailView):
|
|||
results = client.service.storeResource(
|
||||
encodedFile, metadata, collId,
|
||||
table, fileFormat, status)
|
||||
data = recursive_asdict(results)
|
||||
data = sudsobject_to_dict(results)
|
||||
logger.debug('storeResource result: %r', data)
|
||||
|
||||
resId = data.get('resId')
|
||||
|
|
|
@ -28,7 +28,6 @@ from django.conf import settings
|
|||
from suds.transport.http import HttpAuthenticated
|
||||
from suds.transport import Reply
|
||||
from suds.client import Client
|
||||
from suds.sudsobject import asdict
|
||||
|
||||
|
||||
class Transport(HttpAuthenticated):
|
||||
|
@ -64,40 +63,3 @@ class Transport(HttpAuthenticated):
|
|||
def get_client(model):
|
||||
transport = Transport(model)
|
||||
return Client(model.wsdl_url, transport=transport, cache=None)
|
||||
|
||||
def client_to_jsondict(client):
|
||||
"""return description of the client, as dict (for json export)"""
|
||||
res = {}
|
||||
for i, sd in enumerate(client.sd):
|
||||
d = {}
|
||||
d['tns'] = sd.wsdl.tns[1]
|
||||
d['prefixes'] = dict(p for p in sd.prefixes)
|
||||
d['ports'] = {}
|
||||
for p in sd.ports:
|
||||
d['ports'][p[0].name] = {}
|
||||
for m in p[1]:
|
||||
d['ports'][p[0].name][m[0]] = dict(
|
||||
(mp[0], sd.xlate(mp[1])) for mp in m[1])
|
||||
d['types'] = {}
|
||||
for t in sd.types:
|
||||
ft = client.factory.create(sd.xlate(t[0]))
|
||||
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
|
||||
|
|
|
@ -21,8 +21,9 @@ from django.utils.decorators import method_decorator
|
|||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from passerelle import utils
|
||||
from passerelle.soap import sudsobject_to_dict, client_to_jsondict
|
||||
|
||||
from .soap import get_client, client_to_jsondict, recursive_asdict
|
||||
from .soap import get_client
|
||||
from .models import SeisinByEmailManagement
|
||||
|
||||
|
||||
|
@ -120,7 +121,7 @@ class ResourceView(DetailView):
|
|||
request_object,
|
||||
data,
|
||||
attachmentData)
|
||||
data = recursive_asdict(results)
|
||||
data = sudsobject_to_dict(results)
|
||||
|
||||
if debug:
|
||||
data['debug'] = debug_output
|
||||
|
|
|
@ -29,6 +29,7 @@ from django.utils.encoding import smart_text
|
|||
from jsonfield import JSONField
|
||||
|
||||
from passerelle.base.models import BaseResource
|
||||
from passerelle.soap import client_to_jsondict
|
||||
from passerelle.utils.api import endpoint
|
||||
from passerelle.utils.jsonresponse import APIError
|
||||
from passerelle.views import WrongParameter
|
||||
|
@ -172,7 +173,7 @@ class TeamnetAxel(BaseResource):
|
|||
raise APIError('Client Error: %s' % exc.message)
|
||||
res = {'ping': 'pong'}
|
||||
if 'debug' in request.GET:
|
||||
res['client'] = soap.client_to_jsondict(client)
|
||||
res['client'] = client_to_jsondict(client)
|
||||
return {'data': res}
|
||||
|
||||
@endpoint(perm='can_access')
|
||||
|
|
|
@ -25,7 +25,6 @@ except ImportError:
|
|||
from suds.transport.http import HttpAuthenticated
|
||||
from suds.transport import Reply
|
||||
from suds.client import Client
|
||||
from suds.sudsobject import asdict
|
||||
|
||||
|
||||
class Transport(HttpAuthenticated):
|
||||
|
@ -64,45 +63,6 @@ def get_client(model):
|
|||
return Client(model.wsdl_url, transport=transport, cache=None)
|
||||
|
||||
|
||||
def client_to_jsondict(client):
|
||||
"""return description of the client, as dict (for json export)"""
|
||||
res = {}
|
||||
for i, sd in enumerate(client.sd):
|
||||
d = {}
|
||||
d['tns'] = sd.wsdl.tns[1]
|
||||
d['prefixes'] = dict(p for p in sd.prefixes)
|
||||
d['ports'] = {}
|
||||
for p in sd.ports:
|
||||
d['ports'][p[0].name] = {}
|
||||
for m in p[1]:
|
||||
d['ports'][p[0].name][m[0]] = dict(
|
||||
(mp[0], sd.xlate(mp[1])) for mp in m[1])
|
||||
d['types'] = {}
|
||||
for t in sd.types:
|
||||
ft = client.factory.create(sd.xlate(t[0]))
|
||||
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
|
||||
|
||||
|
||||
def xml_to_dict(result):
|
||||
"""Converts AXEL simple XML responses into dict()"""
|
||||
d = {}
|
||||
|
|
|
@ -6,6 +6,7 @@ import requests
|
|||
from suds.client import Client
|
||||
from suds.transport.http import HttpAuthenticated
|
||||
from suds.transport import Reply
|
||||
import suds.sudsobject
|
||||
|
||||
|
||||
class Transport(HttpAuthenticated):
|
||||
|
@ -116,3 +117,41 @@ class Soap(object):
|
|||
domains=self.domains),
|
||||
plugins=self.plugins,
|
||||
cache=self.cache)
|
||||
|
||||
|
||||
def client_to_jsondict(client):
|
||||
"""return description of the client, as dict (for json export)"""
|
||||
res = {}
|
||||
for i, sd in enumerate(client.sd):
|
||||
d = {}
|
||||
d['tns'] = sd.wsdl.tns[1]
|
||||
d['prefixes'] = dict(p for p in sd.prefixes)
|
||||
d['ports'] = {}
|
||||
for p in sd.ports:
|
||||
d['ports'][p[0].name] = {}
|
||||
for m in p[1]:
|
||||
d['ports'][p[0].name][m[0]] = dict(
|
||||
(mp[0], sd.xlate(mp[1])) for mp in m[1])
|
||||
d['types'] = {}
|
||||
for t in sd.types:
|
||||
ft = client.factory.create(sd.xlate(t[0]))
|
||||
d['types'][sd.xlate(t[0])] = unicode(ft)
|
||||
res[sd.service.name] = d
|
||||
return res
|
||||
|
||||
|
||||
def sudsobject_to_dict(sudsobject):
|
||||
out = {}
|
||||
for key, value in suds.sudsobject.asdict(sudsobject).iteritems():
|
||||
if hasattr(value, '__keylist__'):
|
||||
out[key] = sudsobject_to_dict(value)
|
||||
elif isinstance(value, list):
|
||||
out[key] = []
|
||||
for item in value:
|
||||
if hasattr(item, '__keylist__'):
|
||||
out[key].append(sudsobject_to_dict(item))
|
||||
else:
|
||||
out[key].append(item)
|
||||
else:
|
||||
out[key] = value
|
||||
return out
|
||||
|
|
Loading…
Reference in New Issue