Skip to content

Commit

Permalink
Merge branch 'develop' into test-rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
edelclaux committed Jan 15, 2025
2 parents 8aa1aed + 3361d27 commit c750e03
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 93 deletions.
12 changes: 10 additions & 2 deletions backend/geonature/core/command/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import click
from flask.cli import run_command

from geonature.utils.env import GEONATURE_VERSION
from geonature.utils.env import GEONATURE_VERSION, ROOT_DIR
from geonature.utils.module import iter_modules_dist
from geonature import create_app
from geonature.utils.config import config
Expand All @@ -19,6 +19,8 @@
create_frontend_module_config,
build_frontend,
)
from os.path import join
import glob

from flask.cli import FlaskGroup

Expand Down Expand Up @@ -57,7 +59,13 @@ def dev_back(ctx, host, port):
"""
if not environ.get("FLASK_DEBUG"):
environ["FLASK_DEBUG"] = "true"
ctx.invoke(run_command, host=host, port=port)

ctx.invoke(
run_command,
host=host,
port=port,
extra_files=[file for file in glob.glob(join(ROOT_DIR, "config", "*.toml"))],
)


@main.command()
Expand Down
8 changes: 1 addition & 7 deletions backend/geonature/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ def handle_unauthenticated_request(e):
else:
base_url = current_app.config["URL_APPLICATION"]
login_path = "/#/login" # FIXME: move in config
api_endpoint = current_app.config["API_ENDPOINT"]
url_application = current_app.config["URL_APPLICATION"]
if urlparse(api_endpoint).netloc == urlparse(url_application).netloc:
next_url = request.full_path
else:
next_url = request.url
query_string = urlencode({"next": next_url})
query_string = urlencode({"next": request.url})
return redirect(f"{base_url}{login_path}?{query_string}")


Expand Down
21 changes: 0 additions & 21 deletions backend/geonature/core/gn_meta/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,6 @@ def user_actors(self):
def organism_actors(self):
return [actor.organism for actor in self.cor_dataset_actor if actor.organism is not None]

@hybrid_property
def obs_count(self):
from geonature.core.gn_synthese.models import Synthese

return db.session.scalar(
select(func.count(Synthese.id_synthese))
.select_from(Synthese)
.where(Synthese.id_dataset == self.id_dataset)
)

@hybrid_property
def hab_count(self):
from gn_module_occhab.models import OccurenceHabitat, Station

return db.session.scalar(
select(func.count(OccurenceHabitat.id_habitat))
.select_from(OccurenceHabitat)
.where(Station.id_station == OccurenceHabitat.id_station)
.where(Station.id_dataset == self.id_dataset)
)

def is_deletable(self):
return not DB.session.execute(self.synthese_records.exists().select()).scalar()

Expand Down
2 changes: 0 additions & 2 deletions backend/geonature/core/gn_meta/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class Meta:
cor_territories = MA.Nested(NomenclatureSchema, many=True, unknown=EXCLUDE)
acquisition_framework = MA.Nested("AcquisitionFrameworkSchema", dump_only=True)
sources = MA.Nested(SourceSchema, many=True, dump_only=True)
obs_count = fields.Int(dump_only=True)
hab_count = fields.Int(dump_only=True)

@post_dump(pass_many=False, pass_original=True)
def module_input(self, item, original, many, **kwargs):
Expand Down
38 changes: 22 additions & 16 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@

from geonature import app


routes = Blueprint("gn_synthese", __name__)


Expand Down Expand Up @@ -946,21 +945,28 @@ def general_stats(permissions):
.select_from(TDatasets)
.where(TDatasets.filter_by_readable().whereclause)
)
query = select(
func.count(Synthese.id_synthese),
func.count(func.distinct(Synthese.cd_nom)),
func.count(func.distinct(Synthese.observers)),
)
synthese_query_obj = SyntheseQuery(Synthese, query, {})
synthese_query_obj.filter_query_with_cruved(g.current_user, permissions)
result = DB.session.execute(synthese_query_obj.query)
synthese_counts = result.fetchone()
results = {"nb_allowed_datasets": nb_allowed_datasets}

queries = {
"nb_obs": select(Synthese.id_synthese),
"nb_distinct_species": select(
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_permissions(g.current_user, permissions)
results[key] = db.session.scalar(
sa.select(func.count("*")).select_from(synthese_query.query)
)

data = {
"nb_data": synthese_counts[0],
"nb_species": synthese_counts[1],
"nb_observers": synthese_counts[2],
"nb_dataset": nb_allowed_datasets,
"nb_data": results["nb_obs"],
"nb_species": results["nb_distinct_species"],
"nb_observers": results["nb_distinct_observer"],
"nb_dataset": results["nb_allowed_datasets"],
}
return data

Expand Down Expand Up @@ -1035,7 +1041,7 @@ def taxon_stats(scope, cd_ref):

@routes.route("/taxon_observers/<int:cd_ref>", methods=["GET"])
@permissions.check_cruved_scope("R", get_scope=True, module_code="SYNTHESE")
# @json_resp
@json_resp
def taxon_observers(scope, cd_ref):
per_page = request.args.get("per_page", 10, int)
page = request.args.get("page", 1, int)
Expand Down Expand Up @@ -1590,7 +1596,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
3 changes: 2 additions & 1 deletion backend/geonature/core/sensitivity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ def insert_sensitivity_referential(source, csvfile):


def remove_sensitivity_referential(source):
return db.session.execute(sa.delete(SensitivityRule).where(SensitivityRule.source == source))
whereclause = SensitivityRule.source == source
return db.session.execute(sa.delete(SensitivityRule).where(whereclause)).rowcount
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""create_index_on_synthese_observers
Revision ID: 5cf0ce9e669c
Revises: 9df933cc3c7a
Create Date: 2025-01-13 14:14:30.085725
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "5cf0ce9e669c"
down_revision = "9df933cc3c7a"
branch_labels = None
depends_on = None


def upgrade():
op.create_index(
"synthese_observers_idx",
"synthese",
["observers"],
if_not_exists=True,
schema="gn_synthese",
)


def downgrade():
op.drop_index(
"synthese_observers_idx",
table_name="synthese",
schema="gn_synthese",
)
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,6 @@
{% if dataset['dataset_name']: %}
{{ dataset['dataset_name'] }}
{% endif %}
<br>
{% if dataset['obs_count'] > 0: %}
Nombre d'observations dans la Synthèse : {{ dataset['obs_count'] }}
{% endif %}
<br>
{% if dataset['hab_count'] > 0: %}
Nombre d'habitats dans occhab : {{ dataset['hab_count'] }}
{% endif %}
</p>
</div>
{%- endfor %}
Expand Down
30 changes: 30 additions & 0 deletions backend/geonature/tests/benchmarks/test_benchmark_home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import logging
import pytest
from geonature.tests.benchmarks import *
from geonature.tests.test_pr_occhab import stations

from .benchmark_generator import BenchmarkTest, CLater
from .utils import activate_profiling_sql

logging.basicConfig()
logger = logging.getLogger("logger-name")
logger.setLevel(logging.DEBUG)

from .utils import CLIENT_GET, CLIENT_POST


@pytest.mark.benchmark(group="home")
@pytest.mark.usefixtures("client_class", "temporary_transaction", "activate_profiling_sql")
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=[]),
)()
39 changes: 39 additions & 0 deletions backend/geonature/tests/test_sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,35 @@
from ref_geo.models import LAreas, BibAreasTypes
from apptax.taxonomie.models import Taxref
from pypnnomenclature.models import TNomenclatures, BibNomenclaturesTypes
from geonature.core.sensitivity.utils import remove_sensitivity_referential
from click.testing import CliRunner


@pytest.fixture
def sensitivity_rule_source_name():
return "sensitivity_rules_test"


@pytest.fixture(scope="function")
def sensitivity_rules(sensitivity_rule_source_name):
with db.session.begin_nested():
cd_nom = db.session.scalar(sa.select(Taxref.cd_nom).limit(1))
sensitivity_nomenc_type = db.session.execute(
sa.select(BibNomenclaturesTypes).filter_by(mnemonique="SENSIBILITE")
).scalar_one()
diffusion_maille = db.session.execute(
sa.select(TNomenclatures).filter_by(
id_type=sensitivity_nomenc_type.id_type, mnemonique="2"
)
).scalar_one()
for _ in range(100):
rule = SensitivityRule(
cd_nom=cd_nom,
sensitivity_duration=5,
source=sensitivity_rule_source_name,
id_nomenclature_sensitivity=diffusion_maille.id_nomenclature,
)
db.session.add(rule)


@pytest.fixture(scope="function")
Expand All @@ -28,8 +57,14 @@ def clean_all_sensitivity_rules():
db.session.execute(sa.delete(SensitivityRule))


@pytest.fixture
def client_click():
return CliRunner()


@pytest.mark.usefixtures("client_class", "temporary_transaction", "clean_all_sensitivity_rules")
class TestSensitivity:

def test_get_id_nomenclature_sensitivity(self, app):
taxon = db.session.scalars(sa.select(Taxref)).first()
geom = WKTElement("POINT(6.15 44.85)", srid=4326)
Expand Down Expand Up @@ -363,3 +398,7 @@ def test_synthese_sensitivity(self, app, source):
s.date_max = date_obs
db.session.refresh(s)
assert s.id_nomenclature_sensitivity == nomenc_not_sensitive.id_nomenclature

def test_remove_sensitivy_rule(self, sensitivity_rules, sensitivity_rule_source_name):
res = remove_sensitivity_referential(sensitivity_rule_source_name)
assert res == 100
2 changes: 1 addition & 1 deletion backend/requirements-dependencies.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pypnnomenclature>=1.6.4,<2
pypn_habref_api>=0.4.1,<1
utils-flask-sqlalchemy-geo>=0.3.2,<1
utils-flask-sqlalchemy>=0.4.1,<1
taxhub==2.1.0
taxhub==2.1.1
pypn-ref-geo>=1.5.3,<2
6 changes: 4 additions & 2 deletions backend/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ cligj==0.7.2
# via fiona
contourpy==1.3.0
# via bokeh
cryptography==44.0.0
cryptography==43.0.3
# via authlib
cssselect2==0.7.0
# via weasyprint
Expand Down Expand Up @@ -180,7 +180,9 @@ flask-wtf==1.2.2
# -r requirements-common.in
# usershub
fonttools[woff]==4.55.3
# via weasyprint
# via
# fonttools
# weasyprint
geoalchemy2==0.16.0
# via utils-flask-sqlalchemy-geo
geojson==3.2.0
Expand Down
8 changes: 5 additions & 3 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ cligj==0.7.2
# via fiona
contourpy==1.3.0
# via bokeh
cryptography==44.0.0
cryptography==43.0.3
# via authlib
cssselect2==0.7.0
# via weasyprint
Expand Down Expand Up @@ -143,7 +143,9 @@ flask-weasyprint==1.1.0
flask-wtf==1.2.2
# via -r requirements-common.in
fonttools[woff]==4.55.3
# via weasyprint
# via
# fonttools
# weasyprint
geoalchemy2==0.16.0
# via utils-flask-sqlalchemy-geo
geojson==3.2.0
Expand Down Expand Up @@ -312,7 +314,7 @@ sqlalchemy==1.4.54
# utils-flask-sqlalchemy
# utils-flask-sqlalchemy-geo
# wtforms-sqlalchemy
taxhub==2.1.0
taxhub==2.1.1
# via
# -r requirements-dependencies.in
# pypnnomenclature
Expand Down
Loading

0 comments on commit c750e03

Please sign in to comment.