diff --git a/backend/geonature/core/imports/routes/imports.py b/backend/geonature/core/imports/routes/imports.py index 92b1dbb819..af3e6d1e63 100644 --- a/backend/geonature/core/imports/routes/imports.py +++ b/backend/geonature/core/imports/routes/imports.py @@ -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 @@ -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: diff --git a/frontend/src/app/modules/imports/components/import_process/upload-file-step/upload-file-step.component.ts b/frontend/src/app/modules/imports/components/import_process/upload-file-step/upload-file-step.component.ts index d34403894e..8210e3aa61 100644 --- a/frontend/src/app/modules/imports/components/import_process/upload-file-step/upload-file-step.component.ts +++ b/frontend/src/app/modules/imports/components/import_process/upload-file-step/upload-file-step.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; -import { Observable } from 'rxjs'; +import { combineLatest, Observable } from 'rxjs'; import { ImportDataService } from '../../../services/data.service'; import { CommonService } from '@geonature_common/service/common.service'; import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms'; @@ -8,6 +8,9 @@ import { Step } from '../../../models/enums.model'; import { Destination, Import } from '../../../models/import.model'; import { ImportProcessService } from '../import-process.service'; import { ConfigService } from '@geonature/services/config.service'; +import { switchMap } from 'rxjs/operators'; +import { FieldMappingValues } from '@geonature/modules/imports/models/mapping.model'; +import { formatQueryParams } from '@geonature/modules/imports/utils/format-to-fieldsmapping'; @Component({ selector: 'upload-file-step', @@ -27,6 +30,7 @@ export class UploadFileStepComponent implements OnInit { public maxFileNameLength: number = 255; public acceptedExtensions: string = null; public destination: Destination = null; + public paramsFieldMapping: FieldMappingValues; constructor( private ds: ImportDataService, @@ -57,11 +61,20 @@ export class UploadFileStepComponent implements OnInit { } setupDatasetSelect() { - this.route.parent.params.subscribe((params) => { - this.ds.getDestination(params['destination']).subscribe((dest) => { - this.destination = dest; + combineLatest([ + this.route.parent.queryParams, + this.route.parent?.params || [], + ]) + .pipe( + switchMap(([queryParams, parentParams]) => { + this.paramsFieldMapping = formatQueryParams(queryParams); + const destinationLabel = parentParams['destination']; + return this.ds.getDestination(destinationLabel); + }) + ) + .subscribe((destination) => { + this.destination = destination; }); - }); } isNextStepAvailable() { @@ -91,9 +104,9 @@ export class UploadFileStepComponent implements OnInit { } onSaveData(): Observable { if (this.importData) { - return this.ds.updateFile(this.importData.id_import, this.file); + return this.ds.updateFile(this.importData.id_import, this.file, this.paramsFieldMapping); } else { - return this.ds.addFile(this.uploadForm.get('dataset').value, this.file); + return this.ds.addFile(this.uploadForm.get('dataset').value, this.file, this.paramsFieldMapping); } } onNextStep() { diff --git a/frontend/src/app/modules/imports/services/data.service.ts b/frontend/src/app/modules/imports/services/data.service.ts index 8d799f2c7f..27044d6ee1 100644 --- a/frontend/src/app/modules/imports/services/data.service.ts +++ b/frontend/src/app/modules/imports/services/data.service.ts @@ -55,17 +55,19 @@ export class ImportDataService { return this._http.get(`${this.getUrlApiForADestination()}/imports/${id_import}/`); } - addFile(datasetId: number, file: File): Observable { + addFile(datasetId: number, file: File, fieldsToMap: FieldMappingValues): Observable { let fd = new FormData(); fd.append('file', file, file.name); fd.append('datasetId', String(datasetId)); + fd.append('fieldsToMap', JSON.stringify(fieldsToMap)); const url = `${this.getUrlApiForADestination()}/imports/upload`; return this._http.post(url, fd); } - updateFile(importId: number, file: File): Observable { + updateFile(importId: number, file: File, fieldsToMap: FieldMappingValues): Observable { let fd = new FormData(); fd.append('file', file, file.name); + fd.append('fieldsToMap', JSON.stringify(fieldsToMap)); const url = `${this.getUrlApiForADestination()}/imports/${importId}/upload`; return this._http.put(url, fd); } diff --git a/frontend/src/app/modules/imports/utils/format-to-fieldsmapping.ts b/frontend/src/app/modules/imports/utils/format-to-fieldsmapping.ts new file mode 100644 index 0000000000..9941305406 --- /dev/null +++ b/frontend/src/app/modules/imports/utils/format-to-fieldsmapping.ts @@ -0,0 +1,19 @@ +import { FieldMappingValues } from "../models/mapping.model"; + +export function formatQueryParams(queryParams: { [key: string]: string }): FieldMappingValues { + const formattedParams: FieldMappingValues = {}; + + Object.keys(queryParams).forEach((key) => { + const value = queryParams[key]; + + const parsedValue = !isNaN(Number(value)) ? Number(value) : value; + + formattedParams[key] = { + column_src: key, + default_value: parsedValue, + }; + }); + + return formattedParams; + } + \ No newline at end of file