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

Add 'export from model page' #1687

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added docs/_static/images/change-form-export.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 20 additions & 1 deletion docs/advanced_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,26 @@ this to refer to your own model instances. In the example application, the 'Cat

When 'Go' is clicked for the selected items, the user will be directed to the
:ref:`export 'confirm' page<export_confirm>`. It is possible to disable this extra step by setting the
:ref:`import_export_skip_admin_action_export_ui` flag.
:ref:`import_export_skip_admin_action_export_ui` flag

Export from model instance change form
--------------------------------------

When :ref:`export via admin action<export_via_admin_action>` is enabled, then it is also possible to export from a
model instance change form:

.. figure:: _static/images/change-form-export.png
:alt: export from change form

Export from model instance change form

When 'Export' is clicked, the user will be directed to the :ref:`export 'confirm' page<export_confirm>`.

This button can be removed from the UI by setting the
:attr:`~import_export.admin.ExportActionMixin.show_change_form_export` attribute, for example::

class CategoryAdmin(ExportActionModelAdmin):
show_change_form_export = False

Customize admin import forms
----------------------------
Expand Down
7 changes: 6 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Changelog

Please refer to :doc:`release notes<release_notes>`.

4.0.0-beta.2 (unreleased)
--------------------------

- Updated `docker-compose` command with latest version syntax in `runtests.sh` (#1686)
- Support export from model change form (#1687)

4.0.0-beta.1 (2023-11-16)
--------------------------

Expand Down Expand Up @@ -36,7 +42,6 @@ Enhancements
Fixes
#####

- Updated `docker-compose` command with latest version syntax in `runtests.sh` (#1686)
- dynamic widget parameters for CharField fixes 'NOT NULL constraint' error in xlsx (#1485)
- fix cooperation with adminsortable2 (#1633)
- Removed unused method ``utils.original()``
Expand Down
33 changes: 32 additions & 1 deletion import_export/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,10 +778,33 @@ class ExportActionMixin(ExportMixin):
Mixin with export functionality implemented as an admin action.
"""

#: template for change form
change_form_template = "admin/import_export/change_form.html"

#: Flag to indicate whether to show 'export' button on change form
show_change_form_export = True

# This action will receive a selection of items as a queryset,
# store them in the context, and then render the 'export' admin form page,
# so that users can select file format and resource

def change_view(self, request, object_id, form_url="", extra_context=None):
extra_context = extra_context or {}
extra_context["show_change_form_export"] = self.show_change_form_export
return super().change_view(
request,
object_id,
form_url,
extra_context=extra_context,
)

def response_change(self, request, obj):
if "_export-item" in request.POST:
return self.export_admin_action(
request, self.model.objects.filter(id=obj.id)
)
return super().response_change(request, obj)

def export_admin_action(self, request, queryset):
"""
Action runs on POST from instance action menu (if enabled).
Expand Down Expand Up @@ -820,7 +843,15 @@ def export_admin_action(self, request, queryset):

# this is necessary to render the FORM action correctly
# i.e. so the POST goes to the correct URL
context["export_suffix"] = "export/"
export_url = reverse(
"%s:%s_%s_export"
% (
self.admin_site.name,
self.model._meta.app_label,
self.model._meta.model_name,
)
)
context["export_url"] = export_url

return render(request, "admin/import_export/export.html", context=context)

Expand Down
2 changes: 1 addition & 1 deletion import_export/templates/admin/import_export/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% if not is_popup %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
<a href="{% url 'admin:index' %}">{% translate 'Home' %}</a>
&rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
&rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>
&rsaquo; {% block breadcrumbs_last %}{% endblock %}
Expand Down
11 changes: 11 additions & 0 deletions import_export/templates/admin/import_export/change_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'admin/change_form.html' %}
{% load i18n %}

{% block submit_buttons_bottom %}
{{ block.super }}
{% if show_change_form_export %}
<div class="submit-row">
<input type="submit" value="{% translate 'Export' %}" class="default" name="_export-item">
</div>
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
{% load admin_urls %}

{% if has_export_permission %}
<li><a href="{% url opts|admin_urlname:'export' %}{{cl.get_query_string}}" class="export_link">{% trans "Export" %}</a></li>
<li><a href="{% url opts|admin_urlname:'export' %}{{cl.get_query_string}}" class="export_link">{% translate "Export" %}</a></li>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
{% load admin_urls %}

{% if has_import_permission %}
<li><a href='{% url opts|admin_urlname:"import" %}' class="import_link">{% trans "Import" %}</a></li>
<li><a href='{% url opts|admin_urlname:"import" %}' class="import_link">{% translate "Import" %}</a></li>
{% endif %}
6 changes: 3 additions & 3 deletions import_export/templates/admin/import_export/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
{% endblock %}

{% block breadcrumbs_last %}
{% trans "Export" %}
{% translate "Export" %}
{% endblock %}

{% block content %}
<form action="{{ request.path }}{{ export_suffix }}" method="POST">
<form action="{{ export_url }}" method="POST">
{% csrf_token %}
{# export request has originated from an Admin UI action #}
{% if form.initial.export_items %}
Expand Down Expand Up @@ -53,7 +53,7 @@
</fieldset>

<div class="submit-row">
<input type="submit" class="default" value="{% trans "Submit" %}">
<input type="submit" class="default" value="{% translate "Submit" %}">
</div>
</form>
{% endblock %}
32 changes: 16 additions & 16 deletions import_export/templates/admin/import_export/import.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% endblock %}

{% block breadcrumbs_last %}
{% trans "Import" %}
{% translate "Import" %}
{% endblock %}

{% block content %}
Expand All @@ -27,10 +27,10 @@
{% csrf_token %}
{{ confirm_form.as_p }}
<p>
{% trans "Below is a preview of data to be imported. If you are satisfied with the results, click 'Confirm import'" %}
{% translate "Below is a preview of data to be imported. If you are satisfied with the results, click 'Confirm import'" %}
</p>
<div class="submit-row">
<input type="submit" class="default" name="confirm" value="{% trans "Confirm import" %}">
<input type="submit" class="default" name="confirm" value="{% translate "Confirm import" %}">
</div>
</form>
{% endblock %}
Expand Down Expand Up @@ -61,7 +61,7 @@

{% block form_submit_button %}
<div class="submit-row">
<input type="submit" class="default" value="{% trans "Submit" %}">
<input type="submit" class="default" value="{% translate "Submit" %}">
</div>
{% endblock %}
</form>
Expand All @@ -72,7 +72,7 @@

{% if result.has_errors %}
{% block errors %}
<h2>{% trans "Errors" %}</h2>
<h2>{% translate "Errors" %}</h2>
<ul>
{% for error in result.base_errors %}
<li>
Expand All @@ -83,7 +83,7 @@ <h2>{% trans "Errors" %}</h2>
{% for line, errors in result.row_errors %}
{% for error in errors %}
<li>
{% trans "Line number" %}: {{ line }} - {{ error.error }}
{% translate "Line number" %}: {{ line }} - {{ error.error }}
<div><code>{{ error.row.values|join:", " }}</code></div>
<div class="traceback">{{ error.traceback|linebreaks }}</div>
</li>
Expand All @@ -95,15 +95,15 @@ <h2>{% trans "Errors" %}</h2>
{% elif result.has_validation_errors %}

{% block validation_errors %}
<h2>{% trans "Some rows failed to validate" %}</h2>
<h2>{% translate "Some rows failed to validate" %}</h2>

<p>{% trans "Please correct these errors in your data where possible, then reupload it using the form above." %}</p>
<p>{% translate "Please correct these errors in your data where possible, then reupload it using the form above." %}</p>

<table class="import-preview">
<thead>
<tr>
<th>{% trans "Row" %}</th>
<th>{% trans "Errors" %}</th>
<th>{% translate "Row" %}</th>
<th>{% translate "Errors" %}</th>
{% for field in result.diff_headers %}
<th>{{ field }}</th>
{% endfor %}
Expand All @@ -129,7 +129,7 @@ <h2>{% trans "Some rows failed to validate" %}</h2>
{% endfor %}
{% if row.non_field_specific_errors %}
<li>
<span class="validation-error-field-label">{% trans "Non field specific" %}</span>
<span class="validation-error-field-label">{% translate "Non field specific" %}</span>
<ul>
{% for error in row.non_field_specific_errors %}
<li>{{ error }}</li>
Expand All @@ -152,7 +152,7 @@ <h2>{% trans "Some rows failed to validate" %}</h2>
{% else %}

{% block preview %}
<h2>{% trans "Preview" %}</h2>
<h2>{% translate "Preview" %}</h2>

<table class="import-preview">
<thead>
Expand All @@ -167,13 +167,13 @@ <h2>{% trans "Preview" %}</h2>
<tr class="{{ row.import_type }}">
<td class="import-type">
{% if row.import_type == 'new' %}
{% trans "New" %}
{% translate "New" %}
{% elif row.import_type == 'skip' %}
{% trans "Skipped" %}
{% translate "Skipped" %}
{% elif row.import_type == 'delete' %}
{% trans "Delete" %}
{% translate "Delete" %}
{% elif row.import_type == 'update' %}
{% trans "Update" %}
{% translate "Update" %}
{% endif %}
</td>
{% for field in row.diff %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{% block fields_help %}
<p>
{% if import_or_export == "export" %}
{% trans "This exporter will export the following fields: " %}
{% translate "This exporter will export the following fields: " %}
{% elif import_or_export == "import" %}
{% trans "This importer will import the following fields: " %}
{% translate "This importer will import the following fields: " %}
{% endif %}

{% if fields_list|length <= 1 %}
Expand Down
3 changes: 1 addition & 2 deletions tests/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class BookAdmin(ImportExportModelAdmin):


class CategoryAdmin(ExportActionModelAdmin):
def export_admin_action(self, request, queryset):
return super().export_admin_action(request, queryset)
pass


class AuthorAdmin(ImportMixin, admin.ModelAdmin):
Expand Down
61 changes: 51 additions & 10 deletions tests/core/tests/admin_integration/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
from unittest import mock
from unittest.mock import MagicMock

from core.admin import CategoryAdmin
from core.models import Book, Category
from core.tests.admin_integration.mixins import AdminTestMixin
from core.tests.utils import ignore_widget_deprecation_warning
from django.contrib import admin
from django.core.exceptions import PermissionDenied
from django.http import HttpRequest
from django.test import RequestFactory
from django.test.testcases import TestCase
from django.test.utils import override_settings
from django.urls import reverse

from import_export.admin import ExportMixin, ImportExportActionModelAdmin
from import_export.tmp_storages import TempFolderStorage
from import_export.admin import ExportMixin


class ExportActionAdminIntegrationTest(AdminTestMixin, TestCase):
Expand Down Expand Up @@ -120,13 +123,51 @@ def has_export_permission(self, request):
m.get_export_data("0", Book.objects.none(), request=request)


class TestImportExportActionModelAdmin(ImportExportActionModelAdmin):
def __init__(self, mock_model, mock_site, error_instance):
self.error_instance = error_instance
super().__init__(mock_model, mock_site)
class TestExportButtonOnChangeForm(AdminTestMixin, TestCase):
def setUp(self):
super().setUp()
self.cat1 = Category.objects.create(name="Cat 1")
self.change_url = reverse(
"%s:%s_%s_change"
% (
"admin",
"core",
"category",
),
args=[self.cat1.id],
)
self.target_str = (
'<input type="submit" value="Export" '
'class="default" name="_export-item">'
)

def test_export_button_on_change_form(self):
response = self.client.get(self.change_url)
self.assertIn(
self.target_str,
str(response.content),
)
response = self.client.post(
self.change_url, data={"_export-item": "Export", "name": self.cat1.name}
)
self.assertIn("Export 1 selected item", str(response.content))

def test_export_button_on_change_form_disabled(self):
class MockCategoryAdmin(CategoryAdmin):
show_change_form_export = True

factory = RequestFactory()
category_admin = MockCategoryAdmin(Category, admin.site)

request = factory.get(self.change_url)
request.user = self.user

response = category_admin.change_view(request, str(self.cat1.id))
response.render()

def write_to_tmp_storage(self, import_file, input_format):
mock_storage = MagicMock(spec=TempFolderStorage)
self.assertIn(self.target_str, str(response.content))

mock_storage.read.side_effect = self.error_instance
return mock_storage
category_admin.show_change_form_export = False
response = category_admin.change_view(request, str(self.cat1.id))
response.render()
self.assertNotIn(self.target_str, str(response.content))