gdc: replace SOAPpy usage by straight POSTs (#43756)
This commit is contained in:
parent
f44a95ac7f
commit
07b56cdddb
|
@ -1,7 +1,4 @@
|
||||||
try:
|
import xml.etree.ElementTree as ET
|
||||||
import SOAPpy
|
|
||||||
except ImportError:
|
|
||||||
SOAPpy = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import phpserialize
|
import phpserialize
|
||||||
|
@ -17,8 +14,26 @@ from passerelle.base.models import BaseResource
|
||||||
from passerelle.utils.api import endpoint
|
from passerelle.utils.api import endpoint
|
||||||
|
|
||||||
|
|
||||||
|
def deep_bytes2str(obj):
|
||||||
|
if obj is None or isinstance(obj, (int, str)):
|
||||||
|
return obj
|
||||||
|
if isinstance(obj, bytes):
|
||||||
|
try:
|
||||||
|
return obj.decode('utf-8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
return obj
|
||||||
|
if isinstance(obj, list):
|
||||||
|
return [deep_bytes2str(x) for x in obj]
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
new_d = {}
|
||||||
|
for k, v in obj.items():
|
||||||
|
new_d[force_text(k)] = deep_bytes2str(v)
|
||||||
|
return new_d
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def phpserialize_loads(s):
|
def phpserialize_loads(s):
|
||||||
return phpserialize.loads(s.encode('utf-8'))
|
return deep_bytes2str(phpserialize.loads(s.encode('utf-8')))
|
||||||
|
|
||||||
|
|
||||||
class Gdc(BaseResource):
|
class Gdc(BaseResource):
|
||||||
|
@ -31,22 +46,47 @@ class Gdc(BaseResource):
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _('GDC Web Service')
|
verbose_name = _('GDC Web Service')
|
||||||
|
|
||||||
|
def call_soap(self, action, *args, **kwargs):
|
||||||
|
params = []
|
||||||
|
for i, arg in enumerate(args):
|
||||||
|
params.append('<v%(i)s xsi:type="xsd:string">%(value)s</v%(i)s>' % {'i': i + 1, 'value': arg})
|
||||||
|
for key, value in kwargs.items():
|
||||||
|
type_ = 'int' if isinstance(value, int) else 'string'
|
||||||
|
params.append('<%(key)s xsi:type="xsd:%(type)s">%(value)s</%(key)s>' % {
|
||||||
|
'key': key, 'type': type_, 'value': value})
|
||||||
|
|
||||||
|
data = """<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<SOAP-ENV:Envelope
|
||||||
|
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
|
||||||
|
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
|
||||||
|
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
|
||||||
|
>
|
||||||
|
<SOAP-ENV:Body>
|
||||||
|
<%(action)s SOAP-ENC:root="1">
|
||||||
|
%(params)s
|
||||||
|
</%(action)s>
|
||||||
|
</SOAP-ENV:Body>
|
||||||
|
</SOAP-ENV:Envelope>""" % {'action': action, 'params': ''.join(params)}
|
||||||
|
resp = self.requests.post(self.service_url, data=data)
|
||||||
|
return ET.ElementTree(ET.fromstring(resp.content))
|
||||||
|
|
||||||
|
|
||||||
@endpoint()
|
@endpoint()
|
||||||
def communes(self, request, *args, **kwargs):
|
def communes(self, request, *args, **kwargs):
|
||||||
server = SOAPpy.SOAPProxy(self.service_url)
|
resp = self.call_soap('getListeCommune')
|
||||||
soap_result = phpserialize_loads(server.getListeCommune()['listeCommune'])
|
soap_result = phpserialize_loads(resp.findall('.//listeCommune')[0].text)
|
||||||
result = []
|
result = []
|
||||||
for k, v in soap_result.items():
|
for k, v in soap_result.items():
|
||||||
result.append({'id': k, 'text': force_text(v, 'utf-8')})
|
result.append({'id': k, 'text': force_text(v, 'utf-8')})
|
||||||
result.sort(lambda x,y: cmp(x['id'], y['id']))
|
result.sort(key=lambda x: x['id'])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@endpoint()
|
@endpoint()
|
||||||
def objets(self, request, *args, **kwargs):
|
def objets(self, request, *args, **kwargs):
|
||||||
server = SOAPpy.SOAPProxy(self.service_url)
|
resp = self.call_soap('getListeObjet')
|
||||||
soap_result = phpserialize_loads(server.getListeObjet()['listeObjet'])
|
soap_result = phpserialize_loads(resp.findall('.//listeObjet')[0].text)
|
||||||
result = []
|
result = []
|
||||||
for k, v in soap_result.items():
|
for k, v in soap_result.items():
|
||||||
result.append({'id': k, 'text': force_text(v, 'utf-8')})
|
result.append({'id': k, 'text': force_text(v, 'utf-8')})
|
||||||
result.sort(lambda x,y: cmp(x['id'], y['id']))
|
result.sort(key=lambda x: x['id'])
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
from django.utils.encoding import force_text
|
||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
from django.views.generic.detail import SingleObjectMixin, DetailView
|
from django.views.generic.detail import SingleObjectMixin, DetailView
|
||||||
|
|
||||||
|
@ -6,7 +7,7 @@ from passerelle.compat import json_loads
|
||||||
import passerelle.utils as utils
|
import passerelle.utils as utils
|
||||||
from passerelle.utils.conversion import normalize
|
from passerelle.utils.conversion import normalize
|
||||||
|
|
||||||
from .models import Gdc, phpserialize, phpserialize_loads, SOAPpy
|
from .models import Gdc, phpserialize, phpserialize_loads
|
||||||
|
|
||||||
|
|
||||||
class GdcCrash(Exception):
|
class GdcCrash(Exception):
|
||||||
|
@ -17,12 +18,11 @@ class StatusView(View, SingleObjectMixin):
|
||||||
model = Gdc
|
model = Gdc
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
if SOAPpy is None:
|
|
||||||
raise Http404
|
|
||||||
ref = int(kwargs.get('ref'))
|
ref = int(kwargs.get('ref'))
|
||||||
server = SOAPpy.SOAPProxy(self.get_object().service_url)
|
service = self.get_object()
|
||||||
try:
|
try:
|
||||||
soap_result = server.getDemandeControleurEtat(ref)['listeInfo']
|
resp = service.call_soap('getDemandeControleurEtat', ref)
|
||||||
|
soap_result = resp.findall('.//listeInfo')[0].text
|
||||||
except:
|
except:
|
||||||
# if there's a gdc crash don't return anything and hopefully the
|
# if there's a gdc crash don't return anything and hopefully the
|
||||||
# w.c.s. workflow will handle that and retry later.
|
# w.c.s. workflow will handle that and retry later.
|
||||||
|
@ -30,18 +30,14 @@ class StatusView(View, SingleObjectMixin):
|
||||||
else:
|
else:
|
||||||
gdc_result = phpserialize_loads(soap_result)
|
gdc_result = phpserialize_loads(soap_result)
|
||||||
result = {
|
result = {
|
||||||
'status_id': gdc_result['STATUT_ID']
|
'status_id': gdc_result['STATUT_ID'],
|
||||||
}
|
}
|
||||||
return utils.response_for_json(request, {'data': result})
|
return utils.response_for_json(request, {'data': result})
|
||||||
|
|
||||||
|
|
||||||
def get_voies(service_url, insee):
|
def get_voies(service, insee):
|
||||||
server = SOAPpy.SOAPProxy(service_url)
|
resp = service.call_soap('getListeVoieCommune', insee)
|
||||||
try:
|
soap_result = phpserialize_loads(resp.findall('.//listeVoie')[0].text)
|
||||||
raw_soap_result = server.getListeVoieCommune(insee)
|
|
||||||
except:
|
|
||||||
raise GdcCrash()
|
|
||||||
soap_result = phpserialize_loads(raw_soap_result['listeVoie'])
|
|
||||||
result = []
|
result = []
|
||||||
prefix_map = {
|
prefix_map = {
|
||||||
'ALL': 'ALLEE',
|
'ALL': 'ALLEE',
|
||||||
|
@ -84,7 +80,7 @@ def get_voies(service_url, insee):
|
||||||
if v.startswith(prefix + ' '):
|
if v.startswith(prefix + ' '):
|
||||||
v = (full + v[len(prefix):]).strip()
|
v = (full + v[len(prefix):]).strip()
|
||||||
result.append({'id': k, 'text': v})
|
result.append({'id': k, 'text': v})
|
||||||
result.sort(lambda x,y: cmp(x['id'], y['id']))
|
result.sort(key=lambda x: x['id'])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,11 +88,9 @@ class VoiesView(View, SingleObjectMixin):
|
||||||
model = Gdc
|
model = Gdc
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
if SOAPpy is None:
|
|
||||||
raise Http404
|
|
||||||
insee = kwargs.get('insee')
|
insee = kwargs.get('insee')
|
||||||
try:
|
try:
|
||||||
result = get_voies(self.get_object().service_url, insee)
|
result = get_voies(self.get_object(), insee)
|
||||||
except GdcCrash:
|
except GdcCrash:
|
||||||
result = []
|
result = []
|
||||||
q = request.GET.get('q')
|
q = request.GET.get('q')
|
||||||
|
@ -110,7 +104,7 @@ class PostDemandeView(View, SingleObjectMixin):
|
||||||
model = Gdc
|
model = Gdc
|
||||||
|
|
||||||
@utils.protected_api('can_post_request')
|
@utils.protected_api('can_post_request')
|
||||||
def post(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
# <wsdl:message name='addDemandeExterneParticulierRequest'>
|
# <wsdl:message name='addDemandeExterneParticulierRequest'>
|
||||||
# <wsdl:part name='nom' type='xsd:string'></wsdl:part>
|
# <wsdl:part name='nom' type='xsd:string'></wsdl:part>
|
||||||
# <wsdl:part name='prenom' type='xsd:string'></wsdl:part>
|
# <wsdl:part name='prenom' type='xsd:string'></wsdl:part>
|
||||||
|
@ -122,10 +116,7 @@ class PostDemandeView(View, SingleObjectMixin):
|
||||||
# <wsdl:part name='voie_id' type='xsd:string'></wsdl:part>
|
# <wsdl:part name='voie_id' type='xsd:string'></wsdl:part>
|
||||||
# <wsdl:part name='voie_num' type='xsd:string'></wsdl:part>
|
# <wsdl:part name='voie_num' type='xsd:string'></wsdl:part>
|
||||||
# </wsdl:message>
|
# </wsdl:message>
|
||||||
if SOAPpy is None:
|
|
||||||
raise Http404
|
|
||||||
data = json_loads(request.body)
|
data = json_loads(request.body)
|
||||||
server = SOAPpy.SOAPProxy(self.get_object().service_url)
|
|
||||||
voie_id = data['fields'].get('voie_raw')
|
voie_id = data['fields'].get('voie_raw')
|
||||||
voie_str = data['fields'].get('voie')
|
voie_str = data['fields'].get('voie')
|
||||||
insee = data['fields'].get('commune_raw')
|
insee = data['fields'].get('commune_raw')
|
||||||
|
@ -133,7 +124,7 @@ class PostDemandeView(View, SingleObjectMixin):
|
||||||
# look for a voie with that name, so we can provide an identifier
|
# look for a voie with that name, so we can provide an identifier
|
||||||
# to gdc
|
# to gdc
|
||||||
try:
|
try:
|
||||||
voies = get_voies(self.get_object().service_url, insee)
|
voies = get_voies(self.get_object(), insee)
|
||||||
except GdcCrash:
|
except GdcCrash:
|
||||||
result = {'result': 'gdc soap crash'}
|
result = {'result': 'gdc soap crash'}
|
||||||
return utils.response_for_json(request, result)
|
return utils.response_for_json(request, result)
|
||||||
|
@ -152,9 +143,9 @@ class PostDemandeView(View, SingleObjectMixin):
|
||||||
'prenom': data['fields'].get('prenom'),
|
'prenom': data['fields'].get('prenom'),
|
||||||
'mail': data['fields'].get('mail'),
|
'mail': data['fields'].get('mail'),
|
||||||
'telephone': data['fields'].get('telephone'),
|
'telephone': data['fields'].get('telephone'),
|
||||||
'objet_externe': objet,
|
'objet_externe': int(objet),
|
||||||
'commentaire': data['fields'].get('commentaire'),
|
'commentaire': data['fields'].get('commentaire'),
|
||||||
'insee_commune': insee,
|
'insee_commune': int(insee),
|
||||||
'voie_id': voie_id,
|
'voie_id': voie_id,
|
||||||
'voie_str': voie_str,
|
'voie_str': voie_str,
|
||||||
'voie_num': data['fields'].get('voie_num'),
|
'voie_num': data['fields'].get('voie_num'),
|
||||||
|
@ -165,12 +156,13 @@ class PostDemandeView(View, SingleObjectMixin):
|
||||||
kwargs['picture_b64'] = data['fields']['picture']['content']
|
kwargs['picture_b64'] = data['fields']['picture']['content']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
soap_result = server.addDemandeExterneParticulier(**kwargs)
|
resp = self.get_object().call_soap('addDemandeExterneParticulier', **kwargs)
|
||||||
except:
|
except IOError:
|
||||||
result = {'result': 'gdc soap crash'}
|
result = {'result': 'gdc soap crash'}
|
||||||
else:
|
else:
|
||||||
result = phpserialize_loads(soap_result['listeInfo'])
|
code_retour = force_text(resp.findall('.//code_retour')[0].text)
|
||||||
result = {'result': soap_result['code_retour'],
|
result = phpserialize_loads(resp.findall('.//listeInfo')[0].text)
|
||||||
|
result = {'result': code_retour,
|
||||||
'display_id': result.get('IDENTIFIANT'),
|
'display_id': result.get('IDENTIFIANT'),
|
||||||
'id': result.get('IDENTIFIANT'),
|
'id': result.get('IDENTIFIANT'),
|
||||||
'details': result}
|
'details': result}
|
||||||
|
@ -185,6 +177,4 @@ class GdcDetailView(DetailView):
|
||||||
context = super(GdcDetailView, self).get_context_data(**kwargs)
|
context = super(GdcDetailView, self).get_context_data(**kwargs)
|
||||||
if phpserialize is None:
|
if phpserialize is None:
|
||||||
context['missing_phpserialize'] = True
|
context['missing_phpserialize'] = True
|
||||||
if SOAPpy is None:
|
|
||||||
context['missing_soappy'] = True
|
|
||||||
return context
|
return context
|
||||||
|
|
Loading…
Reference in New Issue