From 5098ceae765b60af5a6cd2647394d4a3d5420dbb Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Mon, 13 Jul 2020 17:16:59 +0200
Subject: [PATCH] planitec: eventually ignore places with no capacity (#45076)

If a filtering on capactiy is explicitly asked,
ignore places with no capacity.
---
 passerelle/contrib/planitech/models.py | 16 ++++++++++++----
 tests/test_planitech.py                | 20 +++++++++++++++++---
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/passerelle/contrib/planitech/models.py b/passerelle/contrib/planitech/models.py
index b23c11d7..7df76b9a 100644
--- a/passerelle/contrib/planitech/models.py
+++ b/passerelle/contrib/planitech/models.py
@@ -37,6 +37,10 @@ from passerelle.utils.api import endpoint
 from passerelle.utils.jsonresponse import APIError
 
 
+DEFAULT_MIN_CAPACITY = 0
+DEFAULT_MAX_CAPACITY = 100000
+
+
 CREATE_RESERVATION_SCHEMA = {
     "$schema": "http://json-schema.org/draft-04/schema#",
     "title": "Planitech createreservation",
@@ -353,7 +357,9 @@ class PlanitechConnector(BaseResource):
             cache.set(cache_key, ref)
         return ref
 
-    def _get_places_referential(self, min_capacity=0, max_capacity=100000, **kwargs):
+    def _get_places_referential(
+            self, min_capacity=DEFAULT_MIN_CAPACITY, max_capacity=DEFAULT_MAX_CAPACITY, **kwargs
+    ):
 
         ref = self._raw_get_places_referential()
         res = {}
@@ -365,7 +371,9 @@ class PlanitechConnector(BaseResource):
 
         for place_id, place_data in ref.items():
             # Filter on capacity
-            if 'capacity' in place_data:
+            if min_capacity != DEFAULT_MIN_CAPACITY or max_capacity != DEFAULT_MAX_CAPACITY:
+                if place_data.get('capacity') is None:
+                    continue
                 if place_data['capacity'] < min_capacity or place_data['capacity'] > max_capacity:
                     continue
 
@@ -636,8 +644,8 @@ class PlanitechConnector(BaseResource):
             },
         })
     def getfreegaps(
-            self, request, display, start_time, end_time, min_capacity=0, start_date=None,
-            start_days=None, end_date=None, end_days=None, max_capacity=100000, weekdays=None,
+            self, request, display, start_time, end_time, min_capacity=DEFAULT_MIN_CAPACITY, start_date=None,
+            start_days=None, end_date=None, end_days=None, max_capacity=DEFAULT_MAX_CAPACITY, weekdays=None,
             place_id=None, **kwargs):
 
         # Additional parameters check
diff --git a/tests/test_planitech.py b/tests/test_planitech.py
index 63ed85e5..42250b47 100644
--- a/tests/test_planitech.py
+++ b/tests/test_planitech.py
@@ -360,7 +360,8 @@ def test_getplaces_referential(app, connector, monkeypatch):
             'placesList': [
                 {'identifier': 1.0, 'label': 'salle 1'},
                 {'identifier': 2.0, 'label': 'salle 2'},
-                {'identifier': 3.0, 'label': 'salle 3'}
+                {'identifier': 3.0, 'label': 'salle 3'},
+                {'identifier': 4.0, 'label': 'salle 4'}
             ]
         },
         {
@@ -377,6 +378,9 @@ def test_getplaces_referential(app, connector, monkeypatch):
                 {
                     'identifier': 3.0, 'capacity': 30.0,
                     'some_custom_field': 'Yes'
+                },
+                {
+                    'identifier': 4.0, 'capacity': None
                 }
             ]
         }
@@ -384,6 +388,11 @@ def test_getplaces_referential(app, connector, monkeypatch):
     mock_planitech(monkeypatch, side_effect=side_effect)
     response = app.get('/planitech/slug-planitech/getplacesreferential')
     expected_res = {
+        '4': {
+            'capacity': None, 'label': 'salle 4', 'identifier': 4,
+            'street_number': None, 'address': None,
+            'city': None, 'zipcode': None, 'some_custom_field': None
+        },
         '3': {
             u'capacity': 30, u'label': u'salle 3', u'identifier': 3,
             'street_number': None, 'address': None,
@@ -435,6 +444,11 @@ def test_getplaces_referential(app, connector, monkeypatch):
     # Unkown filter filters nothing
     response = app.get('/planitech/slug-planitech/getplacesreferential?unkownfilter=foo')
     expected_res = {
+        '4': {
+            'capacity': None, 'label': 'salle 4', 'identifier': 4,
+            'street_number': None, 'address': None,
+            'city': None, 'zipcode': None, 'some_custom_field': None
+        },
         '3': {
             u'capacity': 30, u'label': u'salle 3', u'identifier': 3,
             'street_number': None, 'address': None,
@@ -549,9 +563,9 @@ def test_getplaces_referential_no_configuration(app, connector, monkeypatch):
 
 def test_getplaces_referential_use_cache(app, connector):
     cache_key = 'planitech-%s-places' % connector.id
-    cache.set(cache_key, {'some': 'data'})
+    cache.set(cache_key, {'some': {'data': 'foo'}})
     response = app.get('/planitech/slug-planitech/getplacesreferential')
-    assert response.json_body['data'] == {'some': 'data'}
+    assert response.json_body['data'] == {'some': {'data': 'foo'}}
     cache.delete(cache_key)
 
 
-- 
2.20.1

