Skip to content

Commit a55523e

Browse files
authored
fix: import export selectable fields (#431)
1 parent 7cc95fe commit a55523e

File tree

7 files changed

+76
-32
lines changed

7 files changed

+76
-32
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,12 @@ Adding support for django-guardian is quote straightforward in Unfold, just add
839839

840840
from unfold.admin import ModelAdmin
841841
from import_export.admin import ImportExportModelAdmin
842-
from unfold.contrib.import_export.forms import ExportForm, ImportForm
842+
from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm
843843

844844
class ExampleAdmin(ModelAdmin, ImportExportModelAdmin):
845845
import_form_class = ImportForm
846846
export_form_class = ExportForm
847+
# export_form_class = SelectableFieldsExportForm
847848
```
848849

849850
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.

src/unfold/contrib/import_export/forms.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
from django.forms.fields import BooleanField
12
from import_export.forms import ExportForm as BaseExportForm
23
from import_export.forms import ImportForm as BaseImportForm
3-
from unfold.widgets import SELECT_CLASSES, UnfoldAdminFileFieldWidget
4+
from import_export.forms import (
5+
SelectableFieldsExportForm as BaseSelectableFieldsExportForm,
6+
)
7+
from unfold.widgets import (
8+
SELECT_CLASSES,
9+
UnfoldAdminFileFieldWidget,
10+
UnfoldBooleanWidget,
11+
)
412

513

614
class ImportForm(BaseImportForm):
@@ -17,3 +25,14 @@ def __init__(self, *args, **kwargs):
1725
super().__init__(*args, **kwargs)
1826
self.fields["resource"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
1927
self.fields["format"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
28+
29+
30+
class SelectableFieldsExportForm(BaseSelectableFieldsExportForm):
31+
def __init__(self, formats, resources, **kwargs):
32+
super().__init__(formats, resources, **kwargs)
33+
self.fields["resource"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
34+
self.fields["format"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
35+
36+
for _key, field in self.fields.items():
37+
if isinstance(field, BooleanField):
38+
field.widget = UnfoldBooleanWidget()

src/unfold/contrib/import_export/templates/admin/import_export/export.html

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,29 @@
5050
{% include "admin/import_export/resource_fields_list.html" with import_or_export="export" %}
5151
{% endif %}
5252

53+
{{ form.non_field_errors }}
54+
5355
<fieldset class="border border-gray-200 mb-4 rounded-md pt-3 px-3 shadow-sm dark:border-gray-800">
54-
{% if form.resource.field.widget.attrs.readonly %}
55-
{% include "unfold/helpers/field_readonly.html" with title=form.resource.field.label value=form.resource.field.value %}
56-
{{ form.resource.as_hidden }}
57-
{% else %}
58-
{% include "unfold/helpers/field.html" with field=form.resource %}
59-
{% endif %}
60-
61-
{% include "unfold/helpers/field.html" with field=form.format %}
56+
{% for field in form.visible_fields %}
57+
<div {% if field.field.is_selectable_field %}class="selectable-field-export-row" resource-index="{{ field.field.resource_index }}"{% else %}class="form-row aligned"{% endif %}>
58+
{% if field.field.initial_field %}
59+
<p class="block font-medium mb-2 text-gray-900 text-sm dark:text-gray-200">
60+
{% trans "This exporter will export the following fields" %}
61+
</p>
62+
{% endif %}
63+
64+
{% if field.field.widget.attrs.readonly %}
65+
{% include "unfold/helpers/field_readonly.html" with title=field.label value=field.field.value %}
66+
{{ field.as_hidden }}
67+
{% else %}
68+
{% include "unfold/helpers/field.html" with field=field %}
69+
{% endif %}
70+
</div>
71+
{% endfor %}
72+
73+
{% for field in form.hidden_fields %}
74+
{{ field }}
75+
{% endfor %}
6276
</fieldset>
6377

6478
<button type="submit" class="bg-primary-600 border border-transparent font-medium px-3 py-2 rounded-md text-sm text-white">

src/unfold/contrib/import_export/templates/admin/import_export/import_form.html

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@
77
{% include "admin/import_export/resource_fields_list.html" with import_or_export="import" %}
88

99
<fieldset class="border border-gray-200 mb-8 rounded-md pt-3 px-3 shadow-sm dark:border-gray-800">
10-
{% if form.resource.field.widget.attrs.readonly %}
11-
{% include "unfold/helpers/field_readonly.html" with title=form.resource.field.label value=form.resource.field.value %}
12-
{{ form.resource.as_hidden }}
13-
{% else %}
14-
{% include "unfold/helpers/field.html" with field=form.resource %}
15-
{% endif %}
16-
17-
18-
{% include "unfold/helpers/field.html" with field=form.import_file %}
19-
20-
{% include "unfold/helpers/field.html" with field=form.format %}
10+
{% for field in form %}
11+
{% if field.field.widget.attrs.readonly %}
12+
{% include "unfold/helpers/field_readonly.html" with title=field.label value=field.field.value %}
13+
{{ field.as_hidden }}
14+
{% else %}
15+
{% include "unfold/helpers/field.html" with field=field %}
16+
{% endif %}
17+
{% endfor %}
2118
</fieldset>
2219

23-
2420
<button type="submit" class="bg-primary-600 border border-transparent font-medium px-3 py-2 rounded-md text-sm text-white">
2521
{% translate 'Submit' %}
2622
</button>
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
<div class="{% if field.errors %}errors {% endif %}flex group mb-6 flex-col last:mb-4">
2-
{% include "unfold/helpers/form_label.html" with field=field %}
1+
{% if field.field.widget.input_type == "checkbox" %}
2+
<div class="{% if field.errors %}errors {% endif %}flex flex-col group mb-6 last:mb-4">
3+
<div class="flex flex-row gap-2 items-center">
4+
{{ field }}
35

4-
{{ field }}
6+
{% include "unfold/helpers/form_label.html" with field=field %}
7+
</div>
58

6-
{% include "unfold/helpers/form_errors.html" with errors=field.errors %}
9+
{% include "unfold/helpers/form_errors.html" with errors=field.errors %}
710

8-
{% include "unfold/helpers/help_text.html" with help_text=field.help_text %}
9-
</div>
11+
{% include "unfold/helpers/help_text.html" with help_text=field.help_text %}
12+
</div>
13+
{% else %}
14+
<div class="{% if field.errors %}errors {% endif %}flex flex-col group mb-6 last:mb-4">
15+
{% include "unfold/helpers/form_label.html" with field=field %}
16+
17+
{{ field }}
18+
19+
{% include "unfold/helpers/form_errors.html" with errors=field.errors %}
20+
21+
{% include "unfold/helpers/help_text.html" with help_text=field.help_text %}
22+
</div>
23+
{% endif %}

src/unfold/templates/unfold/helpers/form_label.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<label for="{{ field.id_for_label }}" class="block font-medium mb-2 text-gray-900 text-sm dark:text-gray-200">
1+
<label for="{{ field.id_for_label }}" class="block text-gray-900 text-sm dark:text-gray-200{% if field.field.widget.input_type == "checkbox" %}{% else %} font-medium mb-2{% endif %}">
22
{{ field.label }}
33

44
{% if field.field.required %}

src/unfold/widgets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def __init__(
515515
if attrs is None:
516516
attrs = {}
517517

518-
return super().__init__(
518+
super().__init__(
519519
{
520520
**(attrs or {}),
521521
"class": " ".join(CHECKBOX_CLASSES + [attrs.get("class", "")]),
@@ -528,7 +528,7 @@ class UnfoldBooleanSwitchWidget(CheckboxInput):
528528
def __init__(
529529
self, attrs: Optional[Dict[str, Any]] = None, check_test: Callable = None
530530
) -> None:
531-
return super().__init__(
531+
super().__init__(
532532
attrs={"class": " ".join(SWITCH_CLASSES), **(attrs or {})}, check_test=None
533533
)
534534

@@ -551,4 +551,4 @@ def __init__(
551551
"class": " ".join(["vForeignKeyRawIdAdminField"] + INPUT_CLASSES),
552552
**(attrs or {}),
553553
}
554-
return super().__init__(rel, admin_site, attrs, using)
554+
super().__init__(rel, admin_site, attrs, using)

0 commit comments

Comments
 (0)