Projet

Général

Profil

0001-photon-add-a-stable-identifiant-53984.patch

Nicolas Roche, 14 juin 2021 15:31

Télécharger (4,59 ko)

Voir les différences:

Subject: [PATCH] photon: add a stable identifiant (#53984)

 passerelle/apps/photon/models.py | 8 +++++++-
 tests/test_photon.py             | 4 ++--
 2 files changed, 9 insertions(+), 3 deletions(-)
passerelle/apps/photon/models.py
10 10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 12
# GNU Affero General Public License for more details.
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import datetime
18
import hashlib
19
import json
18 20

  
19 21
from django.contrib.postgres.fields import JSONField
20 22
from django.db import models
21 23
from django.utils import timezone
24
from django.utils.encoding import force_bytes
22 25
from django.utils.http import urlencode
23 26
from django.utils.six.moves.urllib import parse as urlparse
24 27
from django.utils.translation import ugettext_lazy as _
25 28
from requests import RequestException
26 29

  
27 30
from passerelle.base.models import BaseResource
28 31
from passerelle.utils.api import endpoint
29 32
from passerelle.utils.jsonresponse import APIError
......
87 90
        if result['address'].get('house_number'):
88 91
            result['text'] += '%s, ' % result['address']['house_number']
89 92
        if result['address'].get('road'):
90 93
            result['text'] += '%s ' % result['address']['road']
91 94
        if result['address'].get('postcode'):
92 95
            result['text'] += '%s ' % result['address']['postcode']
93 96
        if result['address'].get('city'):
94 97
            result['text'] += '%s' % result['address']['city']
98
        result['text'] = result['text'].strip()
95 99
        result['display_name'] = result['text']
96
        result['id'] = data['properties']['osm_id']
100

  
101
        dict_dump = json.dumps(result['address'], sort_keys=True)
102
        result['id'] = hashlib.md5(force_bytes(dict_dump)).hexdigest()
97 103
        return result
98 104

  
99 105
    @endpoint(
100 106
        pattern='(?P<q>.+)?$',
101 107
        description=_('Addresses list'),
102 108
        parameters={
103 109
            'id': {'description': _('Address identifier')},
104 110
            'q': {'description': _('Address'), 'example_value': '169 rue du chateau, paris'},
tests/test_photon.py
189 189
    assert endpoint == '/photon/test/addresses'
190 190
    mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT, status_code=200)
191 191
    resp = app.get(endpoint, params={'q': 'plop'}, status=200)
192 192
    data = resp.json['data'][0]
193 193
    assert data['lat'] == '45.7587414'
194 194
    assert data['lon'] == '4.8522272'
195 195
    assert data['display_name'] == '208, Rue Garibaldi 69003 Lyon 3ème Arrondissement'
196 196
    assert data['text'] == '208, Rue Garibaldi 69003 Lyon 3ème Arrondissement'
197
    assert data['id'] == 154419
197
    assert data['id'] == '2644ca352fdfef476b3f83ee99f0e8e5'
198 198
    assert data['address']['city'] == 'Lyon 3ème Arrondissement'
199 199
    assert data['address']['postcode'] == '69003'
200 200
    assert data['address']['road'] == 'Rue Garibaldi'
201 201

  
202 202

  
203 203
@mock.patch('passerelle.utils.Request.get')
204 204
def test_photon_addresses_qs_page_limit(mocked_get, app, photon):
205 205
    resp = app.get('/photon/%s/addresses?q=plop&page_limit=1' % photon.slug)
......
271 271

  
272 272
    # asking for the address again resets the timestamp
273 273
    freezer.move_to(datetime.timedelta(hours=1, seconds=1))
274 274
    app.get('/photon/%s/addresses?q=plop' % photon.slug)
275 275
    call_command('cron', 'hourly')
276 276
    assert AddressCacheModel.objects.count() == 2
277 277

  
278 278
    freezer.move_to(datetime.timedelta(hours=1, seconds=1))
279
    app.get('/photon/%s/addresses?id=%s' % (photon.slug, '154419'))
279
    app.get('/photon/%s/addresses?id=%s' % (photon.slug, '2644ca352fdfef476b3f83ee99f0e8e5'))
280 280
    call_command('cron', 'hourly')
281 281
    assert AddressCacheModel.objects.count() == 1
282 282

  
283 283

  
284 284
@mock.patch('passerelle.utils.Request.get')
285 285
def test_photon_addresses_data_change(mocked_get, app, photon):
286 286
    endpoint = utils.generic_endpoint_url('photon', 'addresses', slug=photon.slug)
287 287
    mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT, status_code=200)
288
-