Skip to content

Commit

Permalink
Filtrer sites/visites dans contexte d'un module (#377)
Browse files Browse the repository at this point in the history
* Filtrer visites dans contexte d'un module

* Cas hors contexte module

* Sites des groupes de sites dans le contexte d'un module

* Filtre site sur visite

* Date dernière visite
  • Loading branch information
amandine-sahl authored Oct 29, 2024
1 parent 989a821 commit 7066e77
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 20 deletions.
109 changes: 91 additions & 18 deletions backend/gn_module_monitoring/monitoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
from uuid import uuid4

from sqlalchemy import join, select, func, and_
from sqlalchemy.orm import (
column_property,
aliased,
)
from sqlalchemy.orm import column_property, aliased

from sqlalchemy.dialects.postgresql import JSONB, UUID

from utils_flask_sqla.serializers import serializable
Expand Down Expand Up @@ -229,6 +227,18 @@ def organism_actors(self):
return actors_organism_list

def has_instance_permission(self, scope):
# Filtre sur le contexte du module
# Si dans un sous module, on ne peut voir que les
# visites de ce module

if getattr(g, "current_module", None):
if (
not g.current_module.module_code == "MONITORINGS"
and not self.id_module == g.current_module.id_module
):
return False

# Filtre sur les permissions
if scope == 0:
return False
elif scope in (1, 2):
Expand Down Expand Up @@ -288,15 +298,53 @@ class TMonitoringSites(TBaseSites, PermissionModel, SitesQuery):
.scalar_subquery()
)

nb_visits = column_property(
select(func.count(TBaseVisits.id_base_site))
.where(TBaseVisits.id_base_site == id_base_site)
.scalar_subquery()
)

geom_geojson = column_property(func.ST_AsGeoJSON(TBaseSites.geom), deferred=True)
types_site = DB.relationship("BibTypeSite", secondary=cor_site_type, overlaps="sites")

@hybrid_property
def last_visit(self):
query = select(func.max(TBaseVisits.visit_date_min)).where(
TBaseVisits.id_base_site == self.id_base_site
)
# Filtre sur le contexte du module
# Si dans un sous module, on ne dénombre les visites de ce module
if getattr(g, "current_module", None):
if not g.current_module.module_code == "MONITORINGS":
query = query.where(TMonitoringVisits.id_module == g.current_module.id_module)
return DB.session.scalar(query)

@last_visit.expression
def last_visit(cls):
query = select(func.max(TBaseVisits.visit_date_min)).where(
TBaseVisits.id_base_site == cls.id_base_site
)
if getattr(g, "current_module", None):
if not g.current_module.module_code == "MONITORINGS":
query = query.where(TMonitoringVisits.id_module == g.current_module.id_module)
return query.as_scalar()

@hybrid_property
def nb_visits(self):
query = select(func.count(TBaseVisits.id_base_site)).where(
TBaseVisits.id_base_site == self.id_base_site
)
# Filtre sur le contexte du module
# Si dans un sous module, on ne dénombre les visites de ce module
if getattr(g, "current_module", None):
if not g.current_module.module_code == "MONITORINGS":
query = query.where(TMonitoringVisits.id_module == g.current_module.id_module)
return DB.session.scalar(query)

@nb_visits.expression
def nb_visits(cls):
query = select(func.count(TBaseVisits.id_base_site)).where(
TBaseVisits.id_base_site == cls.id_base_site
)
if getattr(g, "current_module", None):
if not g.current_module.module_code == "MONITORINGS":
query = query.where(TMonitoringVisits.id_module == g.current_module.id_module)
return query.as_scalar()

@hybrid_property
def organism_actors(self):
actors_organism_list = []
Expand All @@ -310,6 +358,15 @@ def organism_actors(self):
return actors_organism_list

def has_instance_permission(self, scope):
# Filtre sur le contexte du module
# Si dans un sous module, on ne peut voir que les
# site du type de ce module
if getattr(g, "current_module", None):
if not g.current_module.module_code == "MONITORINGS" and (
not (set(g.current_module.types_site) & set(self.types_site))
):
return False

if scope == 0:
return False
elif scope in (1, 2):
Expand Down Expand Up @@ -375,14 +432,6 @@ class TMonitoringSitesGroups(DB.Model, PermissionModel, SitesGroupsQuery):

altitude_min = DB.Column(DB.Integer)
altitude_max = DB.Column(DB.Integer)
nb_visits = column_property(
select(func.count(TMonitoringVisits.id_base_site))
.where(
TMonitoringVisits.id_base_site == TMonitoringSites.id_base_site,
TMonitoringSites.id_sites_group == id_sites_group,
)
.scalar_subquery()
)

geom_geojson = column_property(
select(func.st_asgeojson(func.st_convexHull(func.st_collect(TMonitoringSites.geom))))
Expand All @@ -392,6 +441,30 @@ class TMonitoringSitesGroups(DB.Model, PermissionModel, SitesGroupsQuery):
.scalar_subquery()
)

@hybrid_property
def nb_visits(self):
query = select(func.count(TMonitoringVisits.id_base_site)).where(
TMonitoringVisits.id_base_site == TMonitoringSites.id_base_site,
TMonitoringSites.id_sites_group == self.id_sites_group,
)
# Filtre sur le contexte du module
# Si dans un sous module, on ne dénombre que les visites de ce module
if getattr(g, "current_module", None):
if not g.current_module.module_code == "MONITORINGS":
query = query.where(TMonitoringVisits.id_module == g.current_module.id_module)
return DB.session.scalar(query)

@nb_visits.expression
def nb_visits(cls):
query = select(func.count(TMonitoringVisits.id_base_site)).where(
TMonitoringVisits.id_base_site == TMonitoringSites.id_base_site,
TMonitoringSites.id_sites_group == cls.id_sites_group,
)
if getattr(g, "current_module", None):
if not g.current_module.module_code == "MONITORINGS":
query = query.where(TMonitoringVisits.id_module == g.current_module.id_module)
return query.as_scalar()

@hybrid_property
def organism_actors(self):
# return self.digitiser.id_organisme
Expand Down
3 changes: 3 additions & 0 deletions backend/gn_module_monitoring/monitoring/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Meta:
id_digitiser = fields.Method("get_id_digitiser")
is_geom_from_child = fields.Method("set_is_geom_from_child", dump_only=True)
modules = MA.Pluck(ModuleSchema, "id_module", many=True)
nb_visits = fields.Integer(dump_only=True)

def get_id_digitiser(self, obj):
return obj.id_digitiser
Expand Down Expand Up @@ -142,6 +143,8 @@ class Meta:
id_inventor = fields.Method("get_id_inventor")
inventor = fields.Method("get_inventor_name")
medias = MA.Nested(MediaSchema, many=True)
nb_visits = fields.Integer(dump_only=True)
last_visit = fields.DateTime(dump_only=True)

def serialize_geojson(self, obj):
if obj.geom is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class MonitoringObjectComponent implements OnInit {
pre_filters['site']['id_base_site'] = this.obj.id;
} else if (this.obj['siteId'] !== undefined) {
// affichage du site parent
pre_filters['id_base_site'] = this.obj['siteId'];
pre_filters['site']['id_base_site'] = this.obj['siteId'];
} else if (queryParams['id_base_site'] !== undefined) {
// récupération du site parent via l'url
pre_filters['site']['id_base_site'] = queryParams['id_base_site'];
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/services/geojson.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export class GeoJSONService {
getSitesGroupsGeometriesWithSites(
sitesGroupOnEachFeature: Function,
sitesOnEachFeature: Function,
paramsSite = {},
paramsSitesGroup = {},
paramsSite = {},
sitesGroupstyle?,
sitesStyle?
) {
Expand Down

0 comments on commit 7066e77

Please sign in to comment.