Skip to content

Commit

Permalink
Move logic to private method
Browse files Browse the repository at this point in the history
  • Loading branch information
yalef committed Nov 27, 2023
1 parent c6546f6 commit b5cb24e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
52 changes: 31 additions & 21 deletions import_export_extensions/resources.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import typing
from enum import Enum

Expand Down Expand Up @@ -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."""
Expand Down
7 changes: 7 additions & 0 deletions import_export_extensions/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit b5cb24e

Please sign in to comment.