[docs]defreport_erroneous_rows(imprt,entity,error_type,error_column,whereclause,level_validity_mapping={"ERROR":False},):""" This function report errors where whereclause in true. But the function also set validity column to False for errors with ERROR level. Warning: level of error "ERROR", the entity must be defined level_validity_mapping may be used to override default behavior: - level does not exist in dict: row validity is untouched - level exists in dict: row validity is set accordingly: - False: row is marked as erroneous - None: row is marked as should not be imported """transient_table=imprt.destination.get_transient_table()error_type=ImportUserErrorType.query.filter_by(name=error_type).one()error_column=generated_fields.get(error_column,error_column)error_column=imprt.fieldmapping.get(error_column,error_column)iferror_type.levelinlevel_validity_mapping:assertentityisnotNonecte=(update(transient_table).values({transient_table.c[entity.validity_column]:level_validity_mapping[error_type.level],}).where(transient_table.c.id_import==imprt.id_import).where(whereclause).returning(transient_table.c.line_no).cte("cte"))else:cte=(select(transient_table.c.line_no).where(transient_table.c.id_import==imprt.id_import).where(whereclause).cte("cte"))insert_args={ImportUserError.id_import:literal(imprt.id_import).label("id_import"),ImportUserError.id_type:literal(error_type.pk).label("id_type"),ImportUserError.rows:array_agg(aggregate_order_by(cte.c.line_no,cte.c.line_no)).label("rows"),ImportUserError.column:literal(error_column).label("error_column"),}ifentityisnotNone:insert_args.update({ImportUserError.id_entity:literal(entity.id_entity).label("id_entity"),})# Create the final insert statementerror_select=select(insert_args.values()).alias("error")stmt=insert(ImportUserError).from_select(names=insert_args.keys(),select=(select(error_select).where(error_select.c.rows!=None)),)db.session.execute(stmt)