Skip to content

Commit d69c379

Browse files
committed
Merge branch 'develop'
2 parents 01ae4f8 + 2bd6136 commit d69c379

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
1.5.2 (2024-09-10)
5+
------------------
6+
7+
**🚀 Nouveautés**
8+
9+
- Possibilité d'appeler la route `GET/areas` sans retourner les géométries (#22)
10+
411
1.5.1 (2024-01-29)
512
------------------
613

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.1
1+
1.5.2

src/ref_geo/routes.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sqlalchemy as sa
77
from sqlalchemy import func, select, asc, desc
88
from sqlalchemy.sql import text
9-
from sqlalchemy.orm import joinedload, undefer
9+
from sqlalchemy.orm import joinedload, undefer, defer
1010
from werkzeug.exceptions import BadRequest
1111

1212
from ref_geo.env import db
@@ -173,23 +173,28 @@ def get_municipalities():
173173
return jsonify(MunicipalitySchema().dump(municipalities, many=True))
174174

175175

176+
# FIXME: Transform to post and change the post /areas
176177
@routes.route("/areas", methods=["GET"])
177178
def get_areas():
178179
"""
179180
Return the areas of ref_geo.l_areas
180181
.. :quickref: Ref Geo;
181182
"""
182183
# change all args in a list of value
183-
params = {key: request.args.getlist(key) for key, value in request.args.items()}
184+
params = request.args
184185

186+
# allow to format response
187+
output_format = request.args.get("format", default="", type=str)
188+
189+
marsh_params = dict(as_geojson=(output_format == "geojson"))
185190
query = (
186191
select(LAreas)
187192
.options(joinedload("area_type").load_only("type_code"))
188193
.order_by(LAreas.area_name.asc())
189194
)
190195

191196
if "enable" in params:
192-
enable_param = params["enable"][0].lower()
197+
enable_param = params["enable"].lower()
193198
accepted_enable_values = ["true", "false", "all"]
194199
if enable_param not in accepted_enable_values:
195200
response = {
@@ -205,28 +210,35 @@ def get_areas():
205210
query = query.where(LAreas.enable == True)
206211

207212
if "id_type" in params:
208-
query = query.where(LAreas.id_type.in_(params["id_type"]))
213+
query = query.where(LAreas.id_type.in_(params.getlist("id_type")))
209214

210215
if "type_code" in params:
211-
query = query.where(LAreas.area_type.has(BibAreasTypes.type_code.in_(params["type_code"])))
216+
query = query.where(
217+
LAreas.area_type.has(BibAreasTypes.type_code.in_(params.getlist("type_code")))
218+
)
212219

213220
if "area_name" in params:
214-
query = query.where(LAreas.area_name.ilike("%{}%".format(params.get("area_name")[0])))
221+
query = query.where(LAreas.area_name.ilike("%{}%".format(params.get("area_name"))))
215222

216-
limit = int(params.get("limit")[0]) if params.get("limit") else 100
223+
without_geom = params.get("without_geom", False, lambda x: x == "true")
224+
if without_geom:
225+
query = query.options(defer("geom"))
226+
marsh_params["exclude"] = ["geom"]
217227

218-
# allow to format response
219-
format = request.args.get("format", default="", type=str)
228+
limit = int(params.get("limit")[0]) if params.get("limit") else 100
220229

221230
fields = {"area_type.type_code"}
222-
if format == "geojson":
231+
if output_format == "geojson" and not without_geom:
223232
fields |= {"+geom_4326"}
224233
query = query.options(undefer("geom_4326"))
225234

226235
areas = db.session.scalars(query.limit(limit)).unique().all()
227236

228-
response = AreaSchema(only=fields, as_geojson=format == "geojson").dump(areas, many=True)
229-
if format == "geojson":
237+
marsh_params["only"] = fields
238+
239+
response = AreaSchema(**marsh_params).dump(areas, many=True)
240+
241+
if output_format == "geojson":
230242
# retro-compat: return a list of Features instead of the FeatureCollection
231243
response = response["features"]
232244
return response

src/ref_geo/tests/test_ref_geo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ def test_get_areas_area_name(self):
296296
assert response.status_code == 200
297297
assert response.json[0]["area_name"] == CITY
298298

299+
def test_get_without_geom(self):
300+
response = self.client.get(
301+
url_for("ref_geo.get_areas"), query_string={"without_geom": "false"}
302+
)
303+
assert response.status_code == 200
304+
for area in response.json:
305+
assert "geom" in area
306+
307+
response = self.client.get(
308+
url_for("ref_geo.get_areas"), query_string={"without_geom": "true"}
309+
)
310+
assert response.status_code == 200
311+
for area in response.json:
312+
assert not "geom" in area
313+
299314
def test_get_areas_as_geojson(self, area_commune):
300315
"""
301316
This test can't try to get only one commune

0 commit comments

Comments
 (0)