Skip to content

Commit

Permalink
fix: import export selectable fields (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav authored Jun 3, 2024
1 parent 7cc95fe commit a55523e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 32 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 20 additions & 1 deletion src/unfold/contrib/import_export/forms.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,29 @@
{% include "admin/import_export/resource_fields_list.html" with import_or_export="export" %}
{% endif %}

{{ form.non_field_errors }}

<fieldset class="border border-gray-200 mb-4 rounded-md pt-3 px-3 shadow-sm dark:border-gray-800">
{% 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 %}
<div {% if field.field.is_selectable_field %}class="selectable-field-export-row" resource-index="{{ field.field.resource_index }}"{% else %}class="form-row aligned"{% endif %}>
{% if field.field.initial_field %}
<p class="block font-medium mb-2 text-gray-900 text-sm dark:text-gray-200">
{% trans "This exporter will export the following fields" %}
</p>
{% 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 %}
</div>
{% endfor %}

{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
</fieldset>

<button type="submit" class="bg-primary-600 border border-transparent font-medium px-3 py-2 rounded-md text-sm text-white">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
{% include "admin/import_export/resource_fields_list.html" with import_or_export="import" %}

<fieldset class="border border-gray-200 mb-8 rounded-md pt-3 px-3 shadow-sm dark:border-gray-800">
{% 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.import_file %}

{% include "unfold/helpers/field.html" with field=form.format %}
{% for field in form %}
{% 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 %}
</fieldset>


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

{{ field }}
{% include "unfold/helpers/form_label.html" with field=field %}
</div>

{% 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 %}
</div>
{% include "unfold/helpers/help_text.html" with help_text=field.help_text %}
</div>
{% else %}
<div class="{% if field.errors %}errors {% endif %}flex flex-col group mb-6 last:mb-4">
{% 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 %}
</div>
{% endif %}
2 changes: 1 addition & 1 deletion src/unfold/templates/unfold/helpers/form_label.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<label for="{{ field.id_for_label }}" class="block font-medium mb-2 text-gray-900 text-sm dark:text-gray-200">
<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 %}">
{{ field.label }}

{% if field.field.required %}
Expand Down
6 changes: 3 additions & 3 deletions src/unfold/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def __init__(
if attrs is None:
attrs = {}

return super().__init__(
super().__init__(
{
**(attrs or {}),
"class": " ".join(CHECKBOX_CLASSES + [attrs.get("class", "")]),
Expand All @@ -528,7 +528,7 @@ class UnfoldBooleanSwitchWidget(CheckboxInput):
def __init__(
self, attrs: Optional[Dict[str, Any]] = None, check_test: Callable = None
) -> None:
return super().__init__(
super().__init__(
attrs={"class": " ".join(SWITCH_CLASSES), **(attrs or {})}, check_test=None
)

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

0 comments on commit a55523e

Please sign in to comment.