Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(areas) add a param too remove geom from query #22

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/ref_geo/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sqlalchemy as sa
from sqlalchemy import func, select, asc, desc
from sqlalchemy.sql import text
from sqlalchemy.orm import joinedload, undefer
from sqlalchemy.orm import joinedload, undefer, defer
from werkzeug.exceptions import BadRequest

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


# FIXME: Transform to post and change the post /areas
@routes.route("/areas", methods=["GET"])
def get_areas():
"""
Return the areas of ref_geo.l_areas
.. :quickref: Ref Geo;
"""
# change all args in a list of value
params = {key: request.args.getlist(key) for key, value in request.args.items()}
params = request.args

# allow to format response
output_format = request.args.get("format", default="", type=str)

marsh_params = dict(as_geojson=(output_format == "geojson"))
query = (
select(LAreas)
.options(joinedload("area_type").load_only("type_code"))
.order_by(LAreas.area_name.asc())
)

if "enable" in params:
enable_param = params["enable"][0].lower()
enable_param = params["enable"].lower()
accepted_enable_values = ["true", "false", "all"]
if enable_param not in accepted_enable_values:
response = {
Expand All @@ -205,28 +210,35 @@ def get_areas():
query = query.where(LAreas.enable == True)

if "id_type" in params:
query = query.where(LAreas.id_type.in_(params["id_type"]))
query = query.where(LAreas.id_type.in_(params.getlist("id_type")))

if "type_code" in params:
query = query.where(LAreas.area_type.has(BibAreasTypes.type_code.in_(params["type_code"])))
query = query.where(
LAreas.area_type.has(BibAreasTypes.type_code.in_(params.getlist("type_code")))
)

if "area_name" in params:
query = query.where(LAreas.area_name.ilike("%{}%".format(params.get("area_name")[0])))
query = query.where(LAreas.area_name.ilike("%{}%".format(params.get("area_name"))))

limit = int(params.get("limit")[0]) if params.get("limit") else 100
without_geom = params.get("without_geom", False, lambda x: x == "true")
if without_geom:
query = query.options(defer("geom"))
marsh_params["exclude"] = ["geom"]

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

fields = {"area_type.type_code"}
if format == "geojson":
if output_format == "geojson" and not without_geom:
fields |= {"+geom_4326"}
query = query.options(undefer("geom_4326"))

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

response = AreaSchema(only=fields, as_geojson=format == "geojson").dump(areas, many=True)
if format == "geojson":
marsh_params["only"] = fields

response = AreaSchema(**marsh_params).dump(areas, many=True)

if output_format == "geojson":
# retro-compat: return a list of Features instead of the FeatureCollection
response = response["features"]
return response
Expand Down
15 changes: 15 additions & 0 deletions src/ref_geo/tests/test_ref_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,21 @@ def test_get_areas_area_name(self):
assert response.status_code == 200
assert response.json[0]["area_name"] == CITY

def test_get_without_geom(self):
response = self.client.get(
url_for("ref_geo.get_areas"), query_string={"without_geom": "false"}
)
assert response.status_code == 200
for area in response.json:
assert "geom" in area

response = self.client.get(
url_for("ref_geo.get_areas"), query_string={"without_geom": "true"}
)
assert response.status_code == 200
for area in response.json:
assert not "geom" in area

def test_get_areas_as_geojson(self, area_commune):
"""
This test can't try to get only one commune
Expand Down
Loading