photon: handle "name" and pack other extra properties (#89845)
gitea/passerelle/pipeline/head This commit looks good Details

This commit is contained in:
Frédéric Péters 2024-04-19 20:58:25 +02:00
parent db9740da15
commit feb6fd1428
2 changed files with 62 additions and 3 deletions

View File

@ -111,25 +111,43 @@ class Photon(BaseResource):
result['lon'] = str(data['geometry']['coordinates'][0])
result['lat'] = str(data['geometry']['coordinates'][1])
result['address'] = {}
non_standard_properties = {}
for prop, value in data['properties'].items():
if prop in ('country', 'city', 'postcode'):
if prop in ('country', 'city', 'postcode', 'name'):
result['address'][prop] = value
elif prop == 'housenumber':
result['address']['house_number'] = value
elif prop == 'street':
result['address']['road'] = value
else:
non_standard_properties[prop] = value
result['text'] = ''
if (
result['address'].get('name')
and result['address'].get('road')
and result['address'].get('name') != result['address'].get('road')
):
# both "name" and "road" attributes, different; then name property has the place name
# (ex: "parc blandan") and it is used as prefix.
result['text'] += '%s, ' % result['address']['name']
if result['address'].get('house_number'):
result['text'] += '%s, ' % result['address']['house_number']
if result['address'].get('road'):
result['text'] += '%s ' % result['address']['road']
elif result['address'].get('name'):
# "name" and no "road" attribute, use the name in place of the road
result['text'] += '%s ' % result['address']['name']
if result['address'].get('postcode'):
result['text'] += '%s ' % result['address']['postcode']
if result['address'].get('city'):
result['text'] += '%s' % result['address']['city']
result['text'] = result['text'].strip()
result['display_name'] = result['text']
if non_standard_properties:
result['extra'] = non_standard_properties
dict_dump = json.dumps(result['address'], sort_keys=True)
result['id'] = hashlib.md5(force_bytes(dict_dump)).hexdigest()

View File

@ -59,6 +59,25 @@ CONTENT = {
},
'type': 'Feature',
},
{
'geometry': {'coordinates': [4.8796, 45.7665], 'type': 'Point'},
'type': 'Feature',
'properties': {
'osm_id': 1235833,
'country': 'France',
'city': 'Villeurbanne',
'countrycode': 'FR',
'postcode': '69601',
'type': 'house',
'osm_type': 'N',
'osm_key': 'amenity',
'housenumber': '2',
'street': 'Place Docteur Lazare Goujon',
'extra': {'metropole': 'true', 'espace_public': 'true'},
'osm_value': 'townhall',
'name': 'Hôtel de Ville de Villeurbanne',
},
},
],
'type': 'FeatureCollection',
}
@ -112,6 +131,28 @@ def test_photon_search(mocked_get, app, photon):
assert data['display_name'] == '208, Rue Garibaldi 69003 Lyon 3ème Arrondissement'
@mock.patch('passerelle.utils.Request.get')
def test_photon_search_get_extra_properties(mocked_get, app, photon):
endpoint = tests.utils.generic_endpoint_url('photon', 'search', slug=photon.slug)
mocked_get.return_value = tests.utils.FakedResponse(content=FAKED_CONTENT, status_code=200)
resp = app.get(endpoint, params={'q': 'plop'}, status=200)
assert mocked_get.call_args[0][0].startswith('http://example.net/path/api/?')
data = resp.json[2]
assert (
data['display_name']
== 'Hôtel de Ville de Villeurbanne, 2, Place Docteur Lazare Goujon 69601 Villeurbanne'
)
assert data['extra'] == {
'countrycode': 'FR',
'extra': {'espace_public': 'true', 'metropole': 'true'},
'osm_id': 1235833,
'osm_key': 'amenity',
'osm_type': 'N',
'osm_value': 'townhall',
'type': 'house',
}
def test_photon_search_qs(app, photon, mock_photon_search):
resp = app.get('/photon/%s/search?q=plop' % photon.slug)
assert 'display_name' in resp.json[0]
@ -253,7 +294,7 @@ def test_photon_addresses_cache(app, photon, mock_photon_search):
api_id = data['id']
assert AddressCacheModel.objects.filter(api_id=api_id).exists()
assert AddressCacheModel.objects.count() == 2
assert AddressCacheModel.objects.count() == 3
resp = app.get('/photon/%s/addresses?id=%s' % (photon.slug, api_id))
assert mock_photon_search.call['count'] == 1 # no new call
@ -261,7 +302,7 @@ def test_photon_addresses_cache(app, photon, mock_photon_search):
assert 'address' in data
resp = app.get('/photon/%s/addresses?q=plop' % photon.slug)
assert AddressCacheModel.objects.count() == 2 # no new object has been created
assert AddressCacheModel.objects.count() == 3 # no new object has been created
def test_photon_addresses_cache_err(app, photon, mock_photon_search):