def process_import(request): supported_extensions = get_supported_extensions() from_encoding = "utf-8" form_kwargs = {} form = ConfirmImportForm(DEFAULT_FORMATS, request.POST or None, request.FILES or None, **form_kwargs) is_confirm_form_valid = form.is_valid() import_formats = get_import_formats() input_format = import_formats[int(form.cleaned_data["input_format"])]() FileStorage = get_file_storage() file_storage = FileStorage(name=form.cleaned_data["import_file_name"]) if not is_confirm_form_valid: data = file_storage.read(input_format.get_read_mode()) if not input_format.is_binary() and from_encoding: data = force_str(data, from_encoding) dataset = input_format.create_dataset(data) initial = { "import_file_name": file_storage.name, "original_file_name": form.cleaned_data["import_file_name"], } return render( request, "wagtailredirects/confirm_import.html", { "form": ConfirmImportForm( dataset.headers, request.POST or None, request.FILES or None, initial=initial, ), "dataset": dataset, }, ) data = file_storage.read(input_format.get_read_mode()) if not input_format.is_binary() and from_encoding: data = force_str(data, from_encoding) dataset = input_format.create_dataset(data) import_summary = create_redirects_from_dataset( dataset, { "from_index": int(form.cleaned_data["from_index"]), "to_index": int(form.cleaned_data["to_index"]), "permanent": form.cleaned_data["permanent"], "site": form.cleaned_data["site"], }, ) file_storage.remove() if import_summary["errors_count"] > 0: return render( request, "wagtailredirects/import_summary.html", { "form": ImportForm(supported_extensions), "import_summary": import_summary, }, ) total = import_summary["total"] messages.success( request, ngettext("Imported %(total)d redirect", "Imported %(total)d redirects", total) % {'total': total}) return redirect('wagtailredirects:index')
def process_import(request): supported_extensions = get_supported_extensions() from_encoding = "utf-8" management_form = ConfirmImportManagementForm(request.POST) if not management_form.is_valid(): # Unable to unsign the hidden form data, or the data is missing, that's suspicious. raise SuspiciousOperation( f"Invalid management form, data is missing or has been tampered with:\n" f"{management_form.errors.as_text()}") input_format = get_import_formats()[int( management_form.cleaned_data["input_format"])]() FileStorage = get_file_storage() file_storage = FileStorage( name=management_form.cleaned_data["import_file_name"]) data = file_storage.read(input_format.get_read_mode()) if not input_format.is_binary() and from_encoding: data = force_str(data, from_encoding) dataset = input_format.create_dataset(data) # Now check if the rest of the management form is valid form = ConfirmImportForm( dataset.headers, request.POST, request.FILES, initial=management_form.cleaned_data, ) if not form.is_valid(): return render( request, "wagtailredirects/confirm_import.html", { "form": form, "dataset": dataset, }, ) import_summary = create_redirects_from_dataset( dataset, { "from_index": int(form.cleaned_data["from_index"]), "to_index": int(form.cleaned_data["to_index"]), "permanent": form.cleaned_data["permanent"], "site": form.cleaned_data["site"], }, ) file_storage.remove() if import_summary["errors_count"] > 0: return render( request, "wagtailredirects/import_summary.html", { "form": ImportForm(supported_extensions), "import_summary": import_summary, }, ) total = import_summary["total"] messages.success( request, ngettext("Imported %(total)d redirect", "Imported %(total)d redirects", total) % {"total": total}, ) return redirect("wagtailredirects:index")