Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/unfold/contrib/import_export/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
from import_export.forms import ImportExportFormBase as BaseImportExportFormBase
from unfold.widgets import SELECT_CLASSES, UnfoldAdminFileFieldWidget
from import_export.forms import ExportForm as ExportFormBase
from import_export.forms import ImportForm as ImportFormBase
from unfold.widgets import UnfoldAdminFileFieldWidget, UnfoldAdminSelectWidget


class ImportForm(BaseImportExportFormBase):
class ImportForm(ImportFormBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["resource"].widget = UnfoldAdminFileFieldWidget()
self.fields["format"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
resource_widget = self.fields["resource"].widget
if len(resource_widget.choices) > 0:
self.fields["resource"].widget = UnfoldAdminSelectWidget(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced to using widgets here since they're already part of the unfold project and don't have to rewrite the same logic.
Not sure if there was a proper way of using this widget but this worked instead of just UnfoldAdminSelectWidget

attrs=resource_widget.attrs, choices=resource_widget.choices
)
else:
self.fields.pop("resource")
self.fields["import_file"].widget = UnfoldAdminFileFieldWidget()
format_widget = self.fields["format"].widget
self.fields["format"].widget = UnfoldAdminSelectWidget(
attrs=format_widget.attrs, choices=format_widget.choices
)


class ExportForm(BaseImportExportFormBase):
class ExportForm(ExportFormBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["format"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
format_widget = self.fields["format"].widget
self.fields["format"].widget = UnfoldAdminSelectWidget(
attrs=format_widget.attrs, choices=format_widget.choices
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}

<p class="bg-blue-50 mb-8 text-blue-500 px-3 py-3 rounded-md text-sm dark:bg-blue-500/20 dark:border-blue-500/10">
<div class="bg-blue-50 mb-8 text-blue-500 px-3 py-3 rounded-md text-sm dark:bg-blue-500/20 dark:border-blue-500/10">
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since dl tags can't be inside p tag, switched to div to capture implemented classes

{% trans "This importer will import the following fields: " %}

{% if fields_list|length <= 1 %}
<span class="font-medium">
{{ fields_list.0.1|join:", " }}
</code>
{% else %}
<dl>
<dl class="pt-1">
{% for resource, fields in fields_list %}
<dt>{{ resource }}</dt>
<dd><code>{{ fields|join:", " }}</code></dd>
{% endfor %}
</dl>
{% endif %}
</p>
</div>

<fieldset class="border border-gray-200 mb-8 rounded-md pt-3 px-3 shadow-sm dark:border-gray-800">
{% include "unfold/helpers/field.html" with field=form.resource %}

{% include "unfold/helpers/field.html" with field=form.import_file %}

{% include "unfold/helpers/field.html" with field=form.format %}
</fieldset>

Expand Down