Skip to content

Commit

Permalink
fix: import export guess type (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav committed Jun 10, 2024
1 parent ce6e979 commit 91c7439
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
28 changes: 21 additions & 7 deletions src/unfold/contrib/import_export/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,37 @@ class ImportForm(BaseImportForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["resource"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
self.fields["import_file"].widget = UnfoldAdminFileFieldWidget()
self.fields["format"].widget.attrs["class"] = " ".join(SELECT_CLASSES)
self.fields["resource"].widget.attrs["class"] = " ".join(
[self.fields["resource"].widget.attrs.get("class", ""), *SELECT_CLASSES]
)
self.fields["import_file"].widget = UnfoldAdminFileFieldWidget(
attrs=self.fields["import_file"].widget.attrs
)
self.fields["format"].widget.attrs["class"] = " ".join(
[self.fields["format"].widget.attrs.get("class", ""), *SELECT_CLASSES]
)


class ExportForm(BaseExportForm):
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)
self.fields["resource"].widget.attrs["class"] = " ".join(
[self.fields["resource"].widget.attrs.get("class", ""), *SELECT_CLASSES]
)
self.fields["format"].widget.attrs["class"] = " ".join(
[self.fields["format"].widget.attrs.get("class", ""), *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)
self.fields["resource"].widget.attrs["class"] = " ".join(
[self.fields["resource"].widget.attrs.get("class", ""), *SELECT_CLASSES]
)
self.fields["format"].widget.attrs["class"] = " ".join(
[self.fields["format"].widget.attrs.get("class", ""), *SELECT_CLASSES]
)

for _key, field in self.fields.items():
if isinstance(field, BooleanField):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<fieldset class="border border-gray-200 mb-4 rounded-md pt-3 px-3 shadow-sm dark:border-gray-800">
{% 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 %}>
<div {% if field.field.is_selectable_field %}class="selectable-field-export-row" resource-index="{{ field.field.resource_index }}"{% 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" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<input type="text" value="{% if widget.value %}{{ widget.value.url }}{% else %}{% trans 'Choose file to upload' %}{% endif %}" disabled class="bg-white flex-grow font-medium px-3 py-2 text-ellipsis dark:bg-gray-900 {% if widget.value %}text-gray-500 dark:text-gray-400{% else %}text-gray-300 dark:text-gray-400{% endif %}">

<div class="flex flex-none items-center leading-none self-stretch">
<input id="{{ widget.name }}" type="{{ widget.type }}" name="{{ widget.name }}" class="opacity-0 pointer-events-none" {% include "django/forms/widgets/attrs.html" %} />
<input id="{{ widget.name }}" type="{{ widget.type }}" name="{{ widget.name }}" class="{{ widget.file_input_class }}" {% include "django/forms/widgets/attrs.html" %} />

<label for="{{ widget.name }}" class="cursor-pointer text-gray-400 px-3 hover:text-gray-700 dark:text-gray-500 dark:hover:text-gray-200">
<span class="material-symbols-outlined">file_upload</span>
Expand Down
13 changes: 12 additions & 1 deletion src/unfold/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,18 @@ class FileFieldMixin:
def get_context(self, name, value, attrs):
widget = super().get_context(name, value, attrs)
widget["widget"].update(
{"class": " ".join([*CHECKBOX_CLASSES, *["form-check-input"]])}
{
"class": " ".join([*CHECKBOX_CLASSES, *["form-check-input"]]),
"file_input_class": " ".join(
[
self.attrs.get("class", ""),
*[
"opacity-0",
"pointer-events-none",
],
]
),
}
)
return widget

Expand Down

0 comments on commit 91c7439

Please sign in to comment.