views: return empty data response on applicative error (#52084)

This commit is contained in:
Nicolas Roche 2021-04-14 12:49:02 +02:00
parent 49408b91ec
commit 800e9a6a11
2 changed files with 75 additions and 13 deletions

View File

@ -11,6 +11,7 @@ from django.shortcuts import redirect
from django.utils.encoding import force_text
from passerelle import utils
from passerelle.utils.jsonresponse import APIError
from .models import MontpellierSig
from .forms import MontpellierSigForm
@ -141,8 +142,11 @@ class VoiesView(View, SingleObjectMixin):
def get(self, request, *args, **kwargs):
insee = kwargs['insee']
result = self.get_object().sig_request('commune/' + insee + '/voie')
voies = []
try:
result = self.get_object().sig_request('commune/' + insee + '/voie')
except APIError:
return utils.response_for_json(request, {'data': voies})
if isinstance(result, dict) and result.get('error'):
return utils.response_for_json(request, {'data': voies})
for i in result:
@ -181,13 +185,16 @@ class VoiesCommuneView(View, SingleObjectMixin):
def get(self, request, *args, **kwargs):
insee = kwargs['insee']
nom_rue = get_original_prefix(kwargs['nom_rue'])
if 'q' in request.GET:
result = self.get_object().sig_request(
'commune/' + insee + '/voie/' + nom_rue + '/numero/' + request.GET['q']
)
else:
result = self.get_object().sig_request('commune/' + insee + '/voie/' + nom_rue + '/numero')
voies_communes = []
try:
if 'q' in request.GET:
result = self.get_object().sig_request(
'commune/' + insee + '/voie/' + nom_rue + '/numero/' + request.GET['q']
)
else:
result = self.get_object().sig_request('commune/' + insee + '/voie/' + nom_rue + '/numero')
except APIError:
return utils.response_for_json(request, {'data': voies_communes})
if isinstance(result, dict) and result.get('error'):
return utils.response_for_json(request, {'data': voies_communes})
for i in result:
@ -204,8 +211,11 @@ class VoieCommuneView(View, SingleObjectMixin):
model = MontpellierSig
def get(self, request, *args, **kwargs):
result = self.get_object().sig_request('voiecommune/' + kwargs['nom_rue'])
voies_communes = []
try:
result = self.get_object().sig_request('voiecommune/' + kwargs['nom_rue'])
except APIError:
return utils.response_for_json(request, {'data': voies_communes})
if isinstance(result, dict) and result.get('error'):
return utils.response_for_json(request, {'data': voies_communes})
for i in result:
@ -236,7 +246,10 @@ class AdresseView(View, SingleObjectMixin):
l_lon, l_lat = pyproj.transform(wgs84, lambert93, lon, lat)
except TypeError:
return HttpResponseBadRequest()
result = self.get_object().sig_request('adresse/%s/%s' % (l_lon, l_lat))
try:
result = self.get_object().sig_request('adresse/%s/%s' % (l_lon, l_lat))
except APIError:
return utils.response_for_json(request, {'address': {}})
house_number, road = split_street(result.get('voie'))
address = {
'road': road,
@ -259,14 +272,20 @@ class DistrictView(View, SingleObjectMixin):
numero = kwargs['numero']
uri = 'commune/%s/voie/%s/numero/%s' % (insee, nom_rue, numero)
result = self.get_object().sig_request(uri)
try:
result = self.get_object().sig_request(uri)
except APIError:
return utils.response_for_json(request, {'data': []})
if isinstance(result, dict) and result.get('error'):
return utils.response_for_json(request, {'data': []})
if result:
r = result[0]
data = self.get_object().sig_request(
'adresse/%s/%s' % (r['geometry'].get('x'), r['geometry'].get('y'))
)
try:
data = self.get_object().sig_request(
'adresse/%s/%s' % (r['geometry'].get('x'), r['geometry'].get('y'))
)
except APIError:
return utils.response_for_json(request, {'data': []})
return utils.response_for_json(request, {'data': data})
return utils.response_for_json(request, {'data': []})

View File

@ -140,6 +140,17 @@ class VoiesViewTest(Mixin, TestCase):
== b'jQuery110206937632912688186_1615823034444({"data": [{"id": "IMP DES ALBYZZIAS", "text": "IMPASSE DES ALBYZZIAS"}]});'
)
def test_voies_view_error(self):
app = Client()
with httmock.HTTMock(self.hebe_http_error):
resp = app.get(
self.URL
+ '?callback=jQuery110206937632912688186_1615823034444&q=IMPASSE+DES+ALBYZZIAS&_=1615823034445'
)
assert resp.content == b'jQuery110206937632912688186_1615823034444({"data": []});'
class VoieCommuneViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/voiecommune/COLUCHE'
@ -185,6 +196,14 @@ class VoieCommuneViewTest(Mixin, TestCase):
},
]
def test_voie_commune_view_error(self):
app = Client()
with httmock.HTTMock(self.hebe_http_error):
resp = app.get(self.URL)
assert json.loads(resp.content) == {'data': []}
class VoiesCommuneViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/voies/34172/R MICHEL COLUCCI DIT COLUCHE/numero'
@ -239,6 +258,14 @@ class VoiesCommuneViewTest(Mixin, TestCase):
]
}
def test_voies_commune_view_error(self):
app = Client()
with httmock.HTTMock(self.hebe_http_error):
resp = app.get(self.URL)
assert json.loads(resp.content) == {'data': []}
class DistrictViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/quartier/34172/R MICHEL COLUCCI DIT COLUCHE/261/'
@ -282,6 +309,14 @@ class DistrictViewTest(Mixin, TestCase):
}
}
def test_district_view_error(self):
app = Client()
with httmock.HTTMock(self.hebe_http_error):
resp = app.get(self.URL)
assert json.loads(resp.content) == {'data': []}
class AdresseViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/reverse?lat=43.5998&lon=3.8775'
@ -315,3 +350,11 @@ class AdresseViewTest(Mixin, TestCase):
'country': 'France',
}
}
def test_district_view_error(self):
app = Client()
with httmock.HTTMock(self.hebe_http_error):
resp = app.get(self.URL)
assert json.loads(resp.content) == {'address': {}}