diff --git a/README.md b/README.md index 2327f565e..2eac09c1f 100644 --- a/README.md +++ b/README.md @@ -839,11 +839,12 @@ Adding support for django-guardian is quote straightforward in Unfold, just add from unfold.admin import ModelAdmin from import_export.admin import ImportExportModelAdmin -from unfold.contrib.import_export.forms import ExportForm, ImportForm +from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm class ExampleAdmin(ModelAdmin, ImportExportModelAdmin): import_form_class = ImportForm export_form_class = ExportForm + # export_form_class = SelectableFieldsExportForm ``` When implementing `import_export.admin.ExportActionModelAdmin` class in admin panel, import_export plugin adds its own implementation of action form which is not incorporating Unfold CSS classes. For this reason, `unfold.contrib.import_export.admin` contains class with the same name `ExportActionModelAdmin` which inherits behavior of parent form and adds appropriate CSS classes. diff --git a/src/unfold/contrib/import_export/forms.py b/src/unfold/contrib/import_export/forms.py index c577ca326..b6ac9101f 100644 --- a/src/unfold/contrib/import_export/forms.py +++ b/src/unfold/contrib/import_export/forms.py @@ -1,6 +1,14 @@ +from django.forms.fields import BooleanField from import_export.forms import ExportForm as BaseExportForm from import_export.forms import ImportForm as BaseImportForm -from unfold.widgets import SELECT_CLASSES, UnfoldAdminFileFieldWidget +from import_export.forms import ( + SelectableFieldsExportForm as BaseSelectableFieldsExportForm, +) +from unfold.widgets import ( + SELECT_CLASSES, + UnfoldAdminFileFieldWidget, + UnfoldBooleanWidget, +) class ImportForm(BaseImportForm): @@ -17,3 +25,14 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["resource"].widget.attrs["class"] = " ".join(SELECT_CLASSES) self.fields["format"].widget.attrs["class"] = " ".join(SELECT_CLASSES) + + +class SelectableFieldsExportForm(BaseSelectableFieldsExportForm): + def __init__(self, formats, resources, **kwargs): + super().__init__(formats, resources, **kwargs) + self.fields["resource"].widget.attrs["class"] = " ".join(SELECT_CLASSES) + self.fields["format"].widget.attrs["class"] = " ".join(SELECT_CLASSES) + + for _key, field in self.fields.items(): + if isinstance(field, BooleanField): + field.widget = UnfoldBooleanWidget() diff --git a/src/unfold/contrib/import_export/templates/admin/import_export/export.html b/src/unfold/contrib/import_export/templates/admin/import_export/export.html index 6b5d77aa0..1aafd62c0 100644 --- a/src/unfold/contrib/import_export/templates/admin/import_export/export.html +++ b/src/unfold/contrib/import_export/templates/admin/import_export/export.html @@ -50,15 +50,29 @@ {% include "admin/import_export/resource_fields_list.html" with import_or_export="export" %} {% endif %} + {{ form.non_field_errors }} +
- {% if form.resource.field.widget.attrs.readonly %} - {% include "unfold/helpers/field_readonly.html" with title=form.resource.field.label value=form.resource.field.value %} - {{ form.resource.as_hidden }} - {% else %} - {% include "unfold/helpers/field.html" with field=form.resource %} - {% endif %} - - {% include "unfold/helpers/field.html" with field=form.format %} + {% for field in form.visible_fields %} +
+ {% if field.field.initial_field %} +

+ {% trans "This exporter will export the following fields" %} +

+ {% endif %} + + {% if field.field.widget.attrs.readonly %} + {% include "unfold/helpers/field_readonly.html" with title=field.label value=field.field.value %} + {{ field.as_hidden }} + {% else %} + {% include "unfold/helpers/field.html" with field=field %} + {% endif %} +
+ {% endfor %} + + {% for field in form.hidden_fields %} + {{ field }} + {% endfor %}
diff --git a/src/unfold/templates/unfold/helpers/field.html b/src/unfold/templates/unfold/helpers/field.html index ffc6cc72f..f6c58e76d 100644 --- a/src/unfold/templates/unfold/helpers/field.html +++ b/src/unfold/templates/unfold/helpers/field.html @@ -1,9 +1,23 @@ -
- {% include "unfold/helpers/form_label.html" with field=field %} +{% if field.field.widget.input_type == "checkbox" %} +
+
+ {{ field }} - {{ field }} + {% include "unfold/helpers/form_label.html" with field=field %} +
- {% include "unfold/helpers/form_errors.html" with errors=field.errors %} + {% include "unfold/helpers/form_errors.html" with errors=field.errors %} - {% include "unfold/helpers/help_text.html" with help_text=field.help_text %} -
+ {% include "unfold/helpers/help_text.html" with help_text=field.help_text %} +
+{% else %} +
+ {% include "unfold/helpers/form_label.html" with field=field %} + + {{ field }} + + {% include "unfold/helpers/form_errors.html" with errors=field.errors %} + + {% include "unfold/helpers/help_text.html" with help_text=field.help_text %} +
+{% endif %} diff --git a/src/unfold/templates/unfold/helpers/form_label.html b/src/unfold/templates/unfold/helpers/form_label.html index 1cc4893e6..f9092da5c 100644 --- a/src/unfold/templates/unfold/helpers/form_label.html +++ b/src/unfold/templates/unfold/helpers/form_label.html @@ -1,4 +1,4 @@ -