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

[IMPORT] Feat context parameter import #3277

Draft
wants to merge 7 commits into
base: feat/import-monitorings
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions backend/geonature/core/imports/routes/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ def upload_file(scope, imprt, destination=None): # destination is set when impr
assert destination
author = g.current_user
f = request.files["file"]
field_to_map_str = request.form.get("fieldsToMap")
if field_to_map_str:
fields_to_map = json.loads(field_to_map_str)
# NOTES: Pas possible d'utiliser le validate value ici
# try:
# FieldMapping.validate_values(fields_to_map)
# except ValueError as e:
# raise BadRequest(*e.args)
else:
fields_to_map = {}

size = get_file_size(f)
# value in config file is in Mo
max_file_size = current_app.config["IMPORT"]["MAX_FILE_SIZE"] * 1024 * 1024
Expand All @@ -203,6 +214,8 @@ def upload_file(scope, imprt, destination=None): # destination is set when impr
if not dataset.active:
raise Forbidden("Le jeu de données est fermé.")
imprt = TImports(destination=destination, dataset=dataset)
if fields_to_map:
imprt.fieldmapping = fields_to_map
imprt.authors.append(author)
db.session.add(imprt)
else:
Expand Down
68 changes: 62 additions & 6 deletions backend/geonature/migrations/data/imports/sample_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ FROM (
FROM gn_permissions.t_objects
WHERE code_object IN ('IMPORT', 'MAPPING')
) t_o;

-- Insert permissions for agent-test-import
INSERT INTO gn_permissions.t_permissions (
id_role,
Expand Down Expand Up @@ -197,7 +196,6 @@ FROM (
FROM gn_permissions.t_objects
WHERE code_object IN ('ALL')
) t_o;

-- Insert permissions for agent-test-import
INSERT INTO gn_permissions.t_permissions (
id_role,
Expand Down Expand Up @@ -231,7 +229,6 @@ FROM (
FROM gn_permissions.t_objects
WHERE code_object IN ('ALL')
) t_o;

-- Ajout du module occhab si non présent
INSERT INTO gn_commons.t_modules (
module_code,
Expand Down Expand Up @@ -934,7 +931,61 @@ VALUES (
true,
'2022-01-01 00:00:00',
'2022-12-31 23:59:59',
'{"entity_source_pk_value": "id_synthese", "unique_id_sinp": "uuid_perm_sinp", "meta_create_date": "date_creation", "meta_update_date": "date_modification", "id_nomenclature_grp_typ": "type_regroupement", "unique_id_sinp_grp": "uuid_perm_grp_sinp", "date_min": "date_debut", "date_max": "date_fin", "hour_min": "heure_debut", "hour_max": "heure_fin", "altitude_min": "alti_min", "altitude_max": "alti_max", "depth_min": "prof_min", "depth_max": "prof_max", "observers": "observateurs", "comment_context": "comment_releve", "WKT": "geometrie_wkt_4326", "id_nomenclature_geo_object_nature": "nature_objet_geo", "place_name": "nom_lieu", "precision": "precision_geographique", "cd_hab": "cd_habref", "grp_method": "methode_regroupement", "nom_cite": "nom_cite", "cd_nom": "cd_nom", "id_nomenclature_obs_technique": "technique_observation", "id_nomenclature_bio_status": "biologique_statut", "id_nomenclature_bio_condition": "etat_biologique", "id_nomenclature_biogeo_status": "biogeographique_statut", "id_nomenclature_naturalness": "naturalite", "comment_description": "comment_occurrence", "id_nomenclature_sensitivity": "niveau_sensibilite", "id_nomenclature_diffusion_level": "niveau_precision_diffusion", "id_nomenclature_observation_status": "statut_observation", "id_nomenclature_blurring": "floutage_dee", "id_nomenclature_source_status": "statut_source", "reference_biblio": "reference_biblio", "id_nomenclature_behaviour": "comportement", "id_nomenclature_life_stage": "stade_vie", "id_nomenclature_sex": "sexe", "id_nomenclature_type_count": "type_denombrement", "id_nomenclature_obj_count": "objet_denombrement", "count_min": "nombre_min", "count_max": "nombre_max", "id_nomenclature_determination_method": "methode_determination", "determiner": "determinateur", "id_nomenclature_exist_proof": "preuve_existante", "digital_proof": "preuve_numerique_url", "non_digital_proof": "preuve_non_numerique", "id_nomenclature_valid_status": "niveau_validation", "validator": "validateur", "meta_validation_date": "date_validation", "validation_comment": "comment_validation"}',
'{
"entity_source_pk_value": {"column_src": "id_synthese"},
"unique_id_sinp": {"column_src": "uuid_perm_sinp"},
"meta_create_date": {"column_src": "date_creation"},
"meta_update_date": {"column_src": "date_modification"},
"id_nomenclature_grp_typ": {"column_src": "type_regroupement"},
"unique_id_sinp_grp": {"column_src": "uuid_perm_grp_sinp"},
"date_min": {"column_src": "date_debut"},
"date_max": {"column_src": "date_fin"},
"hour_min": {"column_src": "heure_debut"},
"hour_max": {"column_src": "heure_fin"},
"altitude_min": {"column_src": "alti_min"},
"altitude_max": {"column_src": "alti_max"},
"depth_min": {"column_src": "prof_min"},
"depth_max": {"column_src": "prof_max"},
"observers": {"column_src": "observateurs"},
"comment_context": {"column_src": "comment_releve"},
"WKT": {"column_src": "geometrie_wkt_4326"},
"id_nomenclature_geo_object_nature": {"column_src": "nature_objet_geo"},
"place_name": {"column_src": "nom_lieu"},
"precision": {"column_src": "precision_geographique"},
"cd_hab": {"column_src": "cd_habref"},
"grp_method": {"column_src": "methode_regroupement"},
"nom_cite": {"column_src": "nom_cite"},
"cd_nom": {"column_src": "cd_nom"},
"id_nomenclature_obs_technique": {"column_src": "technique_observation"},
"id_nomenclature_bio_status": {"column_src": "biologique_statut"},
"id_nomenclature_bio_condition": {"column_src": "etat_biologique"},
"id_nomenclature_biogeo_status": {"column_src": "biogeographique_statut"},
"id_nomenclature_naturalness": {"column_src": "naturalite"},
"comment_description": {"column_src": "comment_occurrence"},
"id_nomenclature_sensitivity": {"column_src": "niveau_sensibilite"},
"id_nomenclature_diffusion_level": {"column_src": "niveau_precision_diffusion"},
"id_nomenclature_observation_status": {"column_src": "statut_observation"},
"id_nomenclature_blurring": {"column_src": "floutage_dee"},
"id_nomenclature_source_status": {"column_src": "statut_source"},
"reference_biblio": {"column_src": "reference_biblio"},
"id_nomenclature_behaviour": {"column_src": "comportement"},
"id_nomenclature_life_stage": {"column_src": "stade_vie"},
"id_nomenclature_sex": {"column_src": "sexe"},
"id_nomenclature_type_count": {"column_src": "type_denombrement"},
"id_nomenclature_obj_count": {"column_src": "objet_denombrement"},
"count_min": {"column_src": "nombre_min"},
"count_max": {"column_src": "nombre_max"},
"id_nomenclature_determination_method": {"column_src": "methode_determination"},
"determiner": {"column_src": "determinateur"},
"id_nomenclature_exist_proof": {"column_src": "preuve_existante"},
"digital_proof": {"column_src": "preuve_numerique_url"},
"non_digital_proof": {"column_src": "preuve_non_numerique"},
"id_nomenclature_valid_status": {"column_src": "niveau_validation"},
"validator": {"column_src": "validateur"},
"meta_validation_date": {"column_src": "date_validation"},
"validation_comment": {"column_src": "comment_validation"}
}
',
'{"OCC_COMPORTEMENT": {"Non renseign\u00e9": "1"}, "ETA_BIO": {"Non renseign\u00e9": "1"}, "STATUT_BIO": {"Non renseign\u00e9": "1"}, "STAT_BIOGEO": {"Non renseign\u00e9": "1"}, "DEE_FLOU": {"Non": "NON"}, "METH_DETERMIN": {"Autre m\u00e9thode de d\u00e9termination": "2"}, "NIV_PRECIS": {"Pr\u00e9cise": "5"}, "PREUVE_EXIST": {"Oui": "1"}, "NAT_OBJ_GEO": {"Inventoriel": "In"}, "TYP_GRP": {"OBS": "OBS"}, "STADE_VIE": {"Adulte": "2", "Immature": "4", "Juv\u00e9nile": "3"}, "NATURALITE": {"Sauvage": "1"}, "OBJ_DENBR": {"Individu": "IND"}, "METH_OBS": {"Galerie/terrier": "23"}, "STATUT_OBS": {"Pr\u00e9sent": "Pr"}, "SENSIBILITE": {"Non sensible - Diffusion pr\u00e9cise": "0"}, "SEXE": {"Femelle": "2"}, "STATUT_SOURCE": {"Terrain": "Te"}, "TYP_DENBR": {"Compt\u00e9": "Co"}, "STATUT_VALID": {"En attente de validation": "0"}}',
';',
NULL,
Expand Down Expand Up @@ -962,7 +1013,12 @@ VALUES (
true,
'2022-01-01 00:00:00',
'2022-12-31 23:59:59',
'{"date_min": "date_min", "cd_hab": "cd_hab", "nom_cite": "nom_cite"}',
'{
"date_min": {"column_src": "date_min"},
"cd_hab": {"column_src": "cd_hab"},
"nom_cite": {"column_src": "nom_cite"}
}
',
'{}',
';',
NULL,
Expand Down Expand Up @@ -1037,4 +1093,4 @@ SELECT cda.id_role,
FROM temp_filtered_imports tfi
JOIN gn_meta.cor_dataset_actor cda ON tfi.id_dataset = cda.id_dataset
WHERE cda.id_role IS NOT NULL;
DROP TABLE temp_filtered_imports;
DROP TABLE temp_filtered_imports;
21 changes: 21 additions & 0 deletions backend/geonature/tests/imports/test_imports_synthese.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from operator import or_
from functools import reduce
import csv
import json

from geonature.core.imports.checks.errors import ImportCodeError
import pytest
Expand Down Expand Up @@ -583,6 +584,26 @@ def test_import_upload(self, users, datasets):
assert imprt.source_file is not None
assert imprt.full_file_name == "simple_file.csv"

with open(tests_path / "files" / "synthese" / "simple_file.csv", "rb") as f:
fields_to_map = {
"nom_cite": {"column_src": "nom_cite", "default_value": "test_nomcite"},
"altitude_max": {"column_src": "altitude_max", "default_value": 10},
}
data = {
"file": (f, "simple_file.csv"),
"datasetId": datasets["own_dataset"].id_dataset,
"fieldsToMap": json.dumps(fields_to_map),
}
r = self.client.post(
url_for("import.upload_file"),
data=data,
headers=Headers({"Content-Type": "multipart/form-data"}),
)
assert r.status_code == 200, r.data

imprt = db.session.get(TImports, r.json["id_import"])
assert imprt.fieldmapping == fields_to_map

def test_import_error(self, users, datasets):
set_logged_user(self.client, users["user"])
with open(tests_path / "files" / "synthese" / "empty.csv", "rb") as f:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { USERS } from './constants/users';
import { TIMEOUT_WAIT, VIEWPORTS } from './constants/common';
import { FILES } from './constants/files';

const USER = USERS[0];
const VIEWPORT = VIEWPORTS[0];

function handleFieldValidation(dataQa, paramsName, expectedValue, fieldType) {
if (['textarea', 'text', 'number'].includes(fieldType)) {
cy.get(dataQa)
.find(`[data-qa^="field-${fieldType}-${paramsName}_default_value_"]`)
.should('have.value', expectedValue);
} else if (fieldType === 'date') {
cy.get(dataQa).find('[data-qa="input-date"]').should('have.value', expectedValue);
} else if (fieldType === 'nomenclature') {
cy.get(`${dataQa} .ng-value-container .ng-value-label`).should('have.text', expectedValue);
}
}

const paramsByDestination = [
{
destination: 'synthese',
queryParams: [
{
paramsName: 'nom_cite',
paramsValue: 'test_nomcite',
fieldType: 'textarea',
expectedValue: 'test_nomcite',
},
{
paramsName: 'altitude_max',
paramsValue: 10,
fieldType: 'number',
expectedValue: 10,
},
{
paramsName: 'date_min',
paramsValue: '2024-12-12',
fieldType: 'date',
expectedValue: '12/12/2024',
},
{
paramsName: 'id_nomenclature_geo_object_nature',
paramsValue: 'Inventoriel',
fieldType: 'nomenclature',
expectedValue: 'Inventoriel',
},
],
},
{
destination: 'occhab',
queryParams: [
{
paramsName: 'id_nomenclature_determination_type',
paramsValue: 'Inconnu',
fieldType: 'nomenclature',
entityLabel: 'Habitat',
expectedValue: 'Inconnu',
},
// TODO: some fields seems to be not handled to be directly added as default value
// {
// paramsName: 'nom_cite',
// paramsValue: 'test_nomcite',
// fieldType: 'taxonomy-input',
// entityLabel: 'Habitat',
// expectedValue: 'test_nomcite',
// },
{
paramsName: 'station_name',
paramsValue: 'test_station_name',
fieldType: 'textarea',
entityLabel: 'Station',
expectedValue: 'test_station_name',
},
],
},
];

describe('Import - Upload step', () => {
context(`viewport: ${VIEWPORT.width}x${VIEWPORT.height}`, () => {
beforeEach(() => {
cy.viewport(VIEWPORT.width, VIEWPORT.height);
cy.geonatureLogin(USER.login.username, USER.login.password);
cy.wait(TIMEOUT_WAIT);
});

paramsByDestination.forEach(({ destination, queryParams }) => {
describe(`Destination: ${destination}`, () => {
beforeEach(() => {
const urlParams = queryParams
.map((param) => `${param.paramsName}=${param.paramsValue}`)
.join('&');
cy.visit(`/#/import/${destination}/process/upload?${urlParams}`);
cy.pickDataset(USER.dataset);
cy.loadImportFile(FILES[destination].valid.fixture);
cy.configureImportFile();
});

it(`Validates fields for destination: ${destination}`, () => {
queryParams.forEach(({ paramsName, expectedValue, fieldType, entityLabel }) => {
let dataQa = `[data-qa="import-fieldmapping-theme-${paramsName}"]`;

if (destination === 'occhab' && entityLabel) {
const dataQaEntity = `[data-qa="import-entity-tab-${entityLabel}"]`;
cy.get(dataQaEntity, { timeout: 30000 }).should('be.visible').click();
}

handleFieldValidation(dataQa, paramsName, expectedValue, fieldType);
});
cy.visitImport();
cy.removeFirstImportInList();
});
});
});
});
});
Loading
Loading