Skip to content

Commit

Permalink
feat(wip): get context import by query params url
Browse files Browse the repository at this point in the history
- Format params to FieldsMapping interface
- Send on upload route frontend
- Set fieldsmapping for t_import on upload route backend

Reviewed-by: andriacap
  • Loading branch information
andriacap committed Dec 6, 2024
1 parent 2d0aa04 commit 1d6a67b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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';
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-row-count';

@Component({
selector: 'upload-file-step',
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -91,9 +104,9 @@ export class UploadFileStepComponent implements OnInit {
}
onSaveData(): Observable<Import> {
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() {
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/app/modules/imports/services/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ export class ImportDataService {
return this._http.get<Import>(`${this.getUrlApiForADestination()}/imports/${id_import}/`);
}

addFile(datasetId: number, file: File): Observable<Import> {
addFile(datasetId: number, file: File, fieldsToMap: FieldMappingValues): Observable<Import> {
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<Import>(url, fd);
}

updateFile(importId: number, file: File): Observable<Import> {
updateFile(importId: number, file: File, fieldsToMap: FieldMappingValues): Observable<Import> {
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<Import>(url, fd);
}
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/app/modules/imports/utils/format-row-count.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { Import } from '../models/import.model';
import { FieldMappingValues } from '../models/mapping.model';

export function formatRowCount(imprt: Import): string {
return imprt && imprt.source_count
? `${imprt.statistics['nb_line_valid'] ?? 0} / ${imprt['source_count']}`
: '';
}

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;
}

0 comments on commit 1d6a67b

Please sign in to comment.