Skip to content

Commit

Permalink
Fix filter type site label (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Oct 14, 2024
1 parent 275571a commit 15e5466
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
8 changes: 7 additions & 1 deletion backend/gn_module_monitoring/monitoring/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ def filter_by_params(cls, query: Select, params: MultiDict = None, **kwargs):
if "modules" in params:
query = query.filter(cls.modules.any(id_module=params["modules"]))
params.pop("modules")
if "types_site_label" in params:
value = params["types_site_label"]
join_types_site = aliased(Models.BibTypeSite)
join_nomenclature_type_site = aliased(TNomenclatures)
query = query.join(join_types_site, cls.types_site)
query = query.join(join_nomenclature_type_site, join_types_site.nomenclature)
query = query.filter(join_nomenclature_type_site.label_default.ilike(f"%{value}%"))
if "types_site" in params:
value = params["types_site"]
if not isinstance(value, list):
Expand All @@ -131,7 +138,6 @@ def filter_by_params(cls, query: Select, params: MultiDict = None, **kwargs):
)

query = super().filter_by_params(query, params)

return query

@classmethod
Expand Down
10 changes: 8 additions & 2 deletions backend/gn_module_monitoring/routes/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ def get_all_site_geometries(object_type):
types_site = []
if "types_site" in params:
types_site = request.args.getlist("types_site")
params["types_site"] = types_site
if not types_site[0].isdigit():
# HACK gestionnaire des sites
# Quand filtre sur type de site envoie une chaine de caractère
params["types_site_label"] = types_site[0]
params.pop("types_site")
types_site = None
else:
params["types_site"] = types_site

query = select(TMonitoringSites)
query_allowed = TMonitoringSites.filter_by_readable(query=query, object_code=object_code)
Expand All @@ -181,7 +188,6 @@ def get_all_site_geometries(object_type):
query=query_allowed, id_types_site=types_site, params=params
)
subquery = query_allowed.subquery()

result = geojson_query(subquery)

return jsonify(result)
Expand Down
28 changes: 28 additions & 0 deletions backend/gn_module_monitoring/tests/test_routes/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ def test_get_all_site_geometries_filter_utils(
assert r.status_code == 200
assert len(features) == 1

def test_get_all_site_geometries_filter_type_sites(
self, sites_with_data_typeutils, monitorings_users, users
):
set_logged_user_cookie(self.client, monitorings_users["admin_user"])
# types_site = [s.types_site[0].id_nomenclature_type_site for s in sites_with_data_typeutils]
types_site = [s for s in sites_with_data_typeutils]
id_nomenclature_type_site = (
sites_with_data_typeutils[types_site[0]].types_site[0].id_nomenclature_type_site
)
r = self.client.get(
url_for("monitorings.get_all_site_geometries", types_site=id_nomenclature_type_site)
)
json_resp = r.json
features = json_resp.get("features")
assert r.status_code == 200
assert len(features) == 1
nom_type_site = list(sites_with_data_typeutils.keys())[0]
r = self.client.get(
url_for(
"monitorings.get_all_site_geometries",
types_site=nom_type_site,
)
)
json_resp = r.json
features = json_resp.get("features")
assert r.status_code == 200
assert len(features) == 1

# def test_get_module_by_id_base_site(self, sites, monitoring_module, monitorings_users):

# set_logged_user_cookie(self.client, monitorings_users["admin_user"])
Expand Down

0 comments on commit 15e5466

Please sign in to comment.