Skip to content

Commit

Permalink
Added error rows import handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrotsko committed Nov 4, 2024
1 parent cd6d64d commit d691729
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/signals/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from import_export import resources
from import_export.fields import Field, widgets
from import_export.results import RowResult

from datasources.models import SourceSubdivision
from datasources.resources import process_links
Expand Down Expand Up @@ -152,7 +153,35 @@ def process_base(row) -> None:
row["base"] = base_signal.id


class SignalBaseResource(resources.ModelResource):
class ModelResource(resources.ModelResource):
def get_field_names(self):
names = []
for field in self.get_fields():
names.append(self.get_field_name(field))
return names

def import_row(self, row, instance_loader, **kwargs):
# overriding import_row to ignore errors and skip rows that fail to import
# without failing the entire import
import_result = super(ModelResource, self).import_row(
row, instance_loader, **kwargs
)

if import_result.import_type in [RowResult.IMPORT_TYPE_ERROR, RowResult.IMPORT_TYPE_INVALID]:
import_result.diff = [row.get(name, "") for name in self.get_field_names()]

# Add a column with the error message
import_result.diff.append(
"Errors: {}".format([err.error for err in import_result.errors])
)
# clear errors and mark the record to skip
import_result.errors = []
import_result.import_type = RowResult.IMPORT_TYPE_SKIP

return import_result


class SignalBaseResource(ModelResource):
"""
Resource class for importing Signals base.
"""
Expand Down Expand Up @@ -180,7 +209,7 @@ def before_import_row(self, row, **kwargs) -> None:
process_base(row)


class SignalResource(resources.ModelResource):
class SignalResource(ModelResource):
"""
Resource class for importing and exporting Signal models
"""
Expand Down

0 comments on commit d691729

Please sign in to comment.