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

Feat/site type categories and module categorie #18

Merged
merged 9 commits into from
Dec 22, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""create_cor_module_category

Revision ID: a54bafb13ce8
Revises:
Create Date: 2022-12-06 16:18:24.512562

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "a54bafb13ce8"
down_revision = "f24adb481f54"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"
referent_schema = "gn_commons"


def upgrade():
op.create_table(
"cor_module_categorie",
sa.Column(
"id_categorie",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_categorie_site.id_categorie",
name="fk_cor_module_categorie_id_categorie",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column("id_module", sa.Integer(),sa.ForeignKey(
f"{referent_schema}.t_modules.id_module",
name="fk_cor_module_categorie_id_module",
ondelete="CASCADE",
onupdate="CASCADE",
), nullable=False),
sa.PrimaryKeyConstraint("id_categorie", "id_module", name="pk_cor_module_categorie"),
schema=monitorings_schema,
)


def downgrade():
op.drop_table("cor_module_categorie", schema=monitorings_schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""create_cor_site_type_category
Revision ID: e64bafb13ce8
Revises:
Create Date: 2022-12-06 16:18:24.512562
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "e64bafb13ce8"
down_revision = "a54bafb13ce8"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"
referent_schema = "ref_nomenclatures"


def upgrade():
op.create_table(
"cor_site_type_categorie",
sa.Column(
"id_categorie",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_categorie_site.id_categorie",
name="fk_cor_site_type_categorie_id_categorie",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column("id_nomenclature", sa.Integer(),sa.ForeignKey(
f"{referent_schema}.t_nomenclatures.id_nomenclature",
name="fk_cor_site_type_categorie_id_type",
ondelete="CASCADE",
onupdate="CASCADE",
), nullable=False),
sa.PrimaryKeyConstraint("id_categorie", "id_nomenclature", name="pk_cor_site_type_categorie"),
schema=monitorings_schema,
)


def downgrade():
op.drop_table("cor_site_type_categorie", schema=monitorings_schema)
30 changes: 29 additions & 1 deletion backend/gn_module_monitoring/monitoring/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask_admin.contrib.sqla import ModelView
from geonature.core.admin.admin import CruvedProtectedMixin

from geonature.core.gn_commons.models import TNomenclatures
from geonature.utils.env import DB

from gn_module_monitoring.monitoring.models import BibCategorieSite

Expand All @@ -16,3 +17,30 @@ class BibCategorieSiteView(CruvedProtectedMixin, ModelView):
def __init__(self, session, **kwargs):
# Référence au model utilisé
super(BibCategorieSiteView, self).__init__(BibCategorieSite, session, **kwargs)

def get_only_type_site_asc():
return (
DB.session.query(TNomenclatures)
.filter(TNomenclatures.id_type == 116)
.order_by(TNomenclatures.label_fr.asc())
)

def get_label_fr_nomenclature(x):
return x.label_fr

def list_label_site_type_formatter(view, _context, model, _name):
return [item.label_fr for item in model.site_type]

# Nom de colonne user friendly
column_labels = dict(site_type="Type de site")
# Description des colonnes
column_descriptions = dict(site_type="Type de site à choisir en lien avec la catégorie")

column_hide_backrefs = False

form_args = dict(
site_type=dict(query_factory=get_only_type_site_asc, get_label=get_label_fr_nomenclature)
)

column_list = ("label", "config", "site_type")
column_formatters = dict(site_type=list_label_site_type_formatter)
44 changes: 42 additions & 2 deletions backend/gn_module_monitoring/monitoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from sqlalchemy.ext.hybrid import hybrid_property


from pypnnomenclature.models import TNomenclatures, BibNomenclaturesTypes
from geonature.core.gn_commons.models import TMedias
from geonature.core.gn_monitoring.models import TBaseSites, TBaseVisits
from geonature.core.gn_meta.models import TDatasets
Expand All @@ -20,6 +20,35 @@
from pypnusershub.db.models import User
from geonature.core.gn_monitoring.models import corVisitObserver

cor_module_categorie = DB.Table(
"cor_module_categorie",
DB.Column(
"id_module",
DB.Integer,
DB.ForeignKey("gn_commons.t_modules.id_module"),
primary_key=True,
),
DB.Column(
"id_categorie",
DB.Integer,
DB.ForeignKey("gn_monitoring.bib_categorie_site.id_categorie"),
primary_key=True,
), schema="gn_monitoring")

cor_site_type_categorie = DB.Table(
"cor_site_type_categorie",
DB.Column(
"id_nomenclature",
DB.Integer,
DB.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"),
primary_key=True,
),
DB.Column(
"id_categorie",
DB.Integer,
DB.ForeignKey("gn_monitoring.bib_categorie_site.id_categorie"),
primary_key=True,
), schema="gn_monitoring")

@serializable
class BibCategorieSite(DB.Model):
Expand All @@ -28,8 +57,13 @@ class BibCategorieSite(DB.Model):
id_categorie = DB.Column(DB.Integer, primary_key=True, nullable=False, unique=True)
label = DB.Column(DB.String, nullable=False)
config = DB.Column(JSONB)
site_type = DB.relationship(
"TNomenclatures",
secondary=cor_site_type_categorie,
lazy="joined",
)


@serializable
class TMonitoringObservationDetails(DB.Model):
__tablename__ = "t_observation_details"
Expand Down Expand Up @@ -305,6 +339,12 @@ class TMonitoringModules(TModules):
lazy="joined",
)

categories = DB.relationship(
"BibCategorieSite",
secondary=cor_module_categorie,
lazy="joined"
)


data = DB.Column(JSONB)

Expand Down
8 changes: 3 additions & 5 deletions backend/gn_module_monitoring/routes/site.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from typing import Tuple

from flask import request
from flask.json import jsonify
from geonature.core.gn_monitoring.models import TBaseSites
from werkzeug.datastructures import MultiDict

from gn_module_monitoring.blueprint import blueprint
from gn_module_monitoring.monitoring.models import BibCategorieSite
from gn_module_monitoring.utils.routes import (filter_params, get_limit_offset,
paginate)
from gn_module_monitoring.utils.routes import filter_params, get_limit_offset, paginate


@blueprint.route("/sites/categories", methods=["GET"])
Expand All @@ -18,7 +15,8 @@ def get_categories():

query = filter_params(query=BibCategorieSite.query, params=params)
query = query.order_by(BibCategorieSite.id_categorie)
return paginate(query=query, object_name="categories", limit=limit, page=page)

return paginate(query=query, object_name="categories", limit=limit, page=page, depth=1)


@blueprint.route("/sites/categories/<int:id_categorie>", methods=["GET"])
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions backend/gn_module_monitoring/tests/fixtures/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from geonature.core.gn_commons.models.base import TModules
from geonature.utils.env import db

from gn_module_monitoring.monitoring.models import TMonitoringModules
from gn_module_monitoring.tests.fixtures.site import categories


@pytest.fixture
def monitoring_module(module, categories):
id_module = TModules.query.filter(TModules.id_module == module.id_module).one().id_module
t_monitoring_module = TMonitoringModules()

module_data = {"id_module": id_module, "categories": list(categories.values())}
t_monitoring_module.from_dict(module_data)
# monitoring = TMonitoringModules(id_module=id_module, categories=list(categories.values()))
monitoring = t_monitoring_module
with db.session.begin_nested():
db.session.add(monitoring)

return monitoring
14 changes: 12 additions & 2 deletions backend/gn_module_monitoring/tests/fixtures/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@


@pytest.fixture()
def categories():
categories = [{"label": "gite", "config": {}}, {"label": "eolienne", "config": {}}]
def site_type():
return TNomenclatures.query.filter(
BibNomenclaturesTypes.mnemonique == "TYPE_SITE", TNomenclatures.mnemonique == "Grotte"
).one()


@pytest.fixture()
def categories(site_type):
categories = [
{"label": "gite", "config": {}, "site_type": [site_type]},
{"label": "eolienne", "config": {}, "site_type": [site_type]},
]

categories = {cat["label"]: BibCategorieSite(**cat) for cat in categories}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

from gn_module_monitoring.tests.fixtures.module import monitoring_module
from gn_module_monitoring.tests.fixtures.site import categories


@pytest.mark.usefixtures("temporary_transaction")
class TestModule:
def test_module(self, monitoring_module):
cateogories = monitoring_module.categories
assert False
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from flask import url_for

from gn_module_monitoring.tests.fixtures.site import categories, sites
from gn_module_monitoring.tests.fixtures.site import categories, site_type, sites


@pytest.mark.usefixtures("client_class", "temporary_transaction")
Expand All @@ -20,13 +20,13 @@ def test_get_categories(self, categories):
r = self.client.get(url_for("monitorings.get_categories"))

assert r.json["count"] >= len(categories)
assert all([cat.as_dict() in r.json["categories"] for cat in categories.values()])
assert all([cat.as_dict(depth=1) in r.json["categories"] for cat in categories.values()])

def test_get_categories_label(self, categories):
label = list(categories.keys())[0]

r = self.client.get(url_for("monitorings.get_categories"), query_string={"label": label})
assert categories[label].as_dict() in r.json["categories"]
assert categories[label].as_dict(depth=1) in r.json["categories"]

def test_get_sites(self, sites):
r = self.client.get(url_for("monitorings.get_sites"))
Expand Down
9 changes: 5 additions & 4 deletions backend/gn_module_monitoring/utils/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ def get_limit_offset(params: MultiDict) -> Tuple[int]:
return params.pop("limit", 50), params.pop("offset", 1)


def paginate(query: Query, object_name: str, limit: int, page: int) -> Response:
def paginate(query: Query, object_name: str, limit: int, page: int, depth: int = 0) -> Response:
result = query.paginate(page=page, error_out=False, max_per_page=limit)
data = {
object_name: [res.as_dict() for res in result.items],
object_name: [res.as_dict(depth=depth) for res in result.items],
"count": result.total,
"limit": limit,
"offset": page - 1,
}
return jsonify(data)

def filter_params(query: Query, params: MultiDict) -> Query:

def filter_params(query: Query, params: MultiDict) -> Query:
if len(params) != 0:
query = query.filter_by(**params)
return query
return query