def test_choices_does_not_get_generated_label_if_single_choice(self): form = ConfirmImportForm(headers=[(1, "Hi")]) first_choice = form.fields["from_index"].choices[0] self.assertNotEqual(first_choice[0], "") self.assertNotEqual(first_choice[1], "---")
def test_choices_get_appended_with_intro_label_if_multiple(self): form = ConfirmImportForm(headers=[(0, "From"), (1, "To")]) first_choice = form.fields["from_index"].choices[0] self.assertEqual(first_choice[0], "") self.assertEqual(first_choice[1], "---")
def start_import(request): supported_extensions = get_supported_extensions() from_encoding = "utf-8" query_string = request.GET.get('q', "") if request.POST or request.FILES: form_kwargs = {} form = ImportForm(supported_extensions, request.POST or None, request.FILES or None, **form_kwargs) else: form = ImportForm(supported_extensions) if not request.FILES or not form.is_valid(): return render( request, "wagtailredirects/choose_import_file.html", { 'search_form': SearchForm(data=dict(q=query_string) if query_string else None, placeholder=_("Search redirects")), "form": form, }, ) import_file = form.cleaned_data["import_file"] _name, extension = os.path.splitext(import_file.name) extension = extension.lstrip(".") if extension not in supported_extensions: messages.error( request, _('File format of type "{}" is not supported').format(extension)) return redirect('wagtailredirects:start_import') import_format_cls = get_format_cls_by_extension(extension) input_format = import_format_cls() file_storage = write_to_file_storage(import_file, input_format) try: 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) except UnicodeDecodeError as e: messages.error(request, _("Imported file has a wrong encoding: %s") % e) return redirect('wagtailredirects:start_import') except Exception as e: # pragma: no cover messages.error( request, _("%(error)s encountered while trying to read file: %(filename)s") % { 'error': type(e).__name__, 'filename': import_file.name }) return redirect('wagtailredirects:start_import') initial = { "import_file_name": file_storage.name, "original_file_name": import_file.name, "input_format": get_import_formats().index(import_format_cls), } return render( request, "wagtailredirects/confirm_import.html", { "form": ConfirmImportForm(dataset.headers, initial=initial), "dataset": dataset, }, )
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")