Skip to content

Commit

Permalink
feat(import): can post fieldmapping in new structured format
Browse files Browse the repository at this point in the history
  • Loading branch information
20cents committed Nov 21, 2024
1 parent b91808b commit 68e327d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
35 changes: 31 additions & 4 deletions backend/geonature/core/imports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def optional_conditions_to_jsonschema(name_field: str, optional_conditions: Iter
"if": {
"not": {
"properties": {
field_opt: {"type": "string"} for field_opt in optional_conditions
field_opt: {"type": "object"} for field_opt in optional_conditions
}
}
},
Expand Down Expand Up @@ -649,9 +649,36 @@ def validate_values(values):
"type": "object",
"properties": {
field.name_field: {
"type": (
"boolean" if field.autogenerated else ("array" if field.multi else "string")
),
"type": "object",
"oneOf": [
{
"properties": {
"column_src": {
"type": (
"boolean"
if field.autogenerated
else ("array" if field.multi else "string")
),
}
},
"required": ["column_src"],
"additionalProperties": False,
},
{
"properties": {
"value": {
"oneOf": [
{"type": "boolean"},
{"type": "number"},
{"type": "string"},
{"type": "array"},
]
}
},
"required": ["value"],
"additionalProperties": False,
},
],
}
for field in fields
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ export class FieldsMappingStepComponent implements OnInit {
let values: FieldMappingValues = {};
for (let [key, value] of Object.entries(this._fieldMappingService.mappingFormGroup.value)) {
if (value != null) {
values[key] = Array.isArray(value) ? value : (value as string);
values[key] = {
column_src: Array.isArray(value) ? value : (value as string),
};
}
}
return values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export class ImportReportComponent implements OnInit {
mapField(listField: Field[], fieldMapping: FieldMappingValues): Array<CorrespondancesField> {
const mappedFields: Array<CorrespondancesField> = listField.map((field) => {
return {
source: fieldMapping[field.name_field],
source: fieldMapping[field.name_field].column_src,
description: field.comment,
destination: field.name_field,
};
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/app/modules/imports/models/mapping.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export interface Field {
}

export interface FieldMappingValues {
[propName: string]: string | string[];
[propName: string]: {
column_src: string | string[];
};
}

export interface FieldMapping extends Mapping {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,23 +333,27 @@ export class FieldMappingService {

this.mappingFormGroup.reset();
Object.entries(mappingvalues as FieldMappingValues).forEach(([target, source]) => {
// TO KEEP until we have migrated import templates
if (typeof source == 'string') {
source = { column_src: source };
}
let control = this.mappingFormGroup.get(target);
if (control) {
if (typeof source === 'object') {
let value = source;
let filtered = source.filter((x) => this.sourceFields.includes(x));
if (typeof source.column_src === 'object') {
let value = source.column_src;
let filtered = source.column_src.filter((x) => this.sourceFields.includes(x));
if (filtered.length > 0) {
value = filtered;
}
control.setValue(value);
} else {
if (
!this.sourceFields.includes(source) &&
!this.sourceFields.includes(source.column_src) &&
!(target in this.fieldMappingStatus.autogenerated)
) {
return;
}
control.setValue(source);
control.setValue(source.column_src);
}
}
});
Expand Down

0 comments on commit 68e327d

Please sign in to comment.