Skip to content

Commit

Permalink
Update method reference in get_actions (#1681)
Browse files Browse the repository at this point in the history
* Update method reference in get_actions

The method reference in get_actions within import_export/admin.py was updated from using ExportActionMixin.export_admin_action to type(self).export_admin_action. This change ensures that the function call always refers to the current instance's class method. This is particularly important in situations where the class has been inherited and the method overridden.

* Added test for the `export_admin_action` override, fixing issue #1680.

* Added test for the `export_admin_action` override, fixing issue #1680.

* reformatting for black, fixing issue #1680.

* reformatting for black, fixing issue #1680.

* reformatting for black, fixing issue #1680.

---------

Co-authored-by: Willem Van Onsem <willem.vanonsem@prosafco.be>
  • Loading branch information
KommuSoft and Willem Van Onsem committed Nov 11, 2023
1 parent 6319211 commit d81869d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
3.3.3 (unreleased)
------------------

- Nothing changed yet.
- `.export_admin_action` can be overridden by subclassing it in the `ModelAdmin`.


3.3.2 (2023-11-09)
Expand Down
2 changes: 1 addition & 1 deletion import_export/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ def get_actions(self, request):
actions = super().get_actions(request)
actions.update(
export_admin_action=(
ExportActionMixin.export_admin_action,
type(self).export_admin_action,
"export_admin_action",
_("Export selected %(verbose_name_plural)s"),
)
Expand Down
3 changes: 2 additions & 1 deletion tests/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class BookAdmin(ImportExportModelAdmin):


class CategoryAdmin(ExportActionModelAdmin):
pass
def export_admin_action(self, request, queryset):
return super().export_admin_action(request, queryset)


class AuthorAdmin(ImportMixin, admin.ModelAdmin):
Expand Down
17 changes: 17 additions & 0 deletions tests/core/tests/test_admin_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@


class AdminTestMixin(object):
category_change_url = "/admin/core/category/"
book_import_url = "/admin/core/book/import/"
book_process_import_url = "/admin/core/book/process_import/"
legacybook_import_url = "/admin/core/legacybook/import/"
Expand Down Expand Up @@ -256,6 +257,22 @@ def test_import_legacy_book(self):
response, "Import finished, with 0 new and 1 updated legacy books."
)

def test_export_admin_action(self):
with mock.patch(
"core.admin.CategoryAdmin.export_admin_action"
) as mock_export_admin_action:
response = self.client.post(
self.category_change_url,
{
"action": "export_admin_action",
"index": "0",
"selected_across": "0",
"_selected_action": "0",
},
)
assert 200 <= response.status_code <= 399
mock_export_admin_action.assert_called()

def test_import_action_handles_UnicodeDecodeError_as_form_error(self):
with mock.patch(
"import_export.admin.TempFolderStorage.read"
Expand Down

0 comments on commit d81869d

Please sign in to comment.