Skip to content

Commit

Permalink
Return kwargs by default from get_resource_kwargs (#1748)
Browse files Browse the repository at this point in the history
* added test and fix

* updated test comments

* updated comment

* updated comments

* updated changelog
  • Loading branch information
matthewhegarty authored Jan 31, 2024
1 parent e113153 commit 43423b6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Changelog
- Pass :meth:`~import_export.mixins.BaseExportMixin.get_export_resource_kwargs` to Resource constructor
:meth:`~import_export.admin.ExportMixin.export_action` (#1739)
- Fix issue with model class passed to Resource constructor crashing on export (#1745)
- Unit and integration test for changes to BaseImportExportMixin.get_resource_kwargs added (#1749)
- Return ``kwargs``` by default from :meth:`~import_export.mixins.BaseImportExportMixin.get_resource_kwargs` (#1748)

3.3.6 (2024-01-10)
------------------

Expand Down
20 changes: 20 additions & 0 deletions import_export/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@


class BaseImportExportMixin:
"""
Base mixin for functionality related to importing and exporting via the Admin
interface.
"""

resource_class = None
resource_classes = []

Expand Down Expand Up @@ -63,9 +68,24 @@ def get_resource_classes(self):
return [self.resource_class]

def get_resource_kwargs(self, request, *args, **kwargs):
"""
Return the kwargs which are to be passed to the Resource constructor.
Can be overridden to provide additional kwarg params.
:param request: The request object.
:param args: Positional arguments.
:param kwargs: Keyword arguments.
:returns: The Resource kwargs (by default, is the kwargs passed).
"""
return kwargs

def get_resource_index(self, form):
"""
Return the index of the resource class defined in the form.
:param form: The form object.
:returns: The index of the resource as an int.
"""
resource_index = 0
if form and "resource" in form.cleaned_data:
try:
Expand Down
36 changes: 20 additions & 16 deletions tests/core/tests/test_admin_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,6 @@ def test_import_export_template(self):
self.assertContains(response, _("Export"))
self.assertContains(response, "Custom change list item")

@patch("import_export.admin.ImportMixin.choose_import_resource_class")
def test_import_passes_correct_kwargs_to_constructor(
self, mock_choose_import_resource_class
):
class TestResource(ModelResource):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.kwargs = kwargs
if "form" not in kwargs:
raise Exception("No form")

mock_choose_import_resource_class.return_value = TestResource

response = self._do_import_post(self.book_import_url, "books.csv")
self.assertEqual(response.status_code, 200)

@override_settings(TEMPLATE_STRING_IF_INVALID="INVALID_VARIABLE")
def test_import(self):
# GET the import form
Expand Down Expand Up @@ -191,6 +175,26 @@ def test_correct_scripts_declared_when_debug_is_true(self):
html=True,
)

@patch("import_export.admin.ImportMixin.choose_import_resource_class")
def test_import_passes_correct_kwargs_to_constructor(
self, mock_choose_import_resource_class
):
# issue 1741
class TestResource(ModelResource):
def __init__(self, **kwargs):
super().__init__(**kwargs)

# the form is passed as a kwarg to the Resource constructor
# if not present, then it means that the original kwargs were lost
if "form" not in kwargs:
raise Exception("No form")

# mock the returned resource class so that we can inspect constructor params
mock_choose_import_resource_class.return_value = TestResource

response = self._do_import_post(self.book_import_url, "books.csv")
self.assertEqual(response.status_code, 200)

@override_settings(DEBUG=False)
def test_correct_scripts_declared_when_debug_is_false(self):
# GET the import form
Expand Down

0 comments on commit 43423b6

Please sign in to comment.