Abdelrahman Hamada 2024-04-27 12:39:01 +00:00 zatwierdzone przez GitHub
commit 9ce0e25989
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 47 dodań i 6 usunięć

Wyświetl plik

@ -6,6 +6,7 @@ from io import BytesIO
from django.contrib.admin.utils import label_for_field from django.contrib.admin.utils import label_for_field
from django.core.exceptions import FieldDoesNotExist from django.core.exceptions import FieldDoesNotExist
from django.db.models.constants import LOOKUP_SEP
from django.http import FileResponse, StreamingHttpResponse from django.http import FileResponse, StreamingHttpResponse
from django.utils import timezone from django.utils import timezone
from django.utils.dateformat import Formatter from django.utils.dateformat import Formatter
@ -17,6 +18,7 @@ from django.utils.translation import gettext as _
from openpyxl import Workbook from openpyxl import Workbook
from openpyxl.cell import WriteOnlyCell from openpyxl.cell import WriteOnlyCell
from wagtail.admin.ui.tables import Column
from wagtail.admin.widgets.button import Button from wagtail.admin.widgets.button import Button
from wagtail.coreutils import multigetattr from wagtail.coreutils import multigetattr
@ -177,9 +179,19 @@ class SpreadsheetExportMixin:
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"""
row_dict = OrderedDict( row_dict = OrderedDict()
(field, multigetattr(item, field)) for field in self.list_export for field in self.list_export:
) if isinstance(field, Column):
row_dict[field] = field.get_value(item)
else:
original_field = field
if "." not in field and not hasattr(item, field):
# Then maybe it is a related field with LOOKUP_SEP, checking
# if LOOKUP_SEP exists in field and working with that will fail
# in some fields like "workflow_state.content_object.__str__"
field = field.replace(LOOKUP_SEP, ".")
row_dict[original_field] = multigetattr(item, field)
return row_dict return row_dict
def get_preprocess_function(self, field, value, export_format): def get_preprocess_function(self, field, value, export_format):
@ -187,6 +199,11 @@ class SpreadsheetExportMixin:
# Try to find a field specific function and return it # Try to find a field specific function and return it
format_dict = self.custom_field_preprocess.get(field, {}) format_dict = self.custom_field_preprocess.get(field, {})
# Column classes can be referred by their name in custom_field_preprocess
if isinstance(field, Column):
format_dict = self.export_headings.get(field.name, format_dict)
if export_format in format_dict: if export_format in format_dict:
return format_dict[export_format] return format_dict[export_format]
@ -227,12 +244,36 @@ class SpreadsheetExportMixin:
def get_heading(self, queryset, field): def get_heading(self, queryset, field):
"""Get the heading label for a given field for a spreadsheet generated from queryset""" """Get the heading label for a given field for a spreadsheet generated from queryset"""
heading_override = self.export_headings.get(field) heading_override = self.export_headings.get(field)
if isinstance(field, Column):
heading_override = self.export_headings.get(field.name, heading_override)
if heading_override: if heading_override:
return force_str(heading_override) return force_str(heading_override)
try: try:
return capfirst(force_str(label_for_field(field, queryset.model))) return field.label
except (AttributeError, FieldDoesNotExist): except AttributeError:
return force_str(field) try:
return capfirst(force_str(label_for_field(field, queryset.model)))
except (AttributeError, FieldDoesNotExist):
seperator = LOOKUP_SEP if "." not in field else "."
*relation, field = field.split(seperator)
try:
model_class = queryset.model
except AttributeError:
return force_str(field)
for model in relation:
foreign_field = model_class._meta.get_field(model)
model_class = foreign_field.related_model
label = label_for_field(field, model_class)
if foreign_field:
label = _("%(related_model_name)s %(field_label)s") % {
"related_model_name": foreign_field.verbose_name,
"field_label": label,
}
return capfirst(label)
def stream_csv(self, queryset): def stream_csv(self, queryset):
"""Generate a csv file line by line from queryset, to be used in a StreamingHTTPResponse""" """Generate a csv file line by line from queryset, to be used in a StreamingHTTPResponse"""