Add export_filename option to modeladmin

Merge pull request #6031 from wagtail/reports-filename
pull/6039/head
Kalob Taulien 2020-05-15 10:04:57 -06:00 zatwierdzone przez GitHub
commit b5f5ac10db
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 28 dodań i 2 usunięć

Wyświetl plik

@ -293,6 +293,21 @@ for your model. For example:
list_filter = ('is_staff', 'company')
.. _modeladmin_export_filename:
------------------------------
``ModelAdmin.export_filename``
------------------------------
**Expected value**: A string specifying the filename of an exported spreadsheet,
without file extensions.
.. code-block:: python
class PersonAdmin(ModelAdmin):
export_filename = 'people_spreadsheet'
.. _modeladmin_search_fields:
----------------------------

Wyświetl plik

@ -48,8 +48,11 @@ class TestBookIndexView(TestCase, WagtailTestUtils):
# Export the whole queryset
response = self.get(export='csv')
# Check response - all books should be in it
self.assertEqual(response.status_code, 200)
# Check attachment is present and named correctly using the modeladmin export_filename
self.assertEqual(response.get('content-disposition'), 'attachment; filename="books-export.csv"')
# Check response - all books should be in it
data_lines = response.getvalue().decode().split("\n")
self.assertEqual(data_lines[0], 'Title,Author\r')
self.assertEqual(data_lines[1], 'Charlie and the Chocolate Factory,Roald Dahl\r')
@ -61,8 +64,11 @@ class TestBookIndexView(TestCase, WagtailTestUtils):
# Export the whole queryset
response = self.get(export='xlsx')
# Check response - all books should be in it
self.assertEqual(response.status_code, 200)
# Check attachment is present and named correctly using the modeladmin export_filename
self.assertEqual(response.get('content-disposition'), 'attachment; filename="books-export.xlsx"')
# Check response - all books should be in it
workbook_data = response.getvalue()
worksheet = load_workbook(filename=BytesIO(workbook_data))['Sheet1']
cell_array = [[cell.value for cell in row] for row in worksheet.rows]

Wyświetl plik

@ -265,6 +265,10 @@ class IndexView(SpreadsheetExportMixin, WMABaseView):
return super().dispatch(request, *args, **kwargs)
def get_filename(self):
""" Get filename for exported spreadsheet, without extension """
return getattr(self.model_admin, 'export_filename', super().get_filename())
def get_heading(self, queryset, field):
""" Get headings for exported spreadsheet column for the relevant field """
heading_override = self.export_headings.get(field)

Wyświetl plik

@ -48,6 +48,7 @@ class BookModelAdmin(ThumbnailMixin, ModelAdmin):
list_display = ('title', 'author', 'admin_thumb')
list_export = ('title', 'author')
list_filter = ('author', )
export_filename = "books-export"
ordering = ('title', )
inspect_view_enabled = True
inspect_view_fields_exclude = ('title', )