From b5f1738a54c808b503abe00d679d43efe0aab421 Mon Sep 17 00:00:00 2001 From: "amandine.sahl" Date: Wed, 2 Sep 2020 12:44:02 +0200 Subject: [PATCH] =?UTF-8?q?Import=20des=20donn=C3=A9es=20dans=20la=20synth?= =?UTF-8?q?=C3=A8se=20-=20cf=20#14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/routes/site.py | 6 +++- backend/routes/visite.py | 1 - backend/utils/repos.py | 48 ++++++++++++++++++++++++++-- data/schema_chiro.sql | 12 +++++++ data/view_synthese.sql | 68 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 data/view_synthese.sql diff --git a/backend/routes/site.py b/backend/routes/site.py index 96a2750..1facdfb 100644 --- a/backend/routes/site.py +++ b/backend/routes/site.py @@ -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() diff --git a/backend/routes/visite.py b/backend/routes/visite.py index 676faf6..31a4784 100644 --- a/backend/routes/visite.py +++ b/backend/routes/visite.py @@ -115,7 +115,6 @@ def create_or_update_visite_chiro(id_visite=None): return _format_visite_data(visite) - @blueprint.route('/visite/', methods=['DELETE']) @permissions.check_cruved_scope("R", False, module_code="SUIVI_CHIRO") @json_resp diff --git a/backend/utils/repos.py b/backend/utils/repos.py index 2e7eb69..8be702e 100644 --- a/backend/utils/repos.py +++ b/backend/utils/repos.py @@ -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 @@ -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 @@ -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() @@ -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 @@ -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(): """ @@ -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): ''' diff --git a/data/schema_chiro.sql b/data/schema_chiro.sql index a728fea..4ff9b91 100644 --- a/data/schema_chiro.sql +++ b/data/schema_chiro.sql @@ -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 diff --git a/data/view_synthese.sql b/data/view_synthese.sql new file mode 100644 index 0000000..d766e34 --- /dev/null +++ b/data/view_synthese.sql @@ -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;