Skip to content

Commit

Permalink
feat(generic): add export support to list views
Browse files Browse the repository at this point in the history
  • Loading branch information
gythaogg committed Jan 29, 2025
1 parent 21c2192 commit 6f1a8c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion apis_core/generic/templates/generic/generic_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load crispy_forms_tags %}
{% load generic %}
{% load core %}
{% load export_url from django_tables2 %}
{% if filter %}

{% block col %}
Expand Down Expand Up @@ -39,7 +40,16 @@
{% block additionalcols %}
<div class="col-8">
<div class="card">
<div class="card-header">{{ table.paginator.count }} results</div>
<div class="card-header">
{{ table.paginator.count }} results.
{% if view.export_formats %}
Download
{% for format in view.export_formats %}
<a href="{% export_url format %}">{{ format | upper }}</a>
{% if not forloop.last %}|{% endif %}
{% endfor %}
{% endif %}
</div>
<div class="card-body">

{% block table %}
Expand Down
11 changes: 11 additions & 0 deletions apis_core/generic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django_filters.views import FilterView
from django_tables2 import SingleTableMixin
from django_tables2.columns import library
from django_tables2.export.views import ExportMixin
from django_tables2.tables import table_factory

from apis_core.apis_metainfo.models import Uri
Expand Down Expand Up @@ -100,6 +101,7 @@ def get_permission_required(self):
class List(
GenericModelMixin,
PermissionRequiredMixin,
ExportMixin,
SingleTableMixin,
FilterView,
):
Expand All @@ -123,6 +125,15 @@ def get_table_class(self):
table_class = first_member_match(table_modules, GenericTable)
return table_factory(self.model, table_class)

export_formats = getattr(settings, "EXPORT_FORMATS", ["csv", "json"])

def get_export_filename(self, extension):
table_class = self.get_table_class()
if hasattr(table_class, "export_filename"):
return f"{table_class.export_filename}.{extension}"

return super().get_export_filename(extension)

def get_table_kwargs(self):
kwargs = super().get_table_kwargs()

Expand Down

0 comments on commit 6f1a8c3

Please sign in to comment.