export.py 924 B

1234567891011121314151617181920212223242526
  1. from django.utils.translation import gettext_lazy as _
  2. from django_tables2.export import TableExport as TableExport_
  3. from utilities.constants import CSV_DELIMITERS
  4. __all__ = (
  5. 'TableExport',
  6. )
  7. class TableExport(TableExport_):
  8. """
  9. A subclass of django-tables2's TableExport class which allows us to specify a delimiting
  10. characters for CSV exports.
  11. """
  12. def __init__(self, *args, delimiter=None, **kwargs):
  13. if delimiter and delimiter not in CSV_DELIMITERS.keys():
  14. raise ValueError(_("Invalid delimiter name: {name}").format(name=delimiter))
  15. self.delimiter = delimiter or 'comma'
  16. super().__init__(*args, **kwargs)
  17. def export(self):
  18. if self.format == self.CSV and self.delimiter is not None:
  19. delimiter = CSV_DELIMITERS[self.delimiter]
  20. return self.dataset.export(self.format, delimiter=delimiter)
  21. return super().export()