def import_action(self, request, *args, **kwargs):
if not self.has_import_permission(request):<EOL><INDENT>raise PermissionDenied<EOL><DEDENT>context = self.get_import_context_data()<EOL>import_formats = self.get_import_formats()<EOL>form_type = self.get_import_form()<EOL>form_kwargs = self.get_form_kwargs(form_type, *args, **kwargs)<EOL>form = form_type(import_formats,<EOL>request.POST or None,<EOL>request.FILES or None,<EOL>**form_kwargs)<EOL>if request.POST and form.is_valid():<EOL><INDENT>input_format = import_formats[<EOL>int(form.cleaned_data['<STR_LIT>'])<EOL>]()<EOL>import_file = form.cleaned_data['<STR_LIT>']<EOL>tmp_storage = self.write_to_tmp_storage(import_file, input_format)<EOL>try:<EOL><INDENT>data = tmp_storage.read(input_format.get_read_mode())<EOL>if not input_format.is_binary() and self.from_encoding:<EOL><INDENT>data = force_text(data, self.from_encoding)<EOL><DEDENT>dataset = input_format.create_dataset(data)<EOL><DEDENT>except UnicodeDecodeError as e:<EOL><INDENT>return HttpResponse(_(u"<STR_LIT>" % e))<EOL><DEDENT>except Exception as e:<EOL><INDENT>return HttpResponse(_(u"<STR_LIT>" % (type(e).__name__, import_file.name)))<EOL><DEDENT>res_kwargs = self.get_import_resource_kwargs(request, form=form, *args, **kwargs)<EOL>resource = self.get_import_resource_class()(**res_kwargs)<EOL>imp_kwargs = self.get_import_data_kwargs(request, form=form, *args, **kwargs)<EOL>result = resource.import_data(dataset, dry_run=True,<EOL>raise_errors=False,<EOL>file_name=import_file.name,<EOL>user=request.user,<EOL>**imp_kwargs)<EOL>context['<STR_LIT:result>'] = result<EOL>if not result.has_errors() and not result.has_validation_errors():<EOL><INDENT>initial = {<EOL>'<STR_LIT>': tmp_storage.name,<EOL>'<STR_LIT>': import_file.name,<EOL>'<STR_LIT>': form.cleaned_data['<STR_LIT>'],<EOL>}<EOL>confirm_form = self.get_confirm_import_form()<EOL>initial = self.get_form_kwargs(form=form, **initial)<EOL>context['<STR_LIT>'] = confirm_form(initial=initial)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>res_kwargs = self.get_import_resource_kwargs(request, form=form, *args, **kwargs)<EOL>resource = self.get_import_resource_class()(**res_kwargs)<EOL><DEDENT>context.update(self.admin_site.each_context(request))<EOL>context['<STR_LIT:title>'] = _("<STR_LIT>")<EOL>context['<STR_LIT>'] = form<EOL>context['<STR_LIT>'] = self.model._meta<EOL>context['<STR_LIT>'] = [f.column_name for f in resource.get_user_visible_fields()]<EOL>request.current_app = self.admin_site.name<EOL>return TemplateResponse(request, [self.import_template_name],<EOL>context)<EOL>
Perform a dry_run of the import to make sure the import will not
result in errors.  If there where no error, save the user
uploaded file to a local temp file that will be used by
'process_import' for the actual import.