0001-opengis-correctly-handle-XML-formatted-OpenGIS-error.patch
passerelle/apps/opengis/models.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
import math |
18 | 18 |
import xml.etree.ElementTree as ET |
19 |
from HTMLParser import HTMLParser |
|
19 | 20 | |
20 | 21 |
import pyproj |
21 | 22 | |
... | ... | |
101 | 102 |
params['CQL_FILTER'] += ' AND %s %s \'%%%s%%\'' % (filter_property_name, operator, q) |
102 | 103 |
response = self.requests.get(self.wfs_service_url, params=params) |
103 | 104 |
data = [] |
105 |
try: |
|
106 |
json_result = response.json() |
|
107 |
except ValueError: |
|
108 |
self.handle_opengis_error(response) |
|
109 |
# if handle_opengis_error did not raise an error, we raise a generic one |
|
110 |
raise APIError(u'OpenGIS Error: unparsable error', |
|
111 |
data={'content': repr(response.content[:1024])}) |
|
104 | 112 |
for feature in response.json()['features']: |
105 | 113 |
feature['text'] = feature['properties'].get(property_name) |
106 | 114 |
data.append(feature) |
107 | 115 |
return {'data': data} |
108 | 116 | |
117 |
def handle_opengis_error(self, response): |
|
118 |
try: |
|
119 |
root = ET.fromstring(response.content) |
|
120 |
except ET.ParseError: |
|
121 |
return None |
|
122 |
if root.tag != '{http://www.opengis.net/ows/1.1}ExceptionReport': |
|
123 |
return None |
|
124 |
exception = root.find('{http://www.opengis.net/ows/1.1}Exception') |
|
125 |
exception_code = exception.attrib.get('exceptionCode') |
|
126 |
if exception is None: |
|
127 |
return None |
|
128 |
exception_text = exception.find('{http://www.opengis.net/ows/1.1}ExceptionText') |
|
129 |
if exception_text is None: |
|
130 |
return None |
|
131 |
content = exception_text.text |
|
132 |
htmlparser = HTMLParser() |
|
133 |
content = htmlparser.unescape(content) |
|
134 |
raise APIError(u'OpenGIS Error: %s' % exception_code or 'unknown code', |
|
135 |
data={'text': content}) |
|
109 | 136 | |
110 | 137 |
@endpoint(perm='can_access', |
111 | 138 |
description=_('Get feature info'), |
112 |
- |