Skip to content

Commit

Permalink
Feat(areas) clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Narcisi committed Apr 3, 2024
1 parent f0c09f4 commit ad7348d
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/ref_geo/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +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")
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 @@ -209,17 +210,18 @@ 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"))))

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

Check warning on line 226 in src/ref_geo/routes.py

View check run for this annotation

Codecov / codecov/patch

src/ref_geo/routes.py#L225-L226

Added lines #L225 - L226 were not covered by tests

Expand Down

0 comments on commit ad7348d

Please sign in to comment.