Skip to content

Commit

Permalink
Export typing
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Dec 21, 2024
1 parent eee0524 commit 0f7e5be
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
10 changes: 3 additions & 7 deletions django_tables2/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

if TYPE_CHECKING:
from django.db.models import QuerySet
from django.db.models.fields import Field
from django.utils.safestring import SafeString

from ..tables import Table
Expand All @@ -36,13 +37,8 @@ def register(self, column: "Column"):
self.columns.append(column)
return column

def column_for_field(self, field, **kwargs):
"""
Return a column object suitable for model field.
Returns:
`.Column` object or `None`
"""
def column_for_field(self, field: "Field", **kwargs) -> "Union[Column, None]":
"""Return a column object suitable for the supplied model field."""
if field is None:
return self.columns[0](**kwargs)

Expand Down
23 changes: 13 additions & 10 deletions django_tables2/export/export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from typing import TYPE_CHECKING, Union

from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse

if TYPE_CHECKING:
from ..tables import Table

try:
from tablib import Dataset
except ImportError: # pragma: no cover
Expand Down Expand Up @@ -51,7 +56,9 @@ def __init__(self, export_format, table, exclude_columns=None, dataset_kwargs=No
self.format = export_format
self.dataset = self.table_to_dataset(table, exclude_columns, dataset_kwargs)

def table_to_dataset(self, table, exclude_columns, dataset_kwargs=None):
def table_to_dataset(
self, table: "Table", exclude_columns: list[str], dataset_kwargs: "Union[dict, None]" = None
) -> Dataset:
"""Transform a table to a tablib dataset."""

def default_dataset_title():
Expand All @@ -71,25 +78,21 @@ def default_dataset_title():
return dataset

@classmethod
def is_valid_format(self, export_format):
def is_valid_format(self, export_format: str) -> bool:
"""
Returns true if `export_format` is one of the supported export formats
"""
return export_format is not None and export_format in TableExport.FORMATS.keys()

def content_type(self):
"""
Returns the content type for the current export format
"""
def content_type(self) -> str:
"""Return the content type for the current export format."""
return self.FORMATS[self.format]

def export(self):
"""
Returns the string/bytes for the current export format
"""
"""Return the string/bytes for the current export format."""
return self.dataset.export(self.format)

def response(self, filename=None):
def response(self, filename: Union[str, None] = None) -> HttpResponse:
"""
Builds and returns a `HttpResponse` containing the exported data
Expand Down
12 changes: 7 additions & 5 deletions django_tables2/export/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.http import HttpResponse

from .export import TableExport


Expand All @@ -24,7 +26,7 @@ class Table(tables.Table):
export_formats (iterable): export formats to render a set of buttons in the template.
dataset_kwargs (dictionary): passed as `**kwargs` to `tablib.Dataset` constructor::
dataset_kwargs = {"tite": "My custom tab title"}
dataset_kwargs = {"title": "My custom tab title"}
"""

export_class = TableExport
Expand All @@ -35,13 +37,13 @@ class Table(tables.Table):

export_formats = (TableExport.CSV,)

def get_export_filename(self, export_format):
def get_export_filename(self, export_format: str) -> str:
return f"{self.export_name}.{export_format}"

def get_dataset_kwargs(self):
def get_dataset_kwargs(self) -> dict:
return self.dataset_kwargs

def create_export(self, export_format):
def create_export(self, export_format: str) -> HttpResponse:
exporter = self.export_class(
export_format=export_format,
table=self.get_table(**self.get_table_kwargs()),
Expand All @@ -51,7 +53,7 @@ def create_export(self, export_format):

return exporter.response(filename=self.get_export_filename(export_format))

def render_to_response(self, context, **kwargs):
def render_to_response(self, context, **kwargs) -> HttpResponse:
export_format = self.request.GET.get(self.export_trigger_param, None)
if self.export_class.is_valid_format(export_format):
return self.create_export(export_format)
Expand Down

0 comments on commit 0f7e5be

Please sign in to comment.