Skip to content

Commit 3d7c943

Browse files
timthelionPetrDlouhyandrewgy8
authored
Add row_number parameter to before_import_row, after_import_row and after_import_instance (django-import-export#1040)
Co-authored-by: Petr Dlouhý <petr.dlouhy@email.cz> Co-authored-by: Andrew Graham-Yooll <andrewgy8@gmail.com>
1 parent e0af04c commit 3d7c943

File tree

3 files changed

+27
-83
lines changed

3 files changed

+27
-83
lines changed

docs/changelog.rst

+4-80
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Changelog
55
2.1.1 (unreleased)
66
------------------
77

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

1010

1111
2.1.0 (2020-05-02)
@@ -46,85 +46,9 @@ Changelog
4646
2.0 (2019-12-03)
4747
----------------
4848

49-
- [django2.2] Add real support of Django 2.2 before 3.0 is out (#1021)
50-
51-
- fix: DateTimeWidget not timezone sensitive (#813) (#943)
52-
53-
- Move actions definition to ExportActionMixin (#992)
54-
55-
- Add language support: Turkish (#1013)
56-
57-
- Fix exception import for Django 3 (#1010)
58-
59-
- Fix potential header / row column mismatches for invalid rows in… (#995)
60-
61-
- Assume user is importing new data if id fields not included (#996)
62-
63-
- Fix bug with spaces in export filename, pass request and queryset (#980)
64-
65-
- Simplify Django version in TravisCI (#970)
66-
67-
- Merge pull request #966 from andrewgy8/bump-stale-bot-time
68-
69-
- Align error in rtl mode (#954)
70-
71-
- Add dutch translations (#951, #1024)
72-
73-
- Add 3.8-dev to travis ci (#926)
74-
75-
- Fix style in getting_started docs (#952)
76-
77-
- Update documentation to show that mixins must be referenced before admin.ModelAdmin. (#946)
78-
79-
- JSONWidget updated with null value fix (#928)
80-
81-
- Import rows have background color (#929)
82-
83-
- Use resource get_queryset in ModelInstanceLoader (#920)
84-
85-
- Simplify coerce to text type (#887)
86-
87-
- More flexibility in ConfirmImportForm, forms and resource kwargs (#893)
88-
89-
- Add JSON B type field mapping (#904)
90-
91-
- Scale back stale bot's time-to-stale (#918)
92-
93-
- test: explicitly order qs in ManyToManyWidget
94-
95-
- Add mysql to travis
96-
97-
- Expand doc strings to include Mixin superclasses (#914)
98-
99-
- Remove python2 compatibility decorator
100-
101-
- chore: fix Imports are incorrectly sorted.
102-
103-
- Use global env vars for postgres
104-
105-
- Used non-fixed id for test. Database is not torn down after each run, which means that the id is incrementing
106-
107-
- Fix warning from assertEquals
108-
109-
- Add psycopg2 as postgres driver to test requirements
110-
111-
- Add django version to the matrix
112-
113-
- Add matrix for sqlite and postgres testing
114-
115-
- Correct mistaken assertTrue() -> assertEquals()
116-
117-
- chore: add package long_description
118-
119-
- chore: add python wheels to dev requirements (#890)
120-
121-
- Add github directory with PR and issue templates
122-
123-
- Isort all the things
124-
125-
- Use coveralls master branch tag in the readme
126-
127-
- Remove support for Django < 2.0 and Python < 3.5
49+
- Removed support for Django < 2.0
50+
- Removed support for Python < 3.5
51+
- feat: Support for Postgres JSONb Field (#904)
12852

12953
1.2.0 (2019-01-10)
13054
------------------

import_export/resources.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -495,19 +495,19 @@ def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
495495
"""
496496
pass
497497

498-
def before_import_row(self, row, **kwargs):
498+
def before_import_row(self, row, row_number=None, **kwargs):
499499
"""
500500
Override to add additional logic. Does nothing by default.
501501
"""
502502
pass
503503

504-
def after_import_row(self, row, row_result, **kwargs):
504+
def after_import_row(self, row, row_result, row_number=None, **kwargs):
505505
"""
506506
Override to add additional logic. Does nothing by default.
507507
"""
508508
pass
509509

510-
def after_import_instance(self, instance, new, **kwargs):
510+
def after_import_instance(self, instance, new, row_number=None, **kwargs):
511511
"""
512512
Override to add additional logic. Does nothing by default.
513513
"""
@@ -661,6 +661,7 @@ def import_data_inner(self, dataset, dry_run, raise_errors, using_transactions,
661661
instance_loader,
662662
using_transactions=using_transactions,
663663
dry_run=dry_run,
664+
row_number=i,
664665
**kwargs
665666
)
666667
result.increment_row_result_total(row_result)

tests/core/tests/test_resources.py

+19
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ class Meta:
155155
exclude = ('imported', )
156156

157157

158+
class BookResourceWithLineNumberLogger(BookResource):
159+
def __init__(self, *args, **kwargs):
160+
self.before_lines = []
161+
self.after_lines = []
162+
return super().__init__(*args, **kwargs)
163+
164+
def before_import_row(self,row, row_number=None, **kwargs):
165+
self.before_lines.append(row_number)
166+
167+
def after_import_row(self, row, row_result, row_number=None, **kwargs):
168+
self.after_lines.append(row_number)
169+
170+
158171
class CategoryResource(resources.ModelResource):
159172

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

414+
def test_importing_with_line_number_logging(self):
415+
resource = BookResourceWithLineNumberLogger()
416+
result = resource.import_data(self.dataset, raise_errors=True)
417+
self.assertEqual(resource.before_lines, [1])
418+
self.assertEqual(resource.after_lines, [1])
419+
401420
def test_import_data_raises_field_specific_validation_errors(self):
402421
resource = AuthorResource()
403422
dataset = tablib.Dataset(headers=['id', 'name', 'birthday'])

0 commit comments

Comments
 (0)