From ee644572b037cbba73d0ec7dde92f3f2f9ecc353 Mon Sep 17 00:00:00 2001 From: Maxime Vergez Date: Wed, 12 Oct 2022 14:25:18 +0200 Subject: [PATCH] feat: restrict area codes & taxon list interact Restrict area codes: filter out the query to search and the one that returns geoms Taxon list interaction now works with the AreaSheet --- atlas/atlasAPI.py | 35 +++++++++++-------- atlas/configuration/config_schema.py | 2 ++ .../modeles/repositories/vmAreasRepository.py | 24 ++++++++----- atlas/static/mapAreas.js | 6 +--- atlas/templates/core/listTaxons.html | 2 +- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/atlas/atlasAPI.py b/atlas/atlasAPI.py index b1b5cbc84..6e0b76575 100644 --- a/atlas/atlasAPI.py +++ b/atlas/atlasAPI.py @@ -41,7 +41,11 @@ def search_area(): search = request.args.get("search") type_code = request.args.get("type") limit = request.args.get("limit", 50) - results = vmAreasRepository.search_area_by_type(session=session, search=search, type_code=type_code, limit=limit) + results = vmAreasRepository.search_area_by_type(session=session, + search=search, + type_code=type_code, + filter_area_codes=current_app.config['AREAS_LIST'], + limit=limit) session.close() return jsonify(results) @@ -49,7 +53,10 @@ def search_area(): @api.route("/area/geom", methods=["GET"]) def get_areas_geom(): session = utils.loadSession() - results = vmAreasRepository.get_areas_geometries(session=session) + limit = request.args.get("limit", 50) + results = vmAreasRepository.get_areas_geometries(session=session, + filter_area_codes=current_app.config['AREAS_LIST'], + limit=limit) session.close() return jsonify(results) @@ -125,23 +132,23 @@ def getObservationsGenericApi(cd_ref: int): if not current_app.config['AFFICHAGE_MAILLE']: - @api.route("/observations//", methods=["GET"]) - def getObservationsCommuneTaxonAPI(insee, cd_ref): - connection = utils.engine.connect() - observations = vmObservationsRepository.getObservationTaxonCommune( - connection, insee, cd_ref + @api.route("/observations//", methods=["GET"]) + def getObservationsCommuneTaxonAPI(area_code, cd_ref): + session = utils.loadSession() + observations = vmAreasRepository.get_areas_observations_by_cd_ref( + session, area_code, cd_ref ) - connection.close() + session.close() return jsonify(observations) -@api.route("/observationsMaille//", methods=["GET"]) -def getObservationsCommuneTaxonMailleAPI(insee, cd_ref): - connection = utils.engine.connect() - observations = vmObservationsMaillesRepository.getObservationsTaxonCommuneMaille( - connection, insee, cd_ref +@api.route("/observationsMaille//", methods=["GET"]) +def getObservationsCommuneTaxonMailleAPI(area_code, cd_ref): + session = utils.loadSession() + observations = vmAreasRepository.get_areas_grid_observations_by_cd_ref( + session, area_code, cd_ref ) - connection.close() + session.close() return jsonify(observations) diff --git a/atlas/configuration/config_schema.py b/atlas/configuration/config_schema.py index 5a596fea4..d9d803f41 100644 --- a/atlas/configuration/config_schema.py +++ b/atlas/configuration/config_schema.py @@ -217,6 +217,8 @@ class Meta: TEMPLATES_AUTO_RELOAD = fields.Boolean(load_default=False) # Enable the search on other areas EXTENDED_AREAS = fields.Boolean(load_default=False) + # Restricts the search (/area) and the /area/geom API + AREAS_LIST = fields.List(fields.String, load_default=[]) @validates_schema def validate_url_taxhub(self, data, **kwargs): diff --git a/atlas/modeles/repositories/vmAreasRepository.py b/atlas/modeles/repositories/vmAreasRepository.py index 279d3b43c..8895aa484 100644 --- a/atlas/modeles/repositories/vmAreasRepository.py +++ b/atlas/modeles/repositories/vmAreasRepository.py @@ -54,6 +54,7 @@ def get_id_area(session, type_code, area_code): def get_area_from_id(session, id_area): query = ( session.query( + VmAreas.id_area, VmAreas.area_name, VmAreas.area_code, VmAreas.area_geojson, @@ -63,6 +64,7 @@ def get_area_from_id(session, id_area): result = query.first() return { + "areaId": result.id_area, "areaName": result.area_name, "areaCode": str(result.area_code), "areaGeoJson": json.loads(result.area_geojson), @@ -168,7 +170,7 @@ def get_observers_area(session, id_area): return query.all() -def search_area_by_type(session, search=None, type_code=None, limit=50): +def search_area_by_type(session, search=None, type_code=None, filter_area_codes=[], limit=50): query = ( session.query( VmAreas.id_area, @@ -186,7 +188,8 @@ def search_area_by_type(session, search=None, type_code=None, limit=50): query = query.filter(or_( VmAreas.area_name.ilike("%" + search + "%"), VmAreas.area_code.ilike("%" + search + "%"))) - + if len(filter_area_codes) > 0: + query = query.filter(VmBibAreasTypes.type_code.in_(filter_area_codes)) query = query.limit(limit) current_app.logger.debug(" query {}".format(query)) @@ -197,7 +200,7 @@ def search_area_by_type(session, search=None, type_code=None, limit=50): return areaList -def get_areas_geometries(session, type_code=None, limit=50): +def get_areas_geometries(session, type_code=None, filter_area_codes=[], limit=50): query = ( session.query( VmAreas.area_name, @@ -211,6 +214,8 @@ def get_areas_geometries(session, type_code=None, limit=50): ) if type_code is not None: query = query.filter(VmBibAreasTypes.type_code == type_code) + if len(filter_area_codes) > 0: + query = query.filter(VmBibAreasTypes.type_code.in_(filter_area_codes)) query = query.limit(limit) return FeatureCollection( [ @@ -259,7 +264,7 @@ def get_areas_observations(session, limit, id_area): return result -def get_areas_observations_by_cdnom(session, id_area): +def get_areas_observations_by_cd_ref(session, area_code, cd_ref): req = ( session.query( VmObservations.id_observation, @@ -275,19 +280,20 @@ def get_areas_observations_by_cdnom(session, id_area): VmCorAreaObservation, VmObservations.id_observation == VmCorAreaObservation.id_observation, ) - .filter(VmCorAreaObservation.id_area == id_area) + .join(VmAreas, VmAreas.id_area == VmCorAreaObservation.id_area) + .filter(VmAreas.area_code == area_code, VmObservations.cd_ref == cd_ref) ).all() result = [] for r in req: temp = r._asdict() - temp["geometry"] = json.loads(r.geometry or "{}") + temp["geojson_point"] = json.loads(r.geometry or "{}") temp["dateobs"] = str(r.dateobs) temp["group2_inpn"] = utils.deleteAccent(r.group2_inpn) result.append(temp) return result -def get_areas_grid_observations_by_cdnom(session, id_area, cd_nom): +def get_areas_grid_observations_by_cd_ref(session, area_code, cd_ref): query = ( session.query( TGrid.id_maille, @@ -298,7 +304,7 @@ def get_areas_grid_observations_by_cdnom(session, id_area, cd_nom): ) .join(VmAreas, VmAreas.the_geom.st_intersects(VmObservations.the_geom_point)) .join(TGrid, TGrid.the_geom.st_intersects(VmObservations.the_geom_point)) - .filter(and_(VmObservations.cd_ref == cd_nom, VmAreas.area_code == id_area)) + .filter(and_(VmObservations.cd_ref == cd_ref, VmAreas.area_code == area_code)) .order_by(TGrid.id_maille) ) @@ -385,7 +391,7 @@ def get_surrounding_areas(session, id_area): VmBibAreasTypes.type_name, ) .join(VmBibAreasTypes, VmAreas.id_type == VmBibAreasTypes.id_type) - .filter(and_(VmAreas.the_geom.st_intersects(subquery.c.the_geom.st_buffer(0)), VmAreas != id_area)) + .filter(and_(VmAreas != id_area, VmAreas.the_geom.st_intersects(subquery.c.the_geom.st_buffer(0)))) ) return query.all() diff --git a/atlas/static/mapAreas.js b/atlas/static/mapAreas.js index 66e00b73f..f859125c7 100644 --- a/atlas/static/mapAreas.js +++ b/atlas/static/mapAreas.js @@ -102,11 +102,7 @@ function displayObsPreciseBaseUrl(areaCode, cd_ref) { } function displayObsGridBaseUrl() { - if (sheetType === 'commune') { - return configuration.URL_APPLICATION + "/api/observationsMaille/" - } else { - return configuration.URL_APPLICATION + "/api/observationsMaille/area/" - } + return configuration.URL_APPLICATION + "/api/observationsMaille/" } // display observation on click diff --git a/atlas/templates/core/listTaxons.html b/atlas/templates/core/listTaxons.html index 031b613a1..1088822ee 100644 --- a/atlas/templates/core/listTaxons.html +++ b/atlas/templates/core/listTaxons.html @@ -18,7 +18,7 @@
    {% for taxon in listTaxons.taxons %} -
  • {{ taxon.group2_inpn }}