Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export feature for list views #1559

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
b1rger marked this conversation as resolved.
Show resolved Hide resolved
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
22 changes: 21 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ crispy-bootstrap5 = ">=2024.10"
django-simple-history = ">=3.6"
pydot = "^3.0.2"
acdh-arche-assets = "^3.21.1"
tablib = "^3.8.0"

[tool.poetry.group.docs]
optional = true
Expand Down Expand Up @@ -74,7 +75,7 @@ extend_exclude = [".*/tests"]

[tool.deptry.per_rule_ignores]
DEP001 = ["apis_ontology"]
DEP002 = ["apis-override-select2js", "crispy-bootstrap4", "crispy-bootstrap5"]
DEP002 = ["apis-override-select2js", "crispy-bootstrap4", "crispy-bootstrap5", "tablib"]

[tool.deptry.package_module_name_map]
djangorestframework = "rest_framework"
Expand Down