kopia lustrzana https://github.com/wagtail/wagtail
Allow customising the spreadsheet file name via SnippetViewSet.export_filename
rodzic
428b7914e2
commit
0550b03dad
|
@ -94,6 +94,7 @@ Viewsets are Wagtail's mechanism for defining a group of related admin views wit
|
||||||
.. autoattribute:: search_backend_name
|
.. autoattribute:: search_backend_name
|
||||||
.. autoattribute:: list_per_page
|
.. autoattribute:: list_per_page
|
||||||
.. autoattribute:: chooser_per_page
|
.. autoattribute:: chooser_per_page
|
||||||
|
.. autoattribute:: export_filename
|
||||||
.. autoattribute:: ordering
|
.. autoattribute:: ordering
|
||||||
.. autoattribute:: admin_url_namespace
|
.. autoattribute:: admin_url_namespace
|
||||||
.. autoattribute:: base_url_path
|
.. autoattribute:: base_url_path
|
||||||
|
|
|
@ -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).
|
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
|
```{versionadded} 5.1
|
||||||
The ability to export the listing view was added.
|
The ability to export the listing view was added.
|
||||||
|
|
|
@ -154,6 +154,8 @@ class SpreadsheetExportMixin:
|
||||||
|
|
||||||
export_buttons_template_name = "wagtailadmin/shared/export_buttons.html"
|
export_buttons_template_name = "wagtailadmin/shared/export_buttons.html"
|
||||||
|
|
||||||
|
export_filename = "spreadsheet-export"
|
||||||
|
|
||||||
def setup(self, request, *args, **kwargs):
|
def setup(self, request, *args, **kwargs):
|
||||||
super().setup(request, *args, **kwargs)
|
super().setup(request, *args, **kwargs)
|
||||||
self.is_export = request.GET.get("export") in self.FORMATS
|
self.is_export = request.GET.get("export") in self.FORMATS
|
||||||
|
@ -165,7 +167,7 @@ class SpreadsheetExportMixin:
|
||||||
|
|
||||||
def get_filename(self):
|
def get_filename(self):
|
||||||
"""Gets the base filename for the exported spreadsheet, without extensions"""
|
"""Gets the base filename for the exported spreadsheet, without extensions"""
|
||||||
return "spreadsheet-export"
|
return self.export_filename
|
||||||
|
|
||||||
def to_row_dict(self, item):
|
def to_row_dict(self, item):
|
||||||
"""Returns an OrderedDict (in the order given by list_export) of the exportable information for a model instance"""
|
"""Returns an OrderedDict (in the order given by list_export) of the exportable information for a model instance"""
|
||||||
|
|
|
@ -707,6 +707,11 @@ class TestListExport(BaseSnippetViewSetTests):
|
||||||
response = self.client.get(self.get_url("list"), {"export": "csv"})
|
response = self.client.get(self.get_url("list"), {"export": "csv"})
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
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")
|
data_lines = response.getvalue().decode().split("\n")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
data_lines[0],
|
data_lines[0],
|
||||||
|
@ -725,6 +730,11 @@ class TestListExport(BaseSnippetViewSetTests):
|
||||||
response = self.client.get(self.get_url("list"), {"export": "xlsx"})
|
response = self.client.get(self.get_url("list"), {"export": "xlsx"})
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(
|
||||||
|
response.get("Content-Disposition"),
|
||||||
|
'attachment; filename="all-fullfeatured-snippets.xlsx"',
|
||||||
|
)
|
||||||
|
|
||||||
workbook_data = response.getvalue()
|
workbook_data = response.getvalue()
|
||||||
worksheet = load_workbook(filename=BytesIO(workbook_data)).active
|
worksheet = load_workbook(filename=BytesIO(workbook_data)).active
|
||||||
cell_array = [[cell.value for cell in row] for row in worksheet.rows]
|
cell_array = [[cell.value for cell in row] for row in worksheet.rows]
|
||||||
|
|
|
@ -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.
|
#: 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 = []
|
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.
|
#: The number of items to display per page in the index view. Defaults to 20.
|
||||||
list_per_page = 20
|
list_per_page = 20
|
||||||
|
|
||||||
|
@ -868,6 +871,7 @@ class SnippetViewSet(ModelViewSet):
|
||||||
list_display=self.list_display,
|
list_display=self.list_display,
|
||||||
list_filter=self.list_filter,
|
list_filter=self.list_filter,
|
||||||
list_export=self.list_export,
|
list_export=self.list_export,
|
||||||
|
export_filename=self.get_export_filename(),
|
||||||
paginate_by=self.list_per_page,
|
paginate_by=self.list_per_page,
|
||||||
default_ordering=self.ordering,
|
default_ordering=self.ordering,
|
||||||
search_fields=self.search_fields,
|
search_fields=self.search_fields,
|
||||||
|
@ -891,6 +895,7 @@ class SnippetViewSet(ModelViewSet):
|
||||||
list_display=self.list_display,
|
list_display=self.list_display,
|
||||||
list_filter=self.list_filter,
|
list_filter=self.list_filter,
|
||||||
list_export=self.list_export,
|
list_export=self.list_export,
|
||||||
|
export_filename=self.get_export_filename(),
|
||||||
paginate_by=self.list_per_page,
|
paginate_by=self.list_per_page,
|
||||||
default_ordering=self.ordering,
|
default_ordering=self.ordering,
|
||||||
search_fields=self.search_fields,
|
search_fields=self.search_fields,
|
||||||
|
@ -1238,6 +1243,9 @@ class SnippetViewSet(ModelViewSet):
|
||||||
"""
|
"""
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_export_filename(self):
|
||||||
|
return self.export_filename or self.model_opts.db_table
|
||||||
|
|
||||||
def get_templates(self, action="index", fallback=""):
|
def get_templates(self, action="index", fallback=""):
|
||||||
"""
|
"""
|
||||||
Utility function that provides a list of templates to try for a given
|
Utility function that provides a list of templates to try for a given
|
||||||
|
|
|
@ -264,6 +264,7 @@ class FullFeaturedSnippetViewSet(SnippetViewSet):
|
||||||
"some_date",
|
"some_date",
|
||||||
"first_published_at",
|
"first_published_at",
|
||||||
]
|
]
|
||||||
|
export_filename = "all-fullfeatured-snippets"
|
||||||
index_template_name = "tests/fullfeaturedsnippet_index.html"
|
index_template_name = "tests/fullfeaturedsnippet_index.html"
|
||||||
ordering = ["text", "-_updated_at", "-pk"]
|
ordering = ["text", "-_updated_at", "-pk"]
|
||||||
add_to_admin_menu = True
|
add_to_admin_menu = True
|
||||||
|
|
Ładowanie…
Reference in New Issue