@@ -252,12 +252,6 @@ def import_data_to_destination(imprt: TImports) -> None:
252252 if entity .code == "visit" :
253253 core_select_cols .append (sa .literal (imprt .destination .id_module ).label ("id_module" ))
254254 core_dest_col_names .append ("id_module" )
255- # FIXME: should be done automatically :/
256- if "v__observers" in map (lambda x : x .name_field , entity_fields ):
257- core_select_cols .append (
258- transient_table .c .src_v__observers .label ("observers_txt" )
259- )
260- core_dest_col_names .append ("observers_txt" )
261255 core_select_stmt = (
262256 sa .select (* core_select_cols )
263257 .where (transient_table .c .id_import == imprt .id_import )
@@ -419,6 +413,7 @@ def import_data_to_destination(imprt: TImports) -> None:
419413 ),
420414 )
421415 )
416+ )
422417
423418 if complement_select_stmt is not None :
424419 if entity .code == "site" and is_sites_group_id_fields :
@@ -460,16 +455,33 @@ def import_data_to_destination(imprt: TImports) -> None:
460455 yield (batch + 1 ) / batch_count
461456 imprt .statistics .update ({f"{ entity .code } _count" : row_count })
462457
463- if entity .code == "visit" :
464- ImportActions .bind_matched_observers (
465- imprt ,
466- TBaseVisits ,
467- "observers_txt" ,
468- "id_base_visit" ,
469- CorVisitObserver ,
470- ["id_base_visit" , "id_role" ],
471- )
472-
458+ if entity .code == "visit" and MonitoringImportActions .is_observer_mapping_enabled ():
459+
460+ if "v__observers" in imprt .fieldmapping :
461+ if imprt .fieldmapping ["v__observers" ].get ("constant_value" , None ) is not None :
462+ db .session .execute (
463+ sa .insert (CorVisitObserver ).from_select (
464+ ["id_base_visit" , "id_role" ],
465+ sa .select (
466+ transient_table .c .id_base_visit ,
467+ sa .func .unnest (transient_table .c ["v__observers" ]).label (
468+ "id_role"
469+ ),
470+ )
471+ .where (transient_table .c ["v__observers" ] != None )
472+ .where (transient_table .c .id_import == imprt .id_import )
473+ .where (transient_table .c [entity .validity_column ] == True ),
474+ )
475+ )
476+ else :
477+ ImportActions .bind_matched_observers (
478+ imprt ,
479+ transient_table ,
480+ "src_v__observers" ,
481+ "id_base_visit" ,
482+ CorVisitObserver ,
483+ ["id_base_visit" , "id_role" ],
484+ )
473485 imprt .statistics .update (ObservationImportActions .compute_taxa_statistics (imprt ))
474486
475487 # filter empty statistics
0 commit comments