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

[Fiche Taxon] Ajustement de la structure de config + Renommage "espèce" en "taxon" + Ajout test manquant de taxon_stats #3205

6 changes: 3 additions & 3 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,11 @@ def general_stats(permissions):
return data


@routes.route("/species_stats/<int:cd_ref>", methods=["GET"])
@routes.route("/taxon_stats/<int:cd_ref>", methods=["GET"])
@permissions.check_cruved_scope("R", get_scope=True, module_code="SYNTHESE")
@json_resp
def species_stats(scope, cd_ref):
"""Return stats about distinct species."""
def taxon_stats(scope, cd_ref):
"""Return stats for a specific taxon"""

area_type = request.args.get("area_type")

Expand Down
74 changes: 0 additions & 74 deletions backend/geonature/core/gn_synthese/synthese_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,77 +100,3 @@
{"prop": "dataset_name", "name": "JDD", "max_width": 200},
{"prop": "observers", "name": "observateur", "max_width": 200},
]


class DefaultProfile:
ENABLED = True
## DEFAULT PROFILE INDICATORS
LIST_INDICATORS = [
{
"name": "observation(s) valide(s)",
"matIcon": "search",
"field": "count_valid_data",
"type": "number",
},
{
"name": "Première observation",
"matIcon": "schedule",
"field": "first_valid_data",
"type": "date",
},
{
"name": "Dernière observation",
"matIcon": "search",
"field": "last_valid_data",
"type": "date",
},
{
"name": "Plage d'altitude(s)",
"matIcon": "terrain",
"field": ["altitude_min", "altitude_max"],
"unit": "m",
"type": "number",
},
]


class DefaultGeographicOverview:
pass


class DefaultSpeciesSheet:
## DEFAULT SPECIES SHEET INDICATORS
LIST_INDICATORS = [
{
"name": "observation(s)",
"matIcon": "search",
"field": "observation_count",
"type": "number",
},
{
"name": "observateur(s)",
"matIcon": "people",
"field": "observer_count",
"type": "number",
},
{
"name": "commune(s)",
"matIcon": "location_on",
"field": "area_count",
"type": "number",
},
{
"name": "Plage d'altitude(s)",
"matIcon": "terrain",
"unit": "m",
"type": "number",
"field": ["altitude_min", "altitude_max"],
},
{
"name": "Plage d'observation(s)",
"matIcon": "date_range",
"type": "date",
"field": ["date_min", "date_max"],
"separator": "-",
},
]
165 changes: 152 additions & 13 deletions backend/geonature/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,24 @@ def sources_modules(modules):


def create_synthese(
geom, taxon, user, dataset, source, uuid=func.uuid_generate_v4(), cor_observers=[], **kwargs
geom,
taxon,
user,
dataset,
source,
uuid=func.uuid_generate_v4(),
cor_observers=[],
date_min="",
date_max="",
altitude_min=800,
altitude_max=1200,
**kwargs,
):
now = datetime.datetime.now()

date_min = date_min if date_min else now
date_max = date_max if date_max else now

return Synthese(
id_source=source.id_source,
id_module=source.id_module,
Expand All @@ -522,8 +536,10 @@ def create_synthese(
the_geom_4326=geom,
the_geom_point=geom,
the_geom_local=func.ST_Transform(geom, 2154), # FIXME
date_min=now,
date_max=now,
date_min=date_min,
date_max=date_max,
altitude_min=altitude_min,
altitude_max=altitude_max,
cor_observers=cor_observers,
**kwargs,
)
Expand All @@ -534,19 +550,138 @@ def synthese_data(app, users, datasets, source, sources_modules):
point1 = Point(5.92, 45.56)
point2 = Point(-1.54, 46.85)
point3 = Point(-3.486786, 48.832182)
date_1 = datetime.datetime(2024, 10, 2, 11, 22, 33)
date_2 = datetime.datetime(2024, 10, 3, 8, 9, 10)
date_3 = datetime.datetime(2024, 10, 4, 17, 4, 9)
date_4 = datetime.datetime(2024, 10, 5, 22, 22, 22)
altitude_1 = 800
altitude_2 = 900
altitude_3 = 1000
altitude_4 = 1100

data = {}
with db.session.begin_nested():
for name, cd_nom, point, ds, comment_description, source_m in [
for (
name,
cd_nom,
point,
ds,
comment_description,
source_m,
date_min,
date_max,
altitude_min,
altitude_max,
) in [
# Donnnées de gypaète : possède des statuts de protection nationale
("obs1", 2852, point1, datasets["own_dataset"], "obs1", sources_modules[0]),
("obs2", 212, point2, datasets["own_dataset"], "obs2", sources_modules[0]),
("obs3", 2497, point3, datasets["own_dataset"], "obs3", sources_modules[1]),
("p1_af1", 713776, point1, datasets["belong_af_1"], "p1_af1", sources_modules[1]),
("p1_af1_2", 212, point1, datasets["belong_af_1"], "p1_af1_2", sources_modules[1]),
("p1_af2", 212, point1, datasets["belong_af_2"], "p1_af2", sources_modules[1]),
("p2_af2", 2497, point2, datasets["belong_af_2"], "p2_af2", source),
("p2_af1", 2497, point2, datasets["belong_af_1"], "p2_af1", source),
("p3_af3", 2497, point3, datasets["belong_af_3"], "p3_af3", source),
(
"obs1",
2852,
point1,
datasets["own_dataset"],
"obs1",
sources_modules[0],
date_1,
date_1,
altitude_1,
altitude_1,
),
(
"obs2",
212,
point2,
datasets["own_dataset"],
"obs2",
sources_modules[0],
date_1,
date_4,
altitude_1,
altitude_4,
),
(
"obs3",
2497,
point3,
datasets["own_dataset"],
"obs3",
sources_modules[1],
date_2,
date_3,
altitude_2,
altitude_3,
),
(
"p1_af1",
713776,
point1,
datasets["belong_af_1"],
"p1_af1",
sources_modules[1],
date_1,
date_3,
altitude_1,
altitude_3,
),
(
"p1_af1_2",
212,
point1,
datasets["belong_af_1"],
"p1_af1_2",
sources_modules[1],
date_3,
date_3,
altitude_3,
altitude_3,
),
(
"p1_af2",
212,
point1,
datasets["belong_af_2"],
"p1_af2",
sources_modules[1],
date_3,
date_4,
altitude_3,
altitude_4,
),
(
"p2_af2",
2497,
point2,
datasets["belong_af_2"],
"p2_af2",
source,
date_1,
date_2,
altitude_1,
altitude_2,
),
(
"p2_af1",
2497,
point2,
datasets["belong_af_1"],
"p2_af1",
source,
date_1,
date_1,
altitude_1,
altitude_1,
),
(
"p3_af3",
2497,
point3,
datasets["belong_af_3"],
"p3_af3",
source,
date_2,
date_2,
altitude_2,
altitude_2,
),
]:
unique_id_sinp = (
"f4428222-d038-40bc-bc5c-6e977bbbc92b" if not data else func.uuid_generate_v4()
Expand All @@ -563,6 +698,10 @@ def synthese_data(app, users, datasets, source, sources_modules):
source_m,
unique_id_sinp,
[users["admin_user"], users["user"]],
date_min,
date_max,
altitude_min,
altitude_max,
**kwargs,
)
db.session.add(s)
Expand Down
57 changes: 57 additions & 0 deletions backend/geonature/tests/test_synthese.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,63 @@ def test_general_stat(self, users):

assert response.status_code == 200

def test_taxon_stats(self, synthese_data, users):
set_logged_user(self.client, users["stranger_user"])

AREA_TYPE_VALID = "COM"
AREA_TYPE_INVALID = "UNDEFINED"
CD_REF_INVALID = 987654321
CD_REF_INVALID_STATS = {
"altitude_max": None,
"altitude_min": None,
"area_count": 0,
"cd_ref": CD_REF_INVALID,
"date_max": None,
"date_min": None,
"observation_count": 0,
"observer_count": 0,
}
CD_REF_VALID = 2497
CD_REF_VALID_STATS = {
"altitude_max": 900,
"altitude_min": 800,
"area_count": 2,
"cd_ref": CD_REF_VALID,
"date_max": "Thu, 03 Oct 2024 08:09:10 GMT",
"date_min": "Wed, 02 Oct 2024 11:22:33 GMT",
"observation_count": 3,
"observer_count": 1,
}

# Missing area_type parameter
response = self.client.get(
url_for("gn_synthese.taxon_stats", cd_ref=CD_REF_VALID),
)
assert response.status_code == 400
assert response.json["description"] == "Missing area_type parameter"

# Invalid area_type parameter
response = self.client.get(
url_for("gn_synthese.taxon_stats", cd_ref=CD_REF_VALID, area_type=AREA_TYPE_INVALID),
)
assert response.status_code == 400
assert response.json["description"] == "Invalid area_type"

# Invalid cd_ref parameter
response = self.client.get(
url_for("gn_synthese.taxon_stats", cd_ref=CD_REF_INVALID, area_type=AREA_TYPE_VALID),
)
assert response.status_code == 200
assert response.get_json() == CD_REF_INVALID_STATS

# Invalid cd_ref parameter
response = self.client.get(
url_for("gn_synthese.taxon_stats", cd_ref=CD_REF_VALID, area_type=AREA_TYPE_VALID),
)
response_json = response.get_json()
assert response.status_code == 200
assert response.get_json() == CD_REF_VALID_STATS

def test_get_one_synthese_record(self, app, users, synthese_data):
response = self.client.get(
url_for("gn_synthese.get_one_synthese", id_synthese=synthese_data["obs1"].id_synthese)
Expand Down
Loading
Loading