diff --git a/import_export_extensions/resources.py b/import_export_extensions/resources.py index 12e4357..cff23bf 100644 --- a/import_export_extensions/resources.py +++ b/import_export_extensions/resources.py @@ -1,3 +1,4 @@ +import collections import typing from enum import Enum @@ -136,29 +137,38 @@ def import_row( else TaskState.PARSING.name ), ) - if not force_import: - return imported_row - if ( - imported_row.import_type == RowResult.IMPORT_TYPE_ERROR - or imported_row.import_type == RowResult.IMPORT_TYPE_INVALID - ): - imported_row.diff = [] - for field in self.get_fields(): - imported_row.diff.append(row.get(field.column_name, "")) - - imported_row.non_field_skipped_errors.extend( - imported_row.errors, - ) - if imported_row.validation_error is not None: - imported_row.field_skipped_errors.update( - **imported_row.validation_error.error_dict, - ) - imported_row.errors = [] - imported_row.validation_error = None - - imported_row.import_type = RowResult.IMPORT_TYPE_SKIP + if force_import and imported_row.has_error_import_type: + imported_row = self._skip_row_with_errors(imported_row, row) return imported_row + def _skip_row_with_errors( + self, + row_result: RowResult, + row_data: collections.OrderedDict[str, str], + ) -> RowResult: + """Process row as skipped. + + Move row errors to skipped errors attributes. + Change import type to skipped. + + """ + row_result.diff = [] + for field in self.get_fields(): + row_result.diff.append(row_data.get(field.column_name, "")) + + row_result.non_field_skipped_errors.extend( + row_result.errors, + ) + if row_result.validation_error is not None: + row_result.field_skipped_errors.update( + **row_result.validation_error.error_dict, + ) + row_result.errors = [] + row_result.validation_error = None + + row_result.import_type = RowResult.IMPORT_TYPE_SKIP + return row_result + @classmethod def get_row_result_class(self): """Return custom row result class.""" diff --git a/import_export_extensions/results.py b/import_export_extensions/results.py index 3c7777d..ce331fd 100644 --- a/import_export_extensions/results.py +++ b/import_export_extensions/results.py @@ -43,6 +43,13 @@ def skipped_errors_count(self) -> int: + len(self.field_skipped_errors) ) + @property + def has_error_import_type(self) -> bool: + """Return true if import type is not valid.""" + if self.import_type not in self.valid_import_types: + return True + return False + class Result(results.Result): """Custom result class with ability to store info about skipped rows."""