Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add row_number parameter to before_import_row, after_import_row and after_import_instance #1040

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 4 additions & 80 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
2.1.1 (unreleased)
------------------

- Nothing changed yet.
- add row_number parameter to before_import_row(), after_import_instance() and after_import_row()


2.1.0 (2020-05-02)
Expand Down Expand Up @@ -46,85 +46,9 @@ Changelog
2.0 (2019-12-03)
----------------

- [django2.2] Add real support of Django 2.2 before 3.0 is out (#1021)

- fix: DateTimeWidget not timezone sensitive (#813) (#943)

- Move actions definition to ExportActionMixin (#992)

- Add language support: Turkish (#1013)

- Fix exception import for Django 3 (#1010)

- Fix potential header / row column mismatches for invalid rows in… (#995)

- Assume user is importing new data if id fields not included (#996)

- Fix bug with spaces in export filename, pass request and queryset (#980)

- Simplify Django version in TravisCI (#970)

- Merge pull request #966 from andrewgy8/bump-stale-bot-time

- Align error in rtl mode (#954)

- Add dutch translations (#951, #1024)

- Add 3.8-dev to travis ci (#926)

- Fix style in getting_started docs (#952)

- Update documentation to show that mixins must be referenced before admin.ModelAdmin. (#946)

- JSONWidget updated with null value fix (#928)

- Import rows have background color (#929)

- Use resource get_queryset in ModelInstanceLoader (#920)

- Simplify coerce to text type (#887)

- More flexibility in ConfirmImportForm, forms and resource kwargs (#893)

- Add JSON B type field mapping (#904)

- Scale back stale bot's time-to-stale (#918)

- test: explicitly order qs in ManyToManyWidget

- Add mysql to travis

- Expand doc strings to include Mixin superclasses (#914)

- Remove python2 compatibility decorator

- chore: fix Imports are incorrectly sorted.

- Use global env vars for postgres

- Used non-fixed id for test. Database is not torn down after each run, which means that the id is incrementing

- Fix warning from assertEquals

- Add psycopg2 as postgres driver to test requirements

- Add django version to the matrix

- Add matrix for sqlite and postgres testing

- Correct mistaken assertTrue() -> assertEquals()

- chore: add package long_description

- chore: add python wheels to dev requirements (#890)

- Add github directory with PR and issue templates

- Isort all the things

- Use coveralls master branch tag in the readme

- Remove support for Django < 2.0 and Python < 3.5
- Removed support for Django < 2.0
- Removed support for Python < 3.5
- feat: Support for Postgres JSONb Field (#904)

1.2.0 (2019-01-10)
------------------
Expand Down
7 changes: 4 additions & 3 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,19 +495,19 @@ def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
"""
pass

def before_import_row(self, row, **kwargs):
def before_import_row(self, row, row_number=None, **kwargs):
"""
Override to add additional logic. Does nothing by default.
"""
pass

def after_import_row(self, row, row_result, **kwargs):
def after_import_row(self, row, row_result, row_number=None, **kwargs):
"""
Override to add additional logic. Does nothing by default.
"""
pass

def after_import_instance(self, instance, new, **kwargs):
def after_import_instance(self, instance, new, row_number=None, **kwargs):
"""
Override to add additional logic. Does nothing by default.
"""
Expand Down Expand Up @@ -661,6 +661,7 @@ def import_data_inner(self, dataset, dry_run, raise_errors, using_transactions,
instance_loader,
using_transactions=using_transactions,
dry_run=dry_run,
row_number=i,
**kwargs
)
result.increment_row_result_total(row_result)
Expand Down
19 changes: 19 additions & 0 deletions tests/core/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ class Meta:
exclude = ('imported', )


class BookResourceWithLineNumberLogger(BookResource):
def __init__(self, *args, **kwargs):
self.before_lines = []
self.after_lines = []
return super().__init__(*args, **kwargs)

def before_import_row(self,row, row_number=None, **kwargs):
self.before_lines.append(row_number)

def after_import_row(self, row, row_result, row_number=None, **kwargs):
self.after_lines.append(row_number)


class CategoryResource(resources.ModelResource):

class Meta:
Expand Down Expand Up @@ -398,6 +411,12 @@ def test_import_data(self):
self.assertEqual(instance.author_email, 'test@example.com')
self.assertEqual(instance.price, Decimal("10.25"))

def test_importing_with_line_number_logging(self):
resource = BookResourceWithLineNumberLogger()
result = resource.import_data(self.dataset, raise_errors=True)
self.assertEqual(resource.before_lines, [1])
self.assertEqual(resource.after_lines, [1])

def test_import_data_raises_field_specific_validation_errors(self):
resource = AuthorResource()
dataset = tablib.Dataset(headers=['id', 'name', 'birthday'])
Expand Down