Allow customising the spreadsheet file name via SnippetViewSet.export_filename

pull/10626/head
Sage Abdullah 2023-07-10 10:20:04 +01:00
rodzic 428b7914e2
commit 0550b03dad
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: EB1A33CC51CC0217
6 zmienionych plików z 24 dodań i 2 usunięć

Wyświetl plik

@ -94,6 +94,7 @@ Viewsets are Wagtail's mechanism for defining a group of related admin views wit
.. autoattribute:: search_backend_name
.. autoattribute:: list_per_page
.. autoattribute:: chooser_per_page
.. autoattribute:: export_filename
.. autoattribute:: ordering
.. autoattribute:: admin_url_namespace
.. autoattribute:: base_url_path

Wyświetl plik

@ -93,7 +93,7 @@ You can add the ability to filter the listing view by defining a {attr}`~wagtail
If you would like to make further customisations to the filtering mechanism, you can also use a custom `wagtail.admin.filters.WagtailFilterSet` subclass by overriding the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.filterset_class` attribute. The `list_filter` attribute is ignored if `filterset_class` is set. For more details, refer to [django-filter's documentation](https://django-filter.readthedocs.io/en/stable/guide/usage.html#the-filter).
You can add the ability to export the listing view to a spreadsheet by setting the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.list_export` attribute to specify the columns to be exported.
You can add the ability to export the listing view to a spreadsheet by setting the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.list_export` attribute to specify the columns to be exported. The {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.export_filename` attribute can be used to customise the file name of the exported spreadsheet.
```{versionadded} 5.1
The ability to export the listing view was added.

Wyświetl plik

@ -154,6 +154,8 @@ class SpreadsheetExportMixin:
export_buttons_template_name = "wagtailadmin/shared/export_buttons.html"
export_filename = "spreadsheet-export"
def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
self.is_export = request.GET.get("export") in self.FORMATS
@ -165,7 +167,7 @@ class SpreadsheetExportMixin:
def get_filename(self):
"""Gets the base filename for the exported spreadsheet, without extensions"""
return "spreadsheet-export"
return self.export_filename
def to_row_dict(self, item):
"""Returns an OrderedDict (in the order given by list_export) of the exportable information for a model instance"""

Wyświetl plik

@ -707,6 +707,11 @@ class TestListExport(BaseSnippetViewSetTests):
response = self.client.get(self.get_url("list"), {"export": "csv"})
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get("Content-Disposition"),
'attachment; filename="all-fullfeatured-snippets.csv"',
)
data_lines = response.getvalue().decode().split("\n")
self.assertEqual(
data_lines[0],
@ -725,6 +730,11 @@ class TestListExport(BaseSnippetViewSetTests):
response = self.client.get(self.get_url("list"), {"export": "xlsx"})
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get("Content-Disposition"),
'attachment; filename="all-fullfeatured-snippets.xlsx"',
)
workbook_data = response.getvalue()
worksheet = load_workbook(filename=BytesIO(workbook_data)).active
cell_array = [[cell.value for cell in row] for row in worksheet.rows]

Wyświetl plik

@ -672,6 +672,9 @@ class SnippetViewSet(ModelViewSet):
#: A list or tuple, where each item is the name of a field, an attribute, or a single-argument callable on the model.
list_export = []
#: The base file name for the exported listing, without extensions. If unset, the model's :attr:`~django.db.models.Options.db_table` will be used instead.
export_filename = None
#: The number of items to display per page in the index view. Defaults to 20.
list_per_page = 20
@ -868,6 +871,7 @@ class SnippetViewSet(ModelViewSet):
list_display=self.list_display,
list_filter=self.list_filter,
list_export=self.list_export,
export_filename=self.get_export_filename(),
paginate_by=self.list_per_page,
default_ordering=self.ordering,
search_fields=self.search_fields,
@ -891,6 +895,7 @@ class SnippetViewSet(ModelViewSet):
list_display=self.list_display,
list_filter=self.list_filter,
list_export=self.list_export,
export_filename=self.get_export_filename(),
paginate_by=self.list_per_page,
default_ordering=self.ordering,
search_fields=self.search_fields,
@ -1238,6 +1243,9 @@ class SnippetViewSet(ModelViewSet):
"""
return None
def get_export_filename(self):
return self.export_filename or self.model_opts.db_table
def get_templates(self, action="index", fallback=""):
"""
Utility function that provides a list of templates to try for a given

Wyświetl plik

@ -264,6 +264,7 @@ class FullFeaturedSnippetViewSet(SnippetViewSet):
"some_date",
"first_published_at",
]
export_filename = "all-fullfeatured-snippets"
index_template_name = "tests/fullfeaturedsnippet_index.html"
ordering = ["text", "-_updated_at", "-pk"]
add_to_admin_menu = True