Skip to content

Commit

Permalink
Import des données dans la synthèse - cf #14
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Sep 2, 2020
1 parent 8d6ff89 commit b5f1738
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 4 deletions.
6 changes: 5 additions & 1 deletion backend/routes/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,17 @@ def create_or_update_site_chiro(id_site=None):
db_sess.commit()
infos_site.base_site = base_site

# Création des médias
# Création des médiasn
if (data['medium']):
attach_uuid_to_medium(
data['medium'],
base_site.uuid_base_site
)

# Si modification sync synthese
if id_site:
base_repo.sync_synthese(base_site.uuid_base_site)

return _format_site_data(infos_site)
except InvalidBaseSiteData:
db_sess.rollback()
Expand Down
1 change: 0 additions & 1 deletion backend/routes/visite.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def create_or_update_visite_chiro(id_visite=None):

return _format_visite_data(visite)


@blueprint.route('/visite/<id_visite>', methods=['DELETE'])
@permissions.check_cruved_scope("R", False, module_code="SUIVI_CHIRO")
@json_resp
Expand Down
48 changes: 46 additions & 2 deletions backend/utils/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from geonature.core.gn_commons.repositories import (
TMediaRepository
)
from geonature.core.gn_synthese.utils.process import import_from_table
# from geonature.core.users.models import TRoles

from pypnusershub.db.models import User
Expand All @@ -29,6 +30,32 @@
from ..utils.relations import get_updated_relations


def process_synthese(schema_name, table_name, field_name, value):

try:
import_from_table(
schema_name,
table_name,
field_name,
value
)
except ValueError as e:
# warning
log.warning(
"""Error in module monitoring chiro, process_synthese.
Function import_from_table with parameters({}, {}, {}) raises the following error :
{}
"""
.format(
table_name,
field_name,
value,
e
)
)

return

class InvalidBaseSiteData(Exception):
pass

Expand All @@ -54,7 +81,7 @@ def handle_write(self, *, data=None, id_base_visite=None):
try:
id_dataset_inventaire = current_app.config["SUIVI_CHIRO"]["id_dataset_inventaire"]
id_dataset_suivi = current_app.config["SUIVI_CHIRO"]["id_dataset_suivi"]

id_module = current_app.config.get('SUIVI_CHIRO', {}).get('ID_MODULE')
if "observers" in data:
observers = self.session.query(User).\
filter(User.id_role.in_(data['observers'])).all()
Expand All @@ -81,7 +108,7 @@ def handle_write(self, *, data=None, id_base_visite=None):
model.id_dataset = id_dataset_suivi
else:
model.id_dataset = id_dataset_inventaire

model.id_module = id_module
self.session.flush() # génération de l'id de la visite
return model
except Exception as e: # vérifier type erreur
Expand Down Expand Up @@ -178,6 +205,14 @@ def handle_delete(self, base_site_id, cascade):
# faux si le site est juste déréférencé pour l'application
return False

def sync_synthese(self, uuid):
process_synthese(
'monitoring_chiro',
'v_chiro_gn_synthese',
'uuid_base_site',
uuid
)


class GNMonitoringContactTaxon():
"""
Expand Down Expand Up @@ -278,9 +313,18 @@ def handle_write(self):
self.data['medium'],
contact_taxon.uuid_chiro_visite_contact_taxon
)
for ctd in contact_taxon.denombrements:
self.sync_synthese(ctd.unique_id_sinp)

return contact_taxon

def sync_synthese(self, uuid):
process_synthese(
'monitoring_chiro',
'v_chiro_gn_synthese',
'unique_id_sinp',
uuid
)

def attach_uuid_to_medium(medium, uuid_attached_row):
'''
Expand Down
12 changes: 12 additions & 0 deletions data/schema_chiro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,19 @@ CREATE TRIGGER tri_log_changes AFTER INSERT OR DELETE OR UPDATE ON monitoring_ch

CREATE TRIGGER tri_log_changes AFTER INSERT OR DELETE OR UPDATE ON monitoring_chiro.cor_counting_contact FOR EACH ROW EXECUTE PROCEDURE gn_commons.fct_trg_log_changes();

CREATE OR REPLACE FUNCTION monitoring_chiro.fct_trg_delete_synthese_cor_counting_contact()
RETURNS trigger AS
$BODY$
BEGIN
--Suppression des données dans la synthèse
DELETE FROM gn_synthese.synthese WHERE unique_id_sinp = OLD.unique_id_sinp;
RETURN OLD;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

CREATE TRIGGER tri_delete_synthese_cor_counting_contact AFTER DELETE ON monitoring_chiro.cor_counting_contact FOR EACH ROW EXECUTE PROCEDURE monitoring_chiro.fct_trg_delete_synthese_cor_counting_contact();

CREATE OR REPLACE FUNCTION monitoring_chiro.fct_trg_get_nom_complet()
RETURNS trigger AS
Expand Down
68 changes: 68 additions & 0 deletions data/view_synthese.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

DROP VIEW monitoring_chiro.v_chiro_gn_synthese ;
CREATE OR REPLACE VIEW monitoring_chiro.v_chiro_gn_synthese AS
WITH source AS (
SELECT id_source FROM gn_synthese.t_sources WHERE name_source = 'SUIVI_CHIRO'
)
SELECT
ccc.unique_id_sinp as unique_id_sinp,
v.uuid_base_visit as unique_id_sinp_grp,
(SELECT id_source FROM source) as id_source,
ccc.id_counting_contact as entity_source_pk_value,
v.id_dataset as id_dataset,
v.id_module as id_module,
-- MISSING FIELD id_nomenclature_geo_object_nature,
v.id_nomenclature_grp_typ as id_nomenclature_grp_typ,
-- MISSING FIELD id_nomenclature_obs_meth,
-- NOT IN SYNTHESE v.id_nomenclature_tech_collect_campanule as id_nomenclature_tech_collect_campanule,
vct.id_nomenclature_etat_bio as id_nomenclature_bio_condition,
-- MISSING FIELD id_nomenclature_bio_status,
-- MISSING FIELD id_nomenclature_naturalness,
-- MISSING FIELD id_nomenclature_exist_proof,
-- MISSING FIELD id_nomenclature_diffusion_level,
ccc.id_nomenclature_life_stage,
ccc.id_nomenclature_sex,
ccc.id_nomenclature_obj_count,
ccc.id_nomenclature_type_count,
-- MISSING FIELD id_nomenclature_observation_status,
-- MISSING FIELD id_nomenclature_blurring,
-- MISSING FIELD id_nomenclature_source_status,
-- MISSING FIELD id_nomenclature_info_geo_type,
ccc.count_min,
ccc.count_max,
vct.cd_nom as cd_nom,
COALESCE(tx_presume, vct.nom_complet, t.nom_complet) as nom_cite,
s.altitude_min,
s.altitude_max,
-- CAS particulier des inventaires
COALESCE(s.geom,vc.geom) as the_geom_4326,
st_centroid(COALESCE(s.geom,vc.geom)) as the_geom_point,
st_transform(COALESCE(geom_local,vc.geom), 2154) as the_geom_local,
visit_date_min as date_min,
visit_date_max as date_max,
obs.observers as observers,
obs.ids_observers as ids_observers,
-- MISSING FIELD determiner,
vct.id_digitiser as id_digitiser,
-- MISSING FIELD id_nomenclature_determination_method,
v.comments as comment_context,
vct.commentaire as comment_description
FROM monitoring_chiro.cor_counting_contact ccc
JOIN monitoring_chiro.t_visite_contact_taxons vct
ON ccc.id_contact_taxon = vct.id_contact_taxon
JOIN taxonomie.taxref t
ON t.cd_nom = vct.cd_nom
JOIN gn_monitoring.t_base_visits v
ON vct.id_base_visit = v.id_base_visit
LEFT OUTER JOIN monitoring_chiro.t_visite_conditions vc
ON vc.id_base_visit = v.id_base_visit
LEFT OUTER JOIN gn_monitoring.t_base_sites s
ON s.id_base_site = v.id_base_site
LEFT OUTER JOIN (
SELECT id_base_visit, array_agg(r.id_role) as ids_observers, string_agg(CONCAT(r.nom_role, ' ', r.prenom_role), ', ') as observers
FROM gn_monitoring.cor_visit_observer vr
JOIN utilisateurs.t_roles r
ON vr.id_role = r.id_role
GROUP BY id_base_visit
) obs
ON obs.id_base_visit = v.id_base_visit;

0 comments on commit b5f1738

Please sign in to comment.