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

Remove code duplication from mixins.py and admin.py #1277

Merged
merged 3 commits into from
May 1, 2021

Conversation

matthewhegarty
Copy link
Contributor

@matthewhegarty matthewhegarty commented Apr 28, 2021

Problem

  • A bug where args and kwargs are not passed to get_export_resource_kwargs()
  • Removed code duplication in admin.py and mixins.py

Solution

When looking into this issue, I noticed that we have a fair amount of code duplication in mixins.py and admin.py.
I removed the duplication as part of this fix.

I tried to keep code changes to a minimum, and also not to affect the public API defined in mixins.
I added some tests to improve the coverage in this area.

Would appreciate a code review from @manelclos and @ZuluPro

Related comments by @manelclos

base mixin for import and base mixin for export, reused in the ViewMixin and the Admin Mixin, share code as much as possible when it makes sense.

Issue 1268

Added get_data_for_export() to remove duplication and to pass the args and kwargs to get_export_resource_kwargs() which was the original issue reported in #1268.
Also added a unit test for this.

Updated mixins.py

Removed the following duplicate methods from ExportViewMixin and added to
ImportExportMixin:

def get_resource_class(self):
def get_resource_kwargs(self, request, *args, **kwargs):

Removed the following duplicate methods from ExportViewMixin and added to
BaseImportMixin:

def get_import_resource_class(self):
def get_import_formats(self):
def get_import_resource_kwargs(self, request, *args, **kwargs):

Removed the following duplicate methods from ExportViewMixin and added to
BaseExportMixin:

def get_export_formats(self):
def get_export_resource_class(self):
def get_export_resource_kwargs(self, request, *args, **kwargs):
def get_export_filename(self, file_format):

Updated admin.py

I didn't rename any classes here because they might be part of the public API.

  • ImportMixin now subclasses BaseImportMixin (and ImportExportMixinBase)

  • Updated ImportMixin: Removed the following duplicate methods (now inherited
    from BaseImportMixin):

def get_resource_kwargs(self, request, *args, **kwargs):
def get_import_resource_kwargs(self, request, *args, **kwargs):
def get_resource_class(self):
def get_import_resource_class(self):
def get_import_formats(self):
  • ExportMixin now subclasses BaseExportMixin (and ImportExportMixinBase)

  • Updated ExportMixin: Removed the following duplicate methods (now inherited
    from BaseExportMixin):

def get_resource_kwargs(self, request, *args, **kwargs):
def get_export_resource_kwargs(self, request, *args, **kwargs):
def get_resource_class(self):
def get_export_resource_class(self):
def get_export_formats(self):

# I kept this filename for API compatibility, even though 'request' and
# 'queryset' are not used
def get_export_filename(self, request, queryset, file_format):

Acceptance Criteria

Test coverage

  • increased admin.py coverage to 100%
  • increased mixins.py coverage to 100%

Testing

  • Ran test suite
  • Manually exported & imported Book
  • Manually exported Category (using 'action' menu)

@matthewhegarty matthewhegarty marked this pull request as ready for review April 28, 2021 15:17
@matthewhegarty matthewhegarty changed the title Issue 1268 Remove code duplication from mixins.py and admin.py Apr 28, 2021
@ZuluPro
Copy link
Contributor

ZuluPro commented Apr 28, 2021

Hi @matthewhegarty

Looks good to me!
Could you just fix the failling test ?

@coveralls
Copy link

coveralls commented Apr 28, 2021

Coverage Status

Coverage increased (+0.5%) to 97.315% when pulling 0acad28 on matthewhegarty:issue-1268 into 722c376 on django-import-export:master.

@matthewhegarty
Copy link
Contributor Author

Thanks for reviewing @ZuluPro

Copy link
Contributor

@manelclos manelclos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far. 👏 for the refactor and the increased coverage.

import_export/mixins.py Outdated Show resolved Hide resolved
import_export/mixins.py Outdated Show resolved Hide resolved
import_export/mixins.py Outdated Show resolved Hide resolved
import_export/mixins.py Show resolved Hide resolved
import_export/mixins.py Show resolved Hide resolved
@matthewhegarty
Copy link
Contributor Author

@manelclos Many thanks for reviewing this. I have responded to your comments.

Copy link
Contributor

@manelclos manelclos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great work on the refactor and the tests, thanks!


def get_export_filename(self, file_format):
def get_filename(self, file_format):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say that you can keep this function as get_export_filename and don't implement it in ExportViewMixin, but not important.

Copy link
Contributor Author

@matthewhegarty matthewhegarty May 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have misunderstood you, but I think this runs into the problem of get_export_filename() being called in two places with the different args.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the admin.py uses the different arguments, and you are already adapting that call inside ExportMixin:

https://github.com/django-import-export/django-import-export/pull/1277/files#diff-a6dabb2139e7ec78138476e9a12311c22cc93cbeab4a93994d4f95f64934805eR437-R438

But I could be wrong :)

Copy link
Member

@andrewgy8 andrewgy8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the work @matthewhegarty

@@ -453,8 +385,7 @@ def get_export_data(self, file_format, queryset, *args, **kwargs):
if not self.has_export_permission(request):
raise PermissionDenied

resource_class = self.get_export_resource_class()
data = resource_class(**self.get_export_resource_kwargs(request)).export(queryset, *args, **kwargs)
data = self.get_data_for_export(request, queryset, *args, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice refactor. Its nice to see

data = resource_class(**self.get_export_resource_kwargs(request)).export(queryset, *args, **kwargs)

go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants