Skip to content

Commit

Permalink
show exported field list in admin UI (#1685)
Browse files Browse the repository at this point in the history
* show exported fields in admin

* shared template

* add test

* remove 2nd test, add template var

* updated changelog

---------

Co-authored-by: matthewhegarty <mrhegarty@gmail.com>
  • Loading branch information
tal66 and matthewhegarty committed Nov 17, 2023
1 parent f7decaa commit ebf58e0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
------------------

- Added `CONTRIBUTING.md`

- Show list of exported fields in Admin UI (#1685)

3.3.3 (2023-11-11)
------------------
Expand Down
10 changes: 10 additions & 0 deletions import_export/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,16 @@ def export_action(self, request, *args, **kwargs):
context["title"] = _("Export")
context["form"] = form
context["opts"] = self.model._meta
context["fields_list"] = [
(
res.get_display_name(),
[
field.column_name
for field in res(model=self.model).get_user_visible_fields()
],
)
for res in self.get_export_resource_classes()
]
request.current_app = self.admin_site.name
return TemplateResponse(request, [self.export_template_name], context)

Expand Down
2 changes: 2 additions & 0 deletions import_export/templates/admin/import_export/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<form action="" method="POST">
{% csrf_token %}

{% include "admin/import_export/resource_fields_list.html" with import_or_export="export" %}

<fieldset class="module aligned">
{% for field in form %}
<div class="form-row">
Expand Down
16 changes: 1 addition & 15 deletions import_export/templates/admin/import_export/import.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,7 @@
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}

{% block fields_help %}
<p>
{% trans "This importer will import the following fields: " %}
{% if fields_list|length <= 1 %}
<code>{{ fields_list.0.1|join:", " }}</code>
{% else %}
<dl>
{% for resource, fields in fields_list %}
<dt>{{ resource }}</dt>
<dd><code>{{ fields|join:", " }}</code></dd>
{% endfor %}
</dl>
{% endif %}
</p>
{% endblock %}
{% include "admin/import_export/resource_fields_list.html" with import_or_export="import" %}

{% block form_detail %}
<fieldset class="module aligned">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% load i18n %}
{% block fields_help %}
<p>
{% if import_or_export == "export" %}
{% trans "This exporter will export the following fields: " %}
{% elif import_or_export == "import" %}
{% trans "This importer will import the following fields: " %}
{% endif %}

{% if fields_list|length <= 1 %}
<code>{{ fields_list.0.1|join:", " }}</code>
{% else %}
<dl>
{% for resource, fields in fields_list %}
<dt>{{ resource }}</dt>
<dd><code>{{ fields|join:", " }}</code></dd>
{% endfor %}
</dl>
{% endif %}
</p>
{% endblock %}
25 changes: 25 additions & 0 deletions tests/core/tests/test_admin_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,31 @@ def test_import_admin_uses_import_format_settings(self):


class ExportAdminIntegrationTest(AdminTestMixin, TestCase):
def test_export_displays_resources_fields(self):
response = self.client.get("/admin/core/book/export/")
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.context["fields_list"],
[
(
"BookResource",
[
"id",
"name",
"author",
"author_email",
"imported",
"published",
"published_time",
"price",
"added",
"categories",
],
),
("Export/Import only book names", ["id", "name"]),
],
)

def test_export(self):
response = self.client.get("/admin/core/book/export/")
self.assertEqual(response.status_code, 200)
Expand Down

0 comments on commit ebf58e0

Please sign in to comment.