Repository: django-import-export/django-import-export

Issue:
Pass Django user when resource instantiated instead to every function call
I previously made a change to how `resource.get_fields()` is called by `import_action()` to take `**kwargs`, so that `get_fields()` would be able to change its behaviour depending on the Django user, like was being done by the call to `resource.import_data()`.

I'm thinking now that it is cleaner to pass `**kwargs` to the constructor for the resource object instead, on the line `resource = self.get_import_resource_class()()`. It's clearer for the resource to get the user in its `__init__` and store it, than to have the user passed as a parameter to every function call the resource has.

I'd be happy to make the change if you agree!



Discussion:
Hi @shaggyfrog. Yes, I agree. As we did not make a new release I think we can just revert changes in

https://github.com/django-import-export/django-import-export/pull/453/files


Resolving patch:
diff --git a/import_export/admin.py b/import_export/admin.py
--- a/import_export/admin.py
+++ b/import_export/admin.py
@@ -118,6 +118,12 @@ def get_urls(self):
         ]
         return my_urls + urls
 
+    def get_resource_kwargs(self, request, *args, **kwargs):
+        return {}
+
+    def get_import_resource_kwargs(self, request, *args, **kwargs):
+        return self.get_resource_kwargs(request, *args, **kwargs)
+
     def get_resource_class(self):
         if not self.resource_class:
             return modelresource_factory(self.model)
@@ -142,7 +148,7 @@ def process_import(self, request, *args, **kwargs):
         wishes to import)
         '''
         opts = self.model._meta
-        resource = self.get_import_resource_class()()
+        resource = self.get_import_resource_class()(**self.get_import_resource_kwargs(request, *args, **kwargs))
 
         confirm_form = ConfirmImportForm(request.POST)
         if confirm_form.is_valid():
@@ -204,7 +210,7 @@ def import_action(self, request, *args, **kwargs):
         uploaded file to a local temp file that will be used by
         'process_import' for the actual import.
         '''
-        resource = self.get_import_resource_class()()
+        resource = self.get_import_resource_class()(**self.get_import_resource_kwargs(request, *args, **kwargs))
 
         context = {}
 
@@ -259,7 +265,7 @@ def import_action(self, request, *args, **kwargs):
 
         context['form'] = form
         context['opts'] = self.model._meta
-        context['fields'] = [f.column_name for f in resource.get_fields(user=request.user)]
+        context['fields'] = [f.column_name for f in resource.get_fields()]
 
         request.current_app = self.admin_site.name
         return TemplateResponse(request, [self.import_template_name],
@@ -290,6 +296,12 @@ def get_urls(self):
         ]
         return my_urls + urls
 
+    def get_resource_kwargs(self, request, *args, **kwargs):
+        return {}
+
+    def get_export_resource_kwargs(self, request, *args, **kwargs):
+        return self.get_resource_kwargs(request, *args, **kwargs)
+
     def get_resource_class(self):
         if not self.resource_class:
             return modelresource_factory(self.model)
@@ -343,8 +355,9 @@ def get_export_data(self, file_format, queryset, *args, **kwargs):
         """
         Returns file_format representation for given queryset.
         """
+        request = kwargs.pop("request")
         resource_class = self.get_export_resource_class()
-        data = resource_class().export(queryset, *args, **kwargs)
+        data = resource_class(**self.get_export_resource_kwargs(request)).export(queryset, *args, **kwargs)
         export_data = file_format.export_data(data)
         return export_data
 
@@ -357,7 +370,7 @@ def export_action(self, request, *args, **kwargs):
             ]()
 
             queryset = self.get_export_queryset(request)
-            export_data = self.get_export_data(file_format, queryset, user=request.user)
+            export_data = self.get_export_data(file_format, queryset, request=request)
             content_type = file_format.get_content_type()
             # Django 1.7 uses the content_type kwarg instead of mimetype
             try:
@@ -435,7 +448,7 @@ def export_admin_action(self, request, queryset):
             formats = self.get_export_formats()
             file_format = formats[int(export_format)]()
 
-            export_data = self.get_export_data(file_format, queryset)
+            export_data = self.get_export_data(file_format, queryset, request=request)
             content_type = file_format.get_content_type()
             # Django 1.7 uses the content_type kwarg instead of mimetype
             try:


Test patch: