Skip to content

Commit

Permalink
fix(general_stats): re-enable cruved on the stats computed (#3327)
Browse files Browse the repository at this point in the history
* fix(general_stats): fix problem where cruved had no impact on the stats computed
  • Loading branch information
jacquesfize authored Jan 15, 2025
1 parent 03322ed commit 3361d27
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
19 changes: 9 additions & 10 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,20 +943,19 @@ def general_stats(permissions):
results = {"nb_allowed_datasets": nb_allowed_datasets}

queries = {
"nb_obs": select(
func.count(Synthese.id_synthese),
),
"nb_obs": select(Synthese.id_synthese),
"nb_distinct_species": select(
func.count(func.distinct(Synthese.cd_nom)),
),
"nb_distinct_observer": select(
func.count(func.distinct(Synthese.observers)),
func.distinct(Synthese.cd_nom),
),
"nb_distinct_observer": select(func.distinct(Synthese.observers)),
}

for key, query in queries.items():
synthese_query = SyntheseQuery(Synthese, query, {})
synthese_query.filter_query_with_cruved(g.current_user, permissions)
results[key] = db.session.scalar(synthese_query.query)
synthese_query.filter_query_with_permissions(g.current_user, permissions)
results[key] = db.session.scalar(
sa.select(func.count("*")).select_from(synthese_query.query)
)

data = {
"nb_data": results["nb_obs"],
Expand Down Expand Up @@ -1561,7 +1560,7 @@ def list_all_reports(permissions):
# On vérifie les permissions en lecture sur la synthese
synthese_query = select(Synthese.id_synthese).select_from(Synthese)
synthese_query_obj = SyntheseQuery(Synthese, synthese_query, {})
synthese_query_obj.filter_query_with_cruved(g.current_user, permissions)
synthese_query_obj.filter_query_with_permissions(g.current_user, permissions)
cte_synthese = synthese_query_obj.query.cte("cte_synthese")
query = query.where(TReport.id_synthese == cte_synthese.c.id_synthese)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ def filter_query_with_permissions(self, user, permissions):
where_clause = self.build_permissions_filter(user=user, permissions=permissions)
self.query = self.query.where(where_clause)

def filter_query_with_cruved(self, user, scope):
def filter_query_with_cruved(self, user, scope: int):
"""
Filter the query with the cruved authorization of a user
"""
assert isinstance(scope, int)
if scope in (1, 2):
# get id synthese where user is observer
subquery_observers = (
Expand Down
6 changes: 6 additions & 0 deletions backend/geonature/tests/benchmarks/test_benchmark_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
class TestBenchmarkHome:

test_general_stats = BenchmarkTest(
CLIENT_GET,
[CLater("""url_for("gn_synthese.general_stats")""")],
dict(user_profile="user", fixtures=[]),
)()

test_general_stats_admin = BenchmarkTest(
CLIENT_GET,
[CLater("""url_for("gn_synthese.general_stats")""")],
dict(user_profile="admin_user", fixtures=[]),
Expand Down

0 comments on commit 3361d27

Please sign in to comment.