Skip to content

Commit e35c5f5

Browse files
authored
fix: import_export actions form styling (#279)
1 parent a2700bf commit e35c5f5

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,18 @@ class ExampleAdmin(ModelAdmin, ImportExportModelAdmin):
702702
export_form_class = ExportForm
703703
```
704704

705+
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.
706+
707+
```python
708+
admin.py
709+
710+
from unfold.admin import ModelAdmin
711+
from unfold.contrib.import_export import ExportActionModelAdmin
712+
713+
class ExampleAdmin(ModelAdmin, ExportActionModelAdmin):
714+
pass
715+
```
716+
705717
### django-modeltranslation
706718

707719
By default, Unfold supports django-modeltranslation and `TabbedTranslationAdmin` admin class for the tabbed navigation is implemented with custom styling as well.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django import forms
2+
from django.utils.translation import gettext_lazy as _
3+
from import_export.admin import ExportActionModelAdmin as BaseExportActionModelAdmin
4+
from unfold.admin import ActionForm
5+
from unfold.widgets import SELECT_CLASSES
6+
7+
8+
def export_action_form_factory(formats):
9+
class _ExportActionForm(ActionForm):
10+
file_format = forms.ChoiceField(
11+
label=" ",
12+
choices=formats,
13+
required=False,
14+
widget=forms.Select(
15+
{"class": " ".join([*SELECT_CLASSES, "ml-3", "!w-auto", "lg:!w-40"])}
16+
),
17+
)
18+
19+
_ExportActionForm.__name__ = "ExportActionForm"
20+
21+
return _ExportActionForm
22+
23+
24+
class ExportActionModelAdmin(BaseExportActionModelAdmin):
25+
def __init__(self, *args, **kwargs):
26+
super().__init__(*args, **kwargs)
27+
28+
choices = []
29+
formats = self.get_export_formats()
30+
if formats:
31+
for i, f in enumerate(formats):
32+
choices.append((str(i), f().get_title()))
33+
34+
if len(formats) > 1:
35+
choices.insert(0, ("", _("Select format")))
36+
37+
self.action_form = export_action_form_factory(choices)

src/unfold/static/unfold/css/styles.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/unfold/templates/admin/actions.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
{% endblock %}
2020

2121
{% block actions-submit %}
22-
<button type="submit" class="bg-white border flex items-center h-9.5 ml-1 px-2 rounded shadow-sm text-gray-400 w-9.5 hover:text-gray-700 focus:outline-none dark:bg-gray-900 dark:border-gray-700 dark:text-gray-500 dark:hover:text-gray-200" title="{% translate "Run the selected action" %}" name="index" value="{{ action_index|default:0 }}">
22+
<button type="submit" class="bg-white border flex items-center h-9.5 ml-3 px-2 rounded shadow-sm text-gray-400 w-9.5 hover:text-gray-700 focus:outline-none dark:bg-gray-900 dark:border-gray-700 dark:text-gray-500 dark:hover:text-gray-200" title="{% translate "Run the selected action" %}" name="index" value="{{ action_index|default:0 }}">
2323
<span class="material-symbols-outlined md-18">chevron_right</span>
2424
</button>
2525
{% endblock %}
2626
</div>
2727

2828
{% block actions-counter %}
2929
{% if actions_selection_counter %}
30-
<div class="hidden md:block">
30+
<div class="hidden truncate md:block">
3131
<span class="action-counter ml-4 text-sm text-gray-500 dark:text-gray-400" data-actions-icnt="{{ cl.result_list|length }}">
3232
{{ selection_note }}
3333
</span>

0 commit comments

Comments
 (0)