Only support csv, tsv and xlsx for redirect import

pull/9265/head
Jaap Roes 2022-07-06 13:08:30 +02:00 zatwierdzone przez LB (Ben Johnston)
rodzic d8ba045826
commit c4edac723e
9 zmienionych plików z 7 dodań i 97 usunięć

Wyświetl plik

@ -34,7 +34,7 @@ install_requires = [
"requests>=2.11.1,<3.0",
"l18n>=2018.5",
"xlsxwriter>=1.2.8,<4.0",
"tablib[xls,xlsx]>=0.14.0",
"tablib[xlsx]>=0.14.0",
"anyascii>=0.1.5",
"telepath>=0.1.1,<1",
]

Wyświetl plik

@ -142,17 +142,6 @@ class CSV(TextFormat):
return super().create_dataset(in_stream, **kwargs)
class JSON(TextFormat):
TABLIB_MODULE = "tablib.formats._json"
CONTENT_TYPE = "application/json"
class YAML(TextFormat):
TABLIB_MODULE = "tablib.formats._yaml"
# See https://stackoverflow.com/questions/332129/yaml-mime-type
CONTENT_TYPE = "text/yaml"
class TSV(TextFormat):
TABLIB_MODULE = "tablib.formats._tsv"
CONTENT_TYPE = "text/tab-separated-values"
@ -161,36 +150,6 @@ class TSV(TextFormat):
return super().create_dataset(in_stream, **kwargs)
class ODS(TextFormat):
TABLIB_MODULE = "tablib.formats._ods"
CONTENT_TYPE = "application/vnd.oasis.opendocument.spreadsheet"
class HTML(TextFormat):
TABLIB_MODULE = "tablib.formats._html"
CONTENT_TYPE = "text/html"
class XLS(TablibFormat):
TABLIB_MODULE = "tablib.formats._xls"
CONTENT_TYPE = "application/vnd.ms-excel"
def create_dataset(self, in_stream):
"""
Create dataset from first sheet.
"""
import xlrd
xls_book = xlrd.open_workbook(file_contents=in_stream)
dataset = tablib.Dataset()
sheet = xls_book.sheets()[0]
dataset.headers = sheet.row_values(0)
for i in range(1, sheet.nrows):
dataset.append(sheet.row_values(i))
return dataset
class XLSX(TablibFormat):
TABLIB_MODULE = "tablib.formats._xlsx"
CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
@ -224,13 +183,8 @@ DEFAULT_FORMATS = [
fmt
for fmt in (
CSV,
XLS,
XLSX,
TSV,
ODS,
JSON,
YAML,
HTML,
)
if fmt.is_available()
]

Wyświetl plik

@ -12,7 +12,7 @@ from wagtail.models import Site
class Command(BaseCommand):
help = "Imports redirects from .csv, .xls, .xlsx"
help = "Imports redirects from a .csv, .tsv or .xlsx file"
def add_arguments(self, parser):
parser.add_argument(
@ -61,7 +61,7 @@ class Command(BaseCommand):
)
parser.add_argument(
"--format",
help="Source file format (example: .csv, .xls etc)",
help="Source file format (csv, tsv or xlsx)",
choices=get_supported_extensions(),
type=str,
)

Wyświetl plik

@ -1,14 +0,0 @@
[
{
"from": "/hello-from-json",
"to": "http://hello.com/random/"
},
{
"from": "/goodbye-from-json",
"to": "http://hello.com/goodbye/"
},
{
"from": "/goodbye-from-json",
"to": "/goodbye-not-working/"
}
]

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -209,36 +209,6 @@ class TestImportAdminViews(TestCase, WagtailTestUtils):
self.assertEqual(Redirect.objects.count(), 2)
self.assertEqual(Redirect.objects.first().site, new_site)
def test_import_xls(self):
f = "{}/files/example.xls".format(TEST_ROOT)
(_, filename) = os.path.split(f)
with open(f, "rb") as infile:
upload_file = SimpleUploadedFile(filename, infile.read())
self.assertEqual(Redirect.objects.all().count(), 0)
response = self.post(
{
"import_file": upload_file,
}
)
self.assertTemplateUsed(response, "wagtailredirects/confirm_import.html")
import_response = self.post_import(
{
**response.context["form"].initial,
"from_index": 0,
"to_index": 1,
"permanent": True,
},
follow=True,
)
self.assertTemplateUsed(import_response, "wagtailredirects/index.html")
self.assertEqual(Redirect.objects.all().count(), 3)
def test_import_xlsx(self):
f = "{}/files/example.xlsx".format(TEST_ROOT)
(_, filename) = os.path.split(f)

Wyświetl plik

@ -25,9 +25,9 @@ class TestImportCommand(TestCase):
call_command("import_redirects", src="random", stdout=out)
def test_invalid_extension_raises_error(self):
f = "{}/files/example.numbers".format(TEST_ROOT)
f = "{}/files/example.yaml".format(TEST_ROOT)
with self.assertRaisesMessage(Exception, "Invalid format 'numbers'"):
with self.assertRaisesMessage(Exception, "Invalid format 'yaml'"):
out = StringIO()
call_command("import_redirects", src=f, stdout=out)
@ -60,7 +60,7 @@ class TestImportCommand(TestCase):
self.assertEqual(Redirect.objects.count(), 2)
def test_binary_formats_are_supported(self):
f = "{}/files/example.xls".format(TEST_ROOT)
f = "{}/files/example.xlsx".format(TEST_ROOT)
out = StringIO()
call_command("import_redirects", src=f, stdout=out)

Wyświetl plik

@ -17,7 +17,7 @@ def write_to_file_storage(import_file, input_format):
def get_supported_extensions():
return ("csv", "tsv", "xls", "xlsx")
return "csv", "tsv", "xlsx"
def get_format_cls_by_extension(extension):