From 4a1b43ac537fb57f38f4f835da934d551d7253e3 Mon Sep 17 00:00:00 2001
From: Thomas NOEL <tnoel@entrouvert.com>
Date: Mon, 25 May 2020 23:15:48 +0200
Subject: [PATCH] okina: add latlon on results when possible (#43292)

---
 passerelle/apps/okina/models.py | 8 ++++++--
 tests/test_okina.py             | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/passerelle/apps/okina/models.py b/passerelle/apps/okina/models.py
index 39321ff5..bd5ea3e5 100644
--- a/passerelle/apps/okina/models.py
+++ b/passerelle/apps/okina/models.py
@@ -80,12 +80,12 @@ class Okina(BaseResource):
         cities = []
         for city in okina_cities:
             if 'name' in city:
-                city['id'] = '%s' % city['id']
                 city['text'] = '%(name)s (%(zipCode)s)' % city
             else:  # old API
                 city = city['cityObject']
-                city['id'] = '%s' % city['id']
                 city['text'] = '%(nameCity)s (%(zipCode)s)' % city
+            city['id'] = '%s' % city['id']
+            city['latlon'] = '%s;%s' % (city.get('latitude') or '0.00', city.get('longitude') or '0.00')
             cities.append(city)
         cities.sort(key=lambda x: x['text'])
         return {'data': cities}
@@ -105,6 +105,9 @@ class Okina(BaseResource):
             del institution['schoolEstablishment']
             institution['id'] = '%s' % institution['id']
             institution['text'] = '%(uaiLabel)s' % institution
+            latitude = institution.get('customLocationY') or institution.get('locationY') or '0.00'
+            longitude = institution.get('customLocationX') or institution.get('locationX') or '0.00'
+            institution['latlon'] = '%s;%s' % (latitude, longitude)
             institutions.append(institution)
         institutions.sort(key=lambda x: x['text'])
         return institutions
@@ -145,6 +148,7 @@ class Okina(BaseResource):
         for stop in stops:
             stop['id'] = '%s' % stop['id']
             stop['text'] = stop['commercial_name']
+            stop['latlon'] = '%s;%s' % (stop.get('latitude') or '0.00', stop.get('longitude') or '0.00')
         return {'data': stops}
 
     @endpoint(name='stop-areas',
diff --git a/tests/test_okina.py b/tests/test_okina.py
index e069b3e5..46b1805c 100644
--- a/tests/test_okina.py
+++ b/tests/test_okina.py
@@ -99,8 +99,8 @@ INSTITUTIONS = '''[ {
     "address" : "4 ALLÉE DES LAURIERS",
     "zipCode" : "36007",
     "cityName" : "CHATEAUROUX CEDEX",
-    "locationX" : "1.692635599708531",
-    "locationY" : "46.791332546947025",
+    "locationX" : "1.6926355997775921",
+    "locationY" : "46.791332586468016",
     "nature" : "340",
     "natureTitle" : "Collège"
   },
@@ -1663,6 +1663,7 @@ def test_okina_cities(app, okina):
         assert resp.json['data'][0]['id'] == '83355'
         assert resp.json['data'][0]['insee'] == '36005'
         assert resp.json['data'][0]['text'] == 'ARDENTES (36120)'
+        assert resp.json['data'][0]['latlon'] == '0.00;0.00'
 
         requests_get.return_value = utils.FakedResponse(content=CITIES_NEW,
                                                         status_code=200)
@@ -1674,6 +1675,7 @@ def test_okina_cities(app, okina):
         assert resp.json['data'][0]['id'] == '83355'
         assert resp.json['data'][0]['insee'] == '36005'
         assert resp.json['data'][0]['text'] == 'ARDENTES (36120)'
+        assert resp.json['data'][0]['latlon'] == '0.00;0.00'
 
 def test_okina_classes(app, okina):
     endpoint = utils.generic_endpoint_url('okina', 'classes', slug=okina.slug)
@@ -1701,6 +1703,7 @@ def test_okina_institutions(app, okina):
             assert len(resp.json['data']) == 2
             assert resp.json['data'][0]['id'] == '277'
             assert resp.json['data'][0]['text'] == u'Collège Touvent'
+            assert resp.json['data'][0]['latlon'] == '46.791332586468016;1.6926355997775921'
 
             resp = app.get(endpoint, params={'insee': '36005'}, status=200)
             assert requests_get.call_args[0][0] == 'https://okina.example.net/b2b/institutions?inseeCode=36005'
@@ -1744,6 +1747,7 @@ def test_okina_search(app, okina):
         assert len(resp.json['data']) == 2
         assert resp.json['data'][0]['id'] == '3312'
         assert resp.json['data'][0]['text'] == 'Le Grand Verger'
+        assert resp.json['data'][0]['latlon'] == '46.8444186;1.708197'
         # french decimals
         resp = app.get(endpoint + '?lat=46,8&lon=1,71&address=nowhere&mode=FAR_ALL&institution=280',
                        status=200)
-- 
2.26.2

